Created
September 8, 2018 12:21
-
-
Save kasper573/4b173fa675a37f1a4fef6ef4f1f290df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getInstances<T, C, B> ( | |
| target: T, | |
| clazz: Class<C>, | |
| transform: (instance: C) => B = noop | |
| ) { | |
| const transformed: Record<keyof SubType<T, C>, B> = {} as any; | |
| for (const key in target) { | |
| const value = target[key]; | |
| if (value instanceof clazz) { | |
| (transformed as any)[key] = transform(value); | |
| } | |
| } | |
| return transformed; | |
| } | |
| function getInstancesRecursive<T, C, B> ( | |
| target: T, | |
| clazz: Class<C>, | |
| transform: (instance: C) => B = noop | |
| ) { | |
| type First = Record<keyof SubType<T, C>, B>; // Instances | |
| type Second = Record<keyof SubType<T, Record<string, C>>, Record<string, B>>; // Records of instances | |
| type Double = First & Second; | |
| const transformed: Double = {} as any; | |
| // Find the instances | |
| Object.assign(transformed, getInstances(target, clazz, transform)); | |
| // Find the records of instances | |
| for (const key in target) { | |
| const value = target[key]; | |
| if (isPlainObject(value)) { | |
| (transformed as any)[key] = getInstances(value, clazz, transform); | |
| } | |
| } | |
| return transformed; | |
| } | |
| function noop (val: any) { | |
| return val; | |
| } | |
| type SubType<Base, Condition> = Pick<Base, { | |
| [Key in keyof Base]: Base[Key] extends Condition ? Key : never | |
| }[keyof Base]>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full Example
Usage Example