Skip to content

Commit

Permalink
fix: parent-metadata extends ability
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored and alex-zhang committed Sep 19, 2020
1 parent 24ea5d1 commit 746e97c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augejs/provider-scanner",
"version": "0.0.29",
"version": "0.0.30",
"description": "`provider-scanner` is a scan framework for nodejs.",
"main": "dist/main.js",
"scripts": {
Expand Down
20 changes: 18 additions & 2 deletions src/metadata/Metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,29 @@ export class Metadata {
}

static defineInsertEndArrayMetadata(key: any, metadata: any[], target: Object, propertyKey?: string | symbol):void {
const previousValue:[] = Metadata.getMetadata(key, target, propertyKey) || [];
if (metadata.length === 0) return;

const previousValue: any[] = Metadata.getMetadata(key, target, propertyKey) || [];
if (previousValue.length > 0) {
metadata = metadata.filter(item => {
return !previousValue.includes(item);
})
}

const value = [...previousValue, ...metadata];
Metadata.defineMetadata(key, value, target, propertyKey);
}

static defineInsertBeginArrayMetadata(key: any, metadata: any[], target: Object, propertyKey?: string | symbol):void {
const previousValue:[] = Metadata.getMetadata(key, target, propertyKey) || [];
if (metadata.length === 0) return;
const previousValue: any[] = Metadata.getMetadata(key, target, propertyKey) || [];

if (previousValue.length > 0) {
metadata = metadata.filter(item => {
return !previousValue.includes(item);
})
}

const value = [...metadata, ...previousValue];
Metadata.defineMetadata(key, value, target, propertyKey);
}
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/ParentMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('decorators: Provider', () => {
class Y {};
ParentMetadata.defineMetadata(Y, [{name:1},{name:2},{name:3}]);
ParentMetadata.defineMetadata(Y, []);
expect(ParentMetadata.getMetadata(Y)).toEqual([]);
expect(ParentMetadata.getMetadata(Y)).toEqual([{name:1},{name:2},{name:3}]);
})

it('should Provider decorator has correctly many array value', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/ParentMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ParentMetadata {
parentResults.push(child);
}
}
Metadata.defineMetadata(ParentMetadata, parentResults, parentTarget);
Metadata.defineInsertEndArrayMetadata(ParentMetadata, parentResults, parentTarget);
}

iterateChildren(target, children, []);
Expand Down

0 comments on commit 746e97c

Please sign in to comment.