forked from documentcloud/underscore-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
underscore.util.operators.js
180 lines (145 loc) · 3.57 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Underscore-contrib (underscore.function.arity.js 0.3.0)
// (c) 2013 Michael Fogus, DocumentCloud and Investigative Reporters & Editors
// Underscore-contrib may be freely distributed under the MIT license.
(function() {
// Baseline setup
// --------------
// Establish the root object, `window` in the browser, or `require` it on the server.
if (typeof exports === 'object') {
_ = module.exports = require('underscore');
}
// Setup for variadic operators
// ----------------------------
// Turn a binary math operator into a variadic operator
function variadicMath(operator) {
return variaderize(function(numbersToOperateOn) {
return _.reduce(numbersToOperateOn, operator);
});
}
// Turn a binary comparator into a variadic comparator
function variadicComparator(comparator) {
return variaderize(function(itemsToCompare) {
var result;
for (var i = 0; i < itemsToCompare.length - 1; i++) {
result = comparator(itemsToCompare[i], itemsToCompare[i + 1]);
if (result === false) return result;
}
return result;
});
}
// Converts a unary function that operates on an array into one that also works with a variable number of arguments
function variaderize(func) {
return function (args) {
var allArgs = isArrayLike(args) ? args : arguments;
return func(allArgs);
};
}
function isArrayLike(obj) {
return obj != null && typeof obj.length === "number";
}
// 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)
});
}).call(this);