-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (141 loc) · 3.48 KB
/
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var acomb = exports;
var _slice = [].slice;
var _nextTick;
if (typeof setImmediate !== "undefined") {
_nextTick = setImmediate;
}
if (!_nextTick) { // browsers
_nextTick = function (fn) {
setTimeout(fn, 0);
};
}
acomb.constant = function constant(value) {
return function (callback) {
return callback(null, value);
};
};
acomb.asyncify = function asyncify(func) {
return function (/*args..., callback*/) {
var callback = _last(arguments),
args = _initial(arguments),
result;
try {
result = func.apply(this, args);
} catch (e) {
return callback(e);
}
callback(null, result);
};
};
acomb.flip = function flip(func) {
return function (cb/*, args..*/ ) {
var args = _rest(arguments);
args.push(cb);
func.apply(this, args);
};
};
acomb.partialRight = function partialRight(func/*, boundArgs... */) {
var boundArgs = _rest(arguments);
return function (/*...args, callback*/) {
var callback = _last(arguments);
var newArgs = _initial(arguments).concat(boundArgs).concat([callback]);
func.apply(this, newArgs);
};
};
acomb.spreadOptions = function spreadOptions(func, option1/*, option2...*/) {
var options;
if (Array.isArray(option1)) {
options = option1;
} else {
options = _rest(arguments);
}
return function (obj, callback) {
var newArgs = options.reduce(function(acc, key) {
acc.push(obj[key]);
return acc;
}, []);
newArgs.push(callback);
func.apply(this, newArgs);
};
};
acomb.before = function before(func, asyncBody) {
return function (/* args..., callback */) {
var args = _initial(arguments);
var callback = _last(arguments);
var result;
try {
result = func.apply(this, args);
} catch (e) {
return callback(e);
}
asyncBody.call(this, result, callback);
};
};
acomb.after = function after(asyncBody, func) {
return function (/* args..., callback */) {
var args = _initial(arguments);
var callback = _last(arguments);
args.push(function newCb(err/*, results...*/) {
if (err) { return callback(err); }
var results = _rest(arguments);
var result;
try {
result = func.apply(null, results);
} catch (e) {
return callback(e);
}
callback(null, result);
});
asyncBody.apply(this, args);
};
};
acomb.provided = function provided(predicate, func) {
return function (/*args..., callback*/) {
var args = _initial(arguments);
var callback = _last(arguments);
var result;
if (predicate === true || predicate === false) {
result = predicate;
} else {
try {
result = predicate.apply(this, args);
} catch (e) {
return callback(e);
}
}
if (result) {
return func.apply(this, arguments);
} else {
callback.apply(null, [null].concat(args));
}
};
};
acomb.ensureAsync = function ensureAsync(func) {
return function (/*args..., cb*/) {
var args = _initial(arguments);
var cb = _last(arguments);
var sameStack = true;
args.push(function () {
if (sameStack) {
return _defer(cb, arguments);
}
cb.apply(null, arguments);
});
func.apply(this, args);
sameStack = false;
};
};
function _last(arr) {
return arr[arr.length - 1];
}
function _initial(arr) {
return _slice.call(arr, 0, arr.length - 1);
}
function _rest(arr) {
return _slice.call(arr, 1);
}
function _defer(fn, args) {
_nextTick(function () {
fn.apply(null, args);
});
}