-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathoperators.js
153 lines (116 loc) · 2.82 KB
/
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
require("collections/shim-object"); // equals, compare
require("collections/shim-regexp"); // escape
var Map = require("collections/map");
var Set = require("collections/set");
// from highest to lowest precedence
exports.toNumber = function (s) {
return +s;
};
exports.toString = function (value) {
if (value == null) {
return value;
} else if (typeof value === "string" || typeof value === "number") {
return "" + value;
} else {
return null;
}
};
exports.toArray = Array.from;
exports.toMap = Map.from.bind(Map);
exports.toSet = Set.from.bind(Set);
exports.not = function (b) {
return !b;
};
exports.neg = function (n) {
return -n;
};
exports.pow = function (a, b) {
return Math.pow(a, b);
};
exports.root = function (a, b) {
return Math.pow(a, 1 / b);
};
exports.log = function (a, b) {
return Math.log(a) / Math.log(b);
};
exports.mul = function (a, b) {
return a * b;
};
exports.div = function (a, b) {
return a / b;
};
exports.mod = function (a, b) {
return ((a % b) + b) % b;
};
exports.rem = function (a, b) {
return a % b;
};
exports.add = function (a, b) {
return a + b;
};
exports.sub = function (a, b) {
return a - b;
};
exports.ceil = function (n) {
return Math.ceil(n);
};
exports.floor = function (n) {
return Math.floor(n);
};
exports.round = function (n) {
return Math.round(n);
};
exports.lt = function (a, b) {
return Object.compare(a, b) < 0;
};
exports.gt = function (a, b) {
return Object.compare(a, b) > 0;
};
exports.le = function (a, b) {
return Object.compare(a, b) <= 0;
};
exports.ge = function (a, b) {
return Object.compare(a, b) >= 0;
};
exports.equals = Object.equals;
exports.compare = Object.compare;
exports.and = function (a, b) {
return a && b;
};
exports.or = function (a, b) {
return a || b;
};
exports.defined = function (value) {
return value != null;
};
// "startsWith", "endsWith", and "contains" are overridden in
// complile-observer so they can precompile the regular expression and reuse it
// in each reaction.
exports.startsWith = function (a, b) {
var expression = new RegExp("^" + RegExp.escape(b));
return expression.test(a);
};
exports.endsWith = function (a, b) {
var expression = new RegExp(RegExp.escape(b) + "$");
return expression.test(a);
};
exports.contains = function (a, b) {
var expression = new RegExp(RegExp.escape(b));
return expression.test(a);
};
exports.join = function (a, b) {
return a.join(b || "");
};
exports.split = function (a, b) {
return a.split(b || "");
};
exports.range = function (stop) {
var range = [];
for (var start = 0; start < stop; start++) {
range.push(start);
}
return range;
};
exports.last = function (collection) {
return collection.get(collection.length - 1);
};