-
Notifications
You must be signed in to change notification settings - Fork 5
/
hooks.js
40 lines (36 loc) · 893 Bytes
/
hooks.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
function withHookBefore (originalFn, hookFn) {
return function () {
if (hookFn.apply(this, arguments) === false) {
return
}
return originalFn.apply(this, arguments)
}
}
function withHookAfter (originalFn, hookFn) {
return function () {
var output = originalFn.apply(this, arguments)
hookFn.apply(this, arguments)
return output
}
}
function hookArgs (originalFn, argsGetter) {
return function () {
var _args = argsGetter.apply(this, arguments)
if (Array.isArray(_args)) {
for (var i = 0; i < _args.length; i++) arguments[i] = _args[i]
}
return originalFn.apply(this, arguments)
}
}
function hookOutput (originalFn, outputGetter) {
return function () {
var _output = originalFn.apply(this, arguments)
return outputGetter(_output)
}
}
module.exports = {
withHookBefore,
withHookAfter,
hookArgs,
hookOutput
}