Skip to content

Commit ee1f1a2

Browse files
authored
feat: impl iterator of moduleConfigs (#386)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [x] `npm test` passes - [x] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. --> Feat: support iterator of moduleConfigs <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Module configuration API now supports direct iteration over entries for easier traversal and inspection. * **Tests** * Added tests verifying iteration behavior, entry order and content, and empty-configuration scenarios. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent d319448 commit ee1f1a2

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

core/common-util/src/ModuleConfigs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ export class ModuleConfigs {
77
get(moduleName: string): ModuleConfig | undefined {
88
return this.inner[moduleName]?.config;
99
}
10+
11+
* [Symbol.iterator](): Iterator<[string, ModuleConfigHolder]> {
12+
yield* Object.entries(this.inner);
13+
}
1014
}

core/common-util/test/ModuleConfig.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { strict as assert } from 'node:assert';
22
import path from 'node:path';
33
import { ModuleConfigUtil } from '../src/ModuleConfig';
4+
import { ModuleConfigs } from '../src/ModuleConfigs';
45
import type { ModuleReference } from '@eggjs/tegg-types';
56

67
describe('test/ModuleConfig.test.ts', () => {
@@ -140,6 +141,45 @@ describe('test/ModuleConfig.test.ts', () => {
140141
}]);
141142
});
142143
});
144+
145+
it('should iterate over all module configs', () => {
146+
const mockInner = {
147+
module1: {
148+
name: 'module1',
149+
reference: { path: '/path/to/module1', name: 'module1' },
150+
config: { foo: 'bar' },
151+
},
152+
module2: {
153+
name: 'module2',
154+
reference: { path: '/path/to/module2', name: 'module2' },
155+
config: { baz: 'qux' },
156+
},
157+
};
158+
159+
const moduleConfigs = new ModuleConfigs(mockInner);
160+
const result: Array<[string, any]> = [];
161+
162+
for (const [ name, holder ] of moduleConfigs) {
163+
result.push([ name, holder ]);
164+
}
165+
166+
assert.strictEqual(result.length, 2);
167+
assert.strictEqual(result[0][0], 'module1');
168+
assert.deepStrictEqual(result[0][1], mockInner.module1);
169+
assert.strictEqual(result[1][0], 'module2');
170+
assert.deepStrictEqual(result[1][1], mockInner.module2);
171+
});
172+
173+
it('should work with empty configs', () => {
174+
const moduleConfigs = new ModuleConfigs({});
175+
const result: Array<[string, any]> = [];
176+
177+
for (const [ name, holder ] of moduleConfigs) {
178+
result.push([ name, holder ]);
179+
}
180+
181+
assert.strictEqual(result.length, 0);
182+
});
143183
});
144184

145185
describe('ModuleConfigUtil.deduplicateModules', () => {
@@ -323,3 +363,4 @@ describe('ModuleConfigUtil.deduplicateModules', () => {
323363
});
324364
});
325365
});
366+

0 commit comments

Comments
 (0)