-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathannotations.js
68 lines (60 loc) · 1.6 KB
/
annotations.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
export class Directive {
constructor(name, options){
this.name = name;
this.options = options;
}
}
export class Filter {
constructor(name) {
this.name = name;
}
}
export class Inject {
constructor(...deps){
this.deps = [];
for (let dep of deps) {
this.deps = this.deps.concat(dep.replace(/\s+/g, '').split(','));
}
}
}
export class InjectAsProperty {
constructor(name, propertyName = null) {
this.name = name;
this.propertyName = propertyName || name;
}
}
/**
* A annotation parser class which allows you to extract particular annotation type
* of a class/function.
*
*/
export class Parser {
/**
*
* @param constructor the actual class or function
*/
constructor(constructor) {
this.constructor = constructor;
}
getAllAnnotations() {
return this.annotations || (this.annotations = this.extractAnnotations(this.constructor));
}
getAnnotations(annotationConstructor) {
var annotations = this.getAllAnnotations();
var result = [];
for (let annotation of annotations) {
if (annotation instanceof annotationConstructor) {
result.push(annotation);
}
}
return result;
}
extractAnnotations(constructor) {
var annotations = constructor.annotations || [];
var parent = Object.getPrototypeOf(constructor);
if ('function' === typeof parent) {
annotations = annotations.concat(this.extractAnnotations(parent));
}
return annotations;
}
}