-
Notifications
You must be signed in to change notification settings - Fork 20
/
inject.ts
40 lines (35 loc) · 1.21 KB
/
inject.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
39
40
import { INJECT_TAG } from '..';
import { ObjectIdentifier } from '../interfaces';
import { tagParameter, tagProperty } from '../utils/decorator';
import { Metadata } from '../utils/metadata';
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
const ARGUMENT_NAMES = /([^\s,]+)/g;
function getParamNames(func) {
const fnStr = func.toString().replace(STRIP_COMMENTS, '');
let result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if (result === null) {
result = [];
}
return result;
}
function inject(identifier?: ObjectIdentifier) {
return function (target: any, targetKey: string, index?: number): void {
if (typeof index === 'number') {
if (!identifier) {
const args = getParamNames(target);
if (target.length === args.length && index < target.length) {
identifier = args[index];
}
}
const metadata = new Metadata(INJECT_TAG, identifier);
tagParameter(target, targetKey, index, metadata);
} else {
if (!identifier) {
identifier = targetKey;
}
const metadata = new Metadata(INJECT_TAG, identifier);
tagProperty(target, targetKey, metadata);
}
};
}
export { inject };