Skip to content

Commit 04dff41

Browse files
committed
refactor: metadata method, use reduce
1 parent 4a15109 commit 04dff41

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/polymorphic.repository.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,41 +36,37 @@ const PrimaryColumn = (options: PolymorphicMetadataInterface): string =>
3636

3737
export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
3838
private getPolymorphicMetadata(): Array<PolymorphicMetadataInterface> {
39-
let keys = Reflect.getMetadataKeys(
39+
const keys = Reflect.getMetadataKeys(
4040
(this.metadata.target as Function)['prototype'],
4141
);
4242

43-
if (!Array.isArray(keys)) {
44-
return [];
45-
}
46-
47-
keys = keys.filter((key: string) => {
48-
const parts = key.split('::');
49-
return parts[0] === POLYMORPHIC_OPTIONS;
50-
});
51-
5243
if (!keys) {
5344
return [];
5445
}
5546

56-
return keys
57-
.map((key: string): PolymorphicMetadataInterface | undefined => {
58-
const data: PolymorphicMetadataOptionsInterface & {
59-
propertyKey: string;
60-
} = Reflect.getMetadata(
61-
key,
62-
(this.metadata.target as Function)['prototype'],
63-
);
64-
65-
if (typeof data === 'object') {
66-
const classType = data.classType();
67-
return {
68-
...data,
69-
classType,
70-
};
47+
return keys.reduce<Array<PolymorphicMetadataInterface>>(
48+
(keys: PolymorphicMetadataInterface[], key: string) => {
49+
if (key.split('::')[0] === POLYMORPHIC_OPTIONS) {
50+
const data: PolymorphicMetadataOptionsInterface & {
51+
propertyKey: string;
52+
} = Reflect.getMetadata(
53+
key,
54+
(this.metadata.target as Function)['prototype'],
55+
);
56+
57+
if (data && typeof data === 'object') {
58+
const classType = data.classType();
59+
keys.push({
60+
...data,
61+
classType,
62+
});
63+
}
7164
}
72-
})
73-
.filter((val) => typeof val !== 'undefined');
65+
66+
return keys;
67+
},
68+
[],
69+
);
7470
}
7571

7672
protected isPolymorph(): boolean {

0 commit comments

Comments
 (0)