-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (76 loc) · 2.29 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
/*
Copyright (C) 2013 Sander Steenhuis; http://www.Redsandro.com/ <[email protected]>
This is free software, and is released under the terms of the GPL version 2 or (at your option) any later version.
See license.txt or <http://www.gnu.org/licenses/>.
*/
/**
* Qtree - Allows switching and branching promise chains so we can completely separate promise flow from boring code.
* It's as sexy as separating content from markup.
*/
/**
* Modules
*/
var q = require('q');
module.exports = q;
/**
* Promise prototypes
*/
q.makePromise.prototype.switchIf = function (testFunc, thenFunc, elseFunc, throwBool) {
return switchIf(this, testFunc, thenFunc, elseFunc, throwBool);
};
// Synonym - if is a perfectly legal method. but most IDEs complain because it's also a keyword.
q.makePromise.prototype.if = q.makePromise.prototype.switchIf;
//Object.defineProperty(q.makePromise.prototype, 'if', {
// enumerable : false,
// value : function(testFunc, thenFunc, elseFunc, throwBool) {
// return switchIf(this, testFunc, thenFunc, elseFunc, throwBool);
// }
//});
/**
* Juggling
*/
exports.switchIf = q.switchIf = switchIf;
function switchIf(val, testFunc, thenFunc, elseFunc, throwBool) {
var deferred = q.defer();
// By default, values will be passed unchanged when output function is not defined.
// Explicitly set throwBool to true will throw a new error instead.
var reject = false;
if (throwBool === true)
reject = true;
// Testfunction is required.
if (typeof testFunc !== 'function') {
if (reject)
deferred.reject('testFunc not specified.');
else
deferred.resolve(val);
return deferred.promise;
}
// Without a thenFunc, just pass through or throw error.
if (typeof thenFunc !== 'function') {
var thenFunc = (reject)
? function(val) {
throw new Error('Value matched: ' + val);
}
: function(val) {
return val;
}
;
}
// Without an elseFunc, just pass through or throw error.
if (typeof elseFunc !== 'function') {
var elseFunc = (reject)
? function(val) {
throw new Error('Value mismatched: ' + val);
}
: function(val) {
return val;
}
;
}
// Start with original promise
deferred.resolve(val);
// Append branches to promise
return deferred.promise.then(function(val) {
return testFunc(val) ? thenFunc(val) : elseFunc(val);
});
}