This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (47 loc) · 1.33 KB
/
index.js
File metadata and controls
53 lines (47 loc) · 1.33 KB
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
/**
* decorate
* @param {...Object | Array} decorators
* @param {Object} target
*/
module.exports = function decorate() {
var decorators = Array.prototype.slice.apply(arguments);
var target = decorators.pop();
// for easy multiline declaration in CoffeeScript
if (decorators[0] instanceof Array) {
decorators = decorators[0];
}
if (typeof decorators[0] === 'string') {
return methodTarget(decorators, target);
} else {
return classTarget(decorators, target);
}
};
function methodTarget(decorators, target) {
var name = decorators.shift();
var prototype = decorators.shift().prototype;
var descriptor = {
value: target,
writable: true,
enumerable: true,
configurable: true
};
for (var i = 0, len = decorators.length; i < len; i++) {
decorator = decorators[i];
var newDescriptor = decorator(prototype, name, descriptor);
if (newDescriptor != null) {
descriptor = newDescriptor;
}
}
Object.defineProperty(prototype, name, descriptor);
return prototype[name];
}
function classTarget(decorators, target) {
for (var i = 0, len = decorators.length; i < len; i++) {
decorator = decorators[i];
var newTarget = decorator(target);
if (newTarget != null) {
target = newTarget;
}
}
return target;
}