forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata-scanner.ts
38 lines (36 loc) · 1.1 KB
/
metadata-scanner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import iterate from 'iterare';
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import {
isConstructor,
isFunction,
isNil,
} from '@nestjs/common/utils/shared.utils';
export class MetadataScanner {
public scanFromPrototype<T extends Injectable, R>(
instance: T,
prototype,
callback: (name: string) => R,
): R[] {
return iterate([...this.getAllFilteredMethodNames(prototype)])
.map(callback)
.filter(metadata => !isNil(metadata))
.toArray();
}
*getAllFilteredMethodNames(prototype): IterableIterator<string> {
do {
yield* iterate(Object.getOwnPropertyNames(prototype))
.filter(prop => {
const descriptor = Object.getOwnPropertyDescriptor(prototype, prop);
if (descriptor.set || descriptor.get) {
return false;
}
return !isConstructor(prop) && isFunction(prototype[prop]);
})
.toArray();
} while (
// tslint:disable-next-line:no-conditional-assignment
(prototype = Reflect.getPrototypeOf(prototype)) &&
prototype !== Object.prototype
);
}
}