-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
54 lines (42 loc) · 1.16 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
module.exports = extend;
module.exports.extend = extend;
function extend(async) {
async.if = builder(ifCheck);
async.ifNot = async.unless = builder(ifNotCheck);
return async;
}
function builder(predicateChecker) {
return function ifFn(predicate, ifStatement) {
var elseStatement = nopStatement;
function wrapper() {
var args = Array.prototype.slice.call(arguments);
var asyncCallback = args[args.length - 1 ];
var predicateArgs = args.slice(0, -1).concat(predicateCb);
predicate.apply(null, predicateArgs);
function predicateCb(error, valid) {
if (error)
return asyncCallback(error);
if (predicateChecker(valid))
ifStatement.apply(null, args);
else
elseStatement.apply(null, args);
}
}
wrapper.else = function(statement) {
elseStatement = statement;
return wrapper;
};
return wrapper;
};
}
function ifCheck(value) {
return value;
}
function ifNotCheck(value) {
return !value;
}
function nopStatement() {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
callback.apply(null, [null].concat(args));
}