-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
42 lines (36 loc) · 854 Bytes
/
index.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
'use strict';
var slice = Array.prototype.slice;
function before(base, fn) {
return function() {
var args = slice.call(arguments);
fn.apply(this, args);
return base.apply(this, args);
};
}
function after(base, fn) {
return function() {
var args = slice.call(arguments);
var result = base.apply(this, args);
fn.apply(this, args);
return result;
};
}
function around(base, fn) {
return function() {
var args = slice.call(arguments);
args.unshift(base);
return fn.apply(this, args);
};
}
function create(advice) {
return function(context, name, fn) {
if ('function' === typeof name) {
return advice(context, name);
} else {
context[name] = advice(context[name], fn);
}
};
}
exports.before = create(before);
exports.after = create(after);
exports.around = create(around);