-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
517 lines (411 loc) · 14.5 KB
/
util.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
if (typeof window !== 'undefined')
window.global = window;
/* start of highly disagreeable things */
EXPORT = function (words) {
// have to wrap it in parens to prevent it from being interpreted as a block with labels
return "({"+map(function (word) { return word+":"+word; }, words).join(",\n")+"})";
};
// returns an evalable string that imports properties of the given module into the current scope.
// module should be the module name (a string)
IMPORT = function (module) {
return IMPORT_EXCLUDING(module, []);
};
// import only the given properties
IMPORT_INCLUDING = function (module, inclusions) {
return "{"+
map(function (inclusion) {
var value = module+"."+inclusion;
return "if (typeof "+inclusion+" === 'undefined') \n"+
" var "+inclusion+" = (typeof "+value+" === 'function') \n"+
" ? "+value+".bind("+module+") \n"+
" : "+value+"; \n"+
"else throw new Error('attempt to import already-defined variable'); \n";
}, inclusions).join("\n")
+"}";
};
// import all but the given properties
IMPORT_EXCLUDING = function (module, exclusions) {
return "eval(IMPORT_INCLUDING('"+module+"', (function (module, exclusions) { \n"+
" var words = []; \n"+
" for (var word in "+module+") { \n"+
" if (!contains(word, exclusions)) \n"+
" words.push(word); \n"+
" } \n"+
" return words; \n"+
"})("+module+", "+JSON.stringify(exclusions)+"))) \n";
};
/* end of highly disagreeable things */
Array.slice = Array.slice || function (array, begin, end) { return Array.prototype.slice.apply(array, [begin, end]); };
Array.concat = Array.concat || function (/* arrays */) { return Array.prototype.concat.apply([], arguments); };
// unlike CL
prepend = function (obj, list) { return concatenate([obj], list); };
append = function (obj, list) { return concatenate(list, [obj]); };
// don't use Array.concat because it's sloppy (arguments can be non-lists) and treats "arguments" objects as non-lists
// use low-level javascript to be able to handle argument objects and avoid infinite recursion through apply()
concatenate = function (/* lists... */) {
var result = [];
for (i = 0; i < arguments.length; i++) {
for (var j = 0; j < arguments[i].length; j++)
result.push(arguments[i][j]);
}
return result;
};
apply = function (fn /*, arg, arg, ..., args */) {
var args = Array.slice(arguments, 1);
if (args.length > 0) {
var lastarg = args[args.length-1];
if (!is_list(lastarg))
throw new Error("not a spreadable argument designator: last element is not a list");
args.splice.apply(args, concatenate([args.length-1, 1], lastarg));
}
return fn.apply(global, args);
};
curry = function (fn /*, args... */) {
var args = Array.slice(arguments, 1);
return function () {
var args2 = Array.slice(arguments, 0);
return fn.apply(this, args.concat(args2));
};
};
rcurry = function (fn /*, args... */) {
var args = Array.slice(arguments, 1);
return function () {
var args2 = Array.slice(arguments, 0);
return fn.apply(this, args2.concat(args));
};
};
negate = function (fn) {
return function () {
return !fn.apply(this, arguments);
};
};
map = function (fn /*, lists... */) {
var lists = Array.slice(arguments, 1);
return lists.transpose().map(function (args) { return apply(fn, args); });
};
flatmap = function (fn /*, lists... */) {
return flatten(apply(map, Array.slice(arguments, 0)));
};
reduce = function (fn, list, base) {
return list.reduce(fn, base);
};
all = function (test /*, lists... */) {
var lists = Array.slice(arguments, 1);
return lists.transpose().every(function (args) {
return test.apply(this, args);
});
};
none = function (test /*, lists... */) {
arguments[0] = negate(arguments[0]);
return apply(all, arguments);
};
any = some = negate(none);
notall = negate(all);
each = function (fn /*, lists... */) {
apply(map, arguments);
};
find = function (obj, list, test) {
if (arguments.length < 3) test = identical;
return find_if(curry(test, obj), list);
};
find_if = function (predicate, list) {
var pos = position_if(predicate, list);
if (pos === null) return null;
else return list[pos];
};
find_if_not = function (predicate, list) {
return find_if(negate(predicate), list);
};
position = function (obj, list, test) {
if (arguments.length < 3) test = identical;
return position_if(curry(test, obj), list);
};
position_if = function (predicate, list) {
for (var i = 0; i < list.length; i++)
if (predicate(list[i]))
return i;
return null;
};
position_if_not = function (predicate, list) {
return position_if(negate(predicate), list);
};
contains = function (obj, list, test) {
if (arguments.length < 3) test = identical;
return contains_if(curry(test, obj), list);
};
contains_if = any;
contains_if_not = notall;
contains_all = function (objs, list, test) {
if (arguments.length < 3) test = identical;
return all(rcurry(contains, list, test), objs);
};
contains_one_to_one = function (objs, list, test) {
if (arguments.length < 3) test = identical;
if (list.length < objs.length)
return false;
list = copy_list(list);
for (var i = 0; i < objs.length; i++) {
var j = position(objs[i], list, test);
if (j === null)
return false;
else
deleet_at(j, list);
}
return true;
};
count = function (obj, list, test) {
if (arguments.length < 3) test = identical;
return count_if(curry(test, obj), list);
};
count_if = function (predicate, list) {
return reduce(function (count, elt) {
return predicate(elt) ? count + 1 : count;
}, list, 0);
};
count_if_not = function (predicate, list) {
return count_if(negate(predicate), list);
};
deleet = function (obj, list, test) {
if (arguments.length < 3) test = identical;
deleet_if(curry(test, obj), list);
};
deleet_if = function (predicate, list) {
for (var i = 0; i < list.length; i++) {
if (predicate(list[i])) {
deleet_at(i, list);
i--;
}
}
};
deleet_if_not = function (predicate, list) {
return deleet_if(negate(predicate), list);
};
deleet_one = function (obj, list, test) {
if (arguments.length < 3) test = identical;
var pos = position(obj, list, test);
if (pos === null) throw new Error("no such element");
else deleet_at(pos, list);
};
deleet_one_to_one = function (objs, list, test) {
if (arguments.length < 3) test = identical;
each(rcurry(deleet_one, list, test), objs);
};
deleet_at = function (pos, list) {
list.splice(pos, 1);
};
remove = function (obj, list, test) {
if (arguments.length < 3) test = identical;
return remove_if(curry(test, obj), list);
};
remove_if = function (predicate, list) {
return list.filter(negate(predicate));
};
remove_if_not = function (predicate, list) {
return list.filter(predicate);
};
remove_one = function (obj, list, test) {
if (arguments.length < 3) test = identical;
var pos = position(obj, list, test);
if (pos === null) throw new Error("no such element");
return remove_at(pos, list);
};
remove_one_to_one = function (objs, list, test) {
if (arguments.length < 3) test = identical;
list = copy_list(list);
deleet_one_to_one(objs, list, test);
return list;
};
remove_at = function (pos, list) {
list = copy_list(list);
deleet_at(pos, list);
return list;
};
is_empty = function (list) {
return list.length === 0;
};
copy_list = function (list) {
return Array.slice(list, 0);
};
reverse = function (list) {
return copy_list(list).reverse();
};
flatten = function (list) {
return apply(concatenate, list);
};
sort = function (list, predicate) {
list.sort(predicate);
return list;
};
shuffle = function (list) {
return sort(list, function () { return Math.random()*2-1; });
};
Array.prototype.indices = function () {
var result = [];
for (var i = 0; i < this.length; i++)
result.push(i);
return result;
};
Array.prototype.equals = function (that) {
if (identical(this, that)) return true;
if (!is_list(that) || this.length !== that.length) return false;
for (var i = 0; i < this.length; i++)
if (this[i] !== that[i])
return false;
return true;
};
Array.prototype.peek = function () {
return this[this.length-1];
};
Array.prototype.push_new = function (obj, test) {
if (arguments.length < 2) test = identical;
if (!contains(obj, this, test))
this.push(obj);
};
Array.prototype.push_all = function (objs) {
each(this.push.bind(this), objs);
};
Array.prototype.groups_of = function (length) {
var result = [];
for (var i = 0; i < this.length; i += length)
result.push(this.slice(i, i+length));
return result;
};
Array.prototype.pairs = function () {
return this.groups_of(2);
};
// transposes a two-dimensional m by n array, where n is the length of
// the shortest inner array. that is, the result is truncated:
// [[1,2,3,4,5,6,7],[1,2,3]].transpose() <=> [[1,1],[2,2],[3,3]]
Array.prototype.transpose = function () {
var result = [];
var shortest = Infinity;
for (var i = 0; i < this.length; i++) {
if (this[i].length < shortest)
shortest = this[i].length;
for (var j = 0; j < this[i].length; j++) {
result[j] = result[j] || [];
result[j][i] = this[i][j];
}
}
if (shortest < Infinity)
result.length = shortest;
return result;
};
Object.from_alist = function (alist) {
var obj = {};
alist.forEach(function (pair) {
// earlier items shadow later ones
if (!(pair[0] in obj))
obj[pair[0]] = pair[1];
});
return obj;
};
Object.from_plist = function (plist) {
return Object.from_alist(plist.pairs());
};
Function.prototype.bind = function (obj /*, args... */) {
var f = this;
var args = Array.slice(arguments, 1);
return function () {
var args2 = Array.slice(arguments, 0);
return f.apply(obj, args.concat(args2));
};
};
String.prototype.words = function () { return this.trim().split(/\s+/); };
String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ''); };
Number.prototype.elements = function () {
var result = [];
for (var i = 0; i < this; i++)
result[i] = i;
return result;
};
identical = function (a, b) { return a === b; };
identity = function (v) { return v; };
tree_equal = function (a, b, test) {
if (arguments.length < 3) test = identical;
if (a === b) return true;
if (is_list(a) && is_list(b)) {
if (a.length === b.length)
return all(rcurry(tree_equal, test), a, b);
else
return false;
} else {
return test(a, b);
}
};
set_product = function (/* lists... */) {
if (arguments.length === 0) return [[]];
var subproduct = apply(set_product, Array.slice(arguments, 1));
return flatmap(function (elt) {
return map(curry(prepend, elt), subproduct);
}, arguments[0]);
};
set_equal = function (a, b, test) {
if (arguments.length < 3) test = identical;
return subset(a, b, test) && subset(b, a, test);
};
subset = contains_all;
property_test = function (property, value, test) {
if (arguments.length < 3) test = identical;
return function (obj) { return test(obj[property], value); }
};
constantly = function (v) { return function () { return v; }; };
is_zero = function (quantity) {
return (is_number(quantity) && quantity === 0) || (is_vector(quantity) && all(is_zero, quantity));
};
is_number = function (quantity) {
return typeof(quantity) === "number";
};
is_vector = function (quantity) {
return is_list(quantity);
};
is_list = function (obj) {
return exists(obj) && exists(obj.length) && !is_string(obj);
};
is_string = function (obj) {
return exists(obj) && obj.constructor === String;
};
exists = function (obj) {
return obj !== null && typeof obj !== "undefined";
};
vectorize = function (combinator, base) {
return function (quantities) {
quantities = Array.slice(arguments, 0);
return quantities.reduce(function (result, quantity) {
if (is_number(result) && is_number(quantity)) return combinator(result, quantity);
if (is_number(result) && is_vector(quantity)) return map(curry(combinator, result), quantity);
if (is_vector(result) && is_number(quantity)) return map(rcurry(combinator, quantity), result);
if (is_vector(result) && is_vector(quantity)) return map(combinator, result, quantity);
}, base);
};
};
// elementwise vector arithmetic
times = vectorize(function (a, b) { return a * b; }, 1);
plus = vectorize(function (a, b) { return a + b; }, 0);
sum = function (v) { return reduce(plus, v); }
product = function (v) { return reduce(times, v); }
is_integer = function (n) { return is_number(n) && Math.floor(n) === n; };
is_positive = function (n) {
if (!is_number(n)) throw new Error("is_positive called with non-number argument");
return n > 0;
};
is_negative = function (n) {
if (!is_number(n)) throw new Error("is_positive called with non-number argument");
return n < 0;
};
is_nonpositive = negate(is_positive);
is_nonnegative = negate(is_negative);
is_positive_integer = function (n) { return is_integer(n) && is_positive(n); };
is_negative_integer = function (n) { return is_integer(n) && is_negative(n); };
is_nonpositive_integer = function (n) { return is_integer(n) && is_nonpositive(n); };
is_nonnegative_integer = function (n) { return is_integer(n) && is_nonnegative(n); };
random_position_within = function (bounds) {
return map(random_nonnegative_integer_below, bounds);
};
random_nonnegative_integer_below = function (ceiling) {
return random_integer_between(0, ceiling);
};
random_integer_between = function (floor, ceiling) {
return Math.floor(floor + Math.random() * (ceiling - floor));
};
postpone = function (fn) { setTimeout(fn, 0); };