Skip to content

Commit

Permalink
fix: get function args error (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Nov 19, 2020
1 parent 9697834 commit 7ec5317
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/decorator/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export function sleep(sleepTime = 1000) {
}

const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;
const ARGUMENT_NAMES = /([^\s,]+)/g;

/**
* get parameter name from function
Expand All @@ -85,8 +84,12 @@ export function getParamNames(func): string[] {
const fnStr = func.toString().replace(STRIP_COMMENTS, '');
let result = fnStr
.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')'))
.match(ARGUMENT_NAMES);
if (result === null) {
.split(',')
.map(content => {
return content.trim().replace(/\s?=.*$/, '');
});

if (result.length === 1 && result[0] === '') {
result = [];
}
return result;
Expand Down
12 changes: 12 additions & 0 deletions packages/decorator/test/common/decoratorManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ describe('/test/common/decoratorManager.test.ts', () => {

args = getParamNames(() => {});
assert(args.length === 0);

args = getParamNames((a) => {});
assert(args.length === 1);

args = getParamNames((a,b) => {});
assert(args.length === 2);

args = getParamNames((a, b=1) => {});
assert(args.length === 2);

args = getParamNames((a = 1, b =2, c) => {});
assert(args.length === 3);
});

it('should get attach data from method', () => {
Expand Down
40 changes: 40 additions & 0 deletions packages/decorator/test/util/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
isAsyncFunction,
isClass,
isFunction,
isGeneratorFunction,
isMap,
isPromise,
isObject,
isNumber,
isSet,
isRegExp,
sleep,
isProxy
} from '../../src/util';

describe('/test/util/index.test.ts', () => {
it('should test util method', async () => {
expect(isAsyncFunction(async () => {
})).toBeTruthy();
expect(isClass(class A {
})).toBeTruthy();
expect(isFunction(() => {
})).toBeTruthy();
expect(isGeneratorFunction(function* () {
})).toBeTruthy();
expect(isMap(new Map())).toBeTruthy();
expect(isPromise(new Promise(() => {
}))).toBeTruthy();

expect(isObject({})).toBeTruthy();

expect(isNumber(2)).toBeTruthy();
expect(isSet(new Set())).toBeTruthy();
expect(isRegExp(/^a/)).toBeTruthy();
expect(isProxy(new Proxy({}, {}))).toBeTruthy();
const startTime = Date.now();
await sleep(500);
expect(Date.now() - startTime).toBeGreaterThanOrEqual(500);
});
});

0 comments on commit 7ec5317

Please sign in to comment.