Skip to content

Commit bdb8346

Browse files
committed
feat(schema): expose iterator methods
1 parent 83f0978 commit bdb8346

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

.changeset/two-ghosts-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@dreamkit/schema": patch
3+
---
4+
5+
Expose iterator methods

packages/schema/src/types/ObjectType.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export class ObjectType<
203203
return new ObjectType(props, otherOptions);
204204
}
205205

206-
private *createIterator(
206+
*iterator(
207207
options: ObjectTypeIteratorOptions = {},
208208
): Generator<ObjectTypeIteratorItem, void, unknown> {
209209
const entry = options.followData ? options.data : this.props;
@@ -221,9 +221,24 @@ export class ObjectType<
221221
};
222222
}
223223
}
224+
225+
*deepIterator(
226+
options: {
227+
parentPath?: string[];
228+
} = {},
229+
): Generator<ObjectTypeIteratorItem, void, unknown> {
230+
for (const item of this.iterator(options)) {
231+
yield item;
232+
if (item.objectType)
233+
yield* item.objectType.deepIterator({
234+
parentPath: item.path,
235+
});
236+
}
237+
}
238+
224239
createWith(options: ObjectTypeCreatorOptions = {}): any {
225240
const output = options.output ?? {};
226-
const it = this.createIterator(options);
241+
const it = this.iterator(options);
227242
for (const item of it) {
228243
let pathName: string | undefined;
229244
const subItem = {
@@ -254,7 +269,7 @@ export class ObjectType<
254269

255270
async createWithAsync(options: ObjectTypeCreatorOptions = {}): Promise<any> {
256271
const output = options.output ?? {};
257-
const it = this.createIterator(options);
272+
const it = this.iterator(options);
258273
for (const item of it) {
259274
let pathName: string | undefined;
260275
const subItem = {
@@ -531,19 +546,6 @@ export class ObjectType<
531546
) as P,
532547
});
533548
}
534-
iterateProps(
535-
cb: (name: string, type: $.Type) => void | false,
536-
parentPath: string[] = [],
537-
) {
538-
const it = this.createIterator({ parentPath });
539-
for (const item of it) {
540-
if (item.objectType) {
541-
item.objectType.iterateProps(cb, item.path);
542-
} else if (cb(item.path.join("."), item.type) === false) {
543-
break;
544-
}
545-
}
546-
}
547549
query<Q extends $.TypeFlag.Query>(
548550
flags: Q,
549551
outMask?: RecursiveRecord<boolean>,
@@ -559,7 +561,10 @@ export class ObjectType<
559561
const nextOutMask = outMask ? (outMask[name] = {}) : undefined;
560562
const objectProp = prop.query(flags, nextOutMask);
561563
let someChildProp = false;
562-
objectProp.iterateProps(() => !(someChildProp = true));
564+
for (const _ of objectProp.deepIterator()) {
565+
someChildProp = true;
566+
break;
567+
}
563568
if (someChildProp) {
564569
props[name] = objectProp;
565570
} else {

0 commit comments

Comments
 (0)