-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun.js
110 lines (86 loc) · 2.04 KB
/
fun.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
/*global module*/
var fun = {
// Basic stuff.
id : function (x) {
return x;
},
const : function (c) {
return function () {
return c;
};
},
exists : function (subject) {
return subject !== null && subject !== undefined;
},
// Object access.
prop : function (key) {
return function (obj) {
return obj[key];
};
},
safeProp : function (key) {
return function (obj) {
if (fun.exists(obj)) {
return obj[key];
}
return obj;
};
},
invoke : function (key) {
var args = [];
for (var i = 1; i < arguments.length; i += 1) {
args[i-1] = arguments[i];
}
return function (obj) {
return obj[key].apply(obj, args);
};
},
// Argument handling.
flipArgs : function (func) {
return function () {
var args = [];
for (var i = 0; i < arguments.length; i += 1) {
args.unshift(arguments[i]);
}
return func.apply(this, args);
};
},
// Boolean logic.
not : function (func) {
return function () {
return !func.apply(this, arguments);
};
},
and : function (f, g) {
return function () {
return f.apply(this, arguments) && g.apply(this, arguments);
};
},
or : function (f, g) {
return function () {
return f.apply(this, arguments) || g.apply(this, arguments);
};
},
// Those math things we always wanted.
add : function (a, b) {
return a + b;
},
sub : function (a, b) {
return a - b;
},
mult : function (a, b) {
return a * b;
},
div : function (a, b) {
return a / b;
},
floorDiv : function (a, b) {
return Math.floor(a / b);
},
mod : function (a, b) {
return a % b;
}
};
if (typeof module !== 'undefined') {
module.exports = fun;
}