forked from documentcloud/underscore-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunderscore.util.operators.js
164 lines (133 loc) · 3.08 KB
/
underscore.util.operators.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
160
161
162
163
164
// Underscore-contrib (underscore.function.arity.js 0.0.1)
// (c) 2013 Michael Fogus, DocumentCloud and Investigative Reporters & Editors
// Underscore-contrib may be freely distributed under the MIT license.
(function(root) {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `global` on the server.
var _ = root._ || require('underscore');
// Setup for variadic operators
// ----------------------------
// Turn a binary math operator into a variadic operator
function variadicMath(operator) {
return function() {
return _.reduce(arguments, operator);
};
}
// Turn a binary comparator into a variadic comparator
function variadicComparator(comparator) {
return function() {
var result;
for (var i = 0; i < arguments.length - 1; i++) {
result = comparator(arguments[i], arguments[i + 1]);
if (result === false) return result;
}
return result;
};
}
// Turn a boolean-returning function into one with the opposite meaning
function invert(fn) {
return function() {
return !fn.apply(this, arguments);
};
}
// Basic math operators
function add(x, y) {
return x + y;
}
function sub(x, y) {
return x - y;
}
function mul(x, y) {
return x * y;
}
function div(x, y) {
return x / y;
}
function mod(x, y) {
return x % y;
}
function inc(x) {
return ++x;
}
function dec(x) {
return --x;
}
function neg(x) {
return -x;
}
// Bitwise operators
function bitwiseAnd(x, y) {
return x & y;
}
function bitwiseOr(x, y) {
return x | y;
}
function bitwiseXor(x, y) {
return x ^ y;
}
function bitwiseLeft(x, y) {
return x << y;
}
function bitwiseRight(x, y) {
return x >> y;
}
function bitwiseZ(x, y) {
return x >>> y;
}
function bitwiseNot(x) {
return ~x;
}
// Basic comparators
function eq(x, y) {
return x == y;
}
function seq(x, y) {
return x === y;
}
// Not
function not(x) {
return !x;
}
// Relative comparators
function gt(x, y) {
return x > y;
}
function lt(x, y) {
return x < y;
}
function gte(x, y) {
return x >= y;
}
function lte(x, y) {
return x <= y;
}
// Mixing in the operator functions
// -----------------------------
_.mixin({
add: variadicMath(add),
sub: variadicMath(sub),
mul: variadicMath(mul),
div: variadicMath(div),
mod: mod,
inc: inc,
dec: dec,
neg: neg,
eq: variadicComparator(eq),
seq: variadicComparator(seq),
neq: invert(variadicComparator(eq)),
sneq: invert(variadicComparator(seq)),
not: not,
gt: variadicComparator(gt),
lt: variadicComparator(lt),
gte: variadicComparator(gte),
lte: variadicComparator(lte),
bitwiseAnd: variadicMath(bitwiseAnd),
bitwiseOr: variadicMath(bitwiseOr),
bitwiseXor: variadicMath(bitwiseXor),
bitwiseNot: bitwiseNot,
bitwiseLeft: variadicMath(bitwiseLeft),
bitwiseRight: variadicMath(bitwiseRight),
bitwiseZ: variadicMath(bitwiseZ)
});
})(this);