-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (73 loc) · 2.51 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
const prototypeHelper = require("./prototypeHelper");
prototypeHelper.bindTypes({
whatIf,
whatIfNotNull,
whatIfNotUndefined,
whatIfNotNullOrEmpty
});
prototypeHelper.monkeyPatchBind();
/**
* An expression for invoking action when the given value is true.
* NOTE: When using directly with boolean, given becomes the boolean passed
* so pass your action in the given parameter.
* @param {any} given - the condition
* @param {function} action - the action to perform when condition is true
* @param {function} actionWhatIfNot - the action to perform when condition is not true
*/
function whatIf(given, action, actionWhatIfNot = null) {
given =
(typeof given === "function" && given) ||
(given != null && given != undefined && given == true); //non-strict mode is intentional
return whatIfNot.call(this, given, action, actionWhatIfNot);
}
/**
* An expression for invoking action when the value is not null.
* @param {function} action - the action to perform when the value is not null
* @param {function} actionWhatIfNot - the action to perform when the value is null.
*/
function whatIfNotNull(action, actionWhatIfNot) {
let given = this;
return whatIfNot.call(this, given !== null, action, actionWhatIfNot);
}
/**
* An expression for invoking action when the value is not undefined.
* @param {function} action - the action to perform when the value is not undefined.
* @param {function} actionWhatIfNot - the action to perform when the value is undefined.
*/
function whatIfNotUndefined(action, actionWhatIfNot) {
let given = this;
return whatIfNot.call(this, given !== undefined, action, actionWhatIfNot);
}
/**
* An expression for invoking action when the given array is not null & not empty.
* @param {function} action - the action to perform
* @param {function} actionWhatIfNot - the action to perform when the array is null or empty.s
*/
function whatIfNotNullOrEmpty(action, actionWhatIfNot) {
if (!Array.isArray(this)) return this;
let given = this;
return whatIfNot.call(
this,
given !== null && given.length > 0,
action,
actionWhatIfNot
);
}
/**
* @ignore
*/
function whatIfNot(given, action, actionWhatIfNot) {
if (typeof given === "function") {
actionWhatIfNot = action;
action = given;
given = this;
}
if (given) {
return typeof this === "function"
? this.boundObject
? action.call(this, this())
: action
: (action && action.call(this, this)) || this;
} else if (actionWhatIfNot) return actionWhatIfNot(this);
else return this;
}