-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontextDecorator.js
42 lines (35 loc) · 1.09 KB
/
contextDecorator.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
const decorateArrow = (property, descriptor) => {
var original = descriptor.initializer
descriptor.initializer = function initializer() {
// sets the name of the function to `property`
const temp = {
[property]() {
return original().apply(
this,
[this].concat(Array.prototype.slice.call(arguments))
)
}
}
return temp[property]
}
}
const decorateFunction = (property, descriptor) => {
const original = descriptor.value
// sets the name of the function to `property`
const temp = {
[property]() {
return original.apply(
this,
[this].concat(Array.prototype.slice.call(arguments))
)
}
}
descriptor.value = temp[property]
}
const isArrow = descriptor => typeof descriptor.initializer === 'function'
const isFunction = descriptor => typeof descriptor.value === 'function'
const contextDecorator = (target, property, descriptor) => {
if (isArrow(descriptor)) decorateArrow(property, descriptor)
if (isFunction(descriptor)) decorateFunction(property, descriptor)
}
module.exports = contextDecorator