-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrjs.1.4.js
630 lines (561 loc) · 16 KB
/
rjs.1.4.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/**
* Raine's Javascript Extensions
* A library of basic Javascript functions that nobody should be without.
*/
/***********************************
* String overrides
***********************************/
/** Replaces occurrences of {0}, {1}, ... with each additional argument passed. Like sprintf.
DEPRECATED: Use supplant instead.
*/
String.prototype.format = (function() {
// private vars
var preRE = [];
return function() {
var str = this;
for(var i=0, l=arguments.length; i<l; i++) {
// cache regular expression
if(!preRE[i]) {
preRE[i] = new RegExp();
preRE[i].compile('\\{' + (i) + '\\}','gm');
}
str = str.replace(preRE[i], arguments[i]);
}
return str;
}
})();
/** Performs variable substitution on the string, replacing items in {curly braces}.
@author Douglas Crockford http://javascript.crockford.com/remedial.html
*/
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
/** Removes whitespace from both ends of a string.
@author Douglas Crockford http://javascript.crockford.com/remedial.html
*/
String.prototype.trim = function () {
return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1");
};
/** Returns true if the string starts with the given substring. */
String.prototype.startsWith = function(str){
return (this.indexOf(str) === 0);
};
/** Encodes angled brackets and ampersands as HTML entities.
@author Douglas Crockford http://javascript.crockford.com/remedial.html
*/
String.prototype.entityify = function () {
return this
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
};
/** Produces a quoted string.
@author Douglas Crockford http://javascript.crockford.com/remedial.html
*/
String.prototype.quote = function () {
var c, i, l = this.length, o = '"';
for (i = 0; i < l; i += 1) {
c = this.charAt(i);
if (c >= ' ') {
if (c === '\\' || c === '"') {
o += '\\';
}
o += c;
} else {
switch (c) {
case '\b':
o += '\\b';
break;
case '\f':
o += '\\f';
break;
case '\n':
o += '\\n';
break;
case '\r':
o += '\\r';
break;
case '\t':
o += '\\t';
break;
default:
c = c.charCodeAt();
o += '\\u00' + Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}
}
}
return o + '"';
};
if (!String.prototype.supplant) {
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
}
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, "$1");
};
}
/***********************************
* Function overrides
***********************************/
/**
* Returns a function that applies the underlying function
* to the result of the application of `fn`.
*
* Based on Functional library by Oliver Steele
* http://osteele.com/javascripts/functional
*/
Function.prototype.compose = function(fn) {
var self = this;
return function() {
return self.apply(this, [fn.apply(this, arguments)]);
}
};
/**
* Returns a function that applies the underlying function
* to the result of the application of `fn`.
*/
Function.prototype.sequence = function(fn) {
var self = this;
return function() {
return fn.apply(this, [self.apply(this, arguments)]);
}
};
/**
* Returns a function that, applied to an argument list $arg2$,
* applies the underlying function to $args ++ arg2$.
* :: (a... b... -> c) a... -> (b... -> c)
* == f.curry(args1...)(args2...) == f(args1..., args2...)
*/
Function.prototype.curry = function(/*args...*/) {
var fn = this;
var args = Array.slice(arguments, 0);
return function() {
return fn.apply(this, args.concat(Array.slice(arguments, 0)));
};
};
/*
* Right curry. Returns a function that, applied to an argument list $args2$,
* applies the underlying function to $args2 + args$.
* == f.curry(args1...)(args2...) == f(args2..., args1...)
* :: (a... b... -> c) b... -> (a... -> c)
*/
Function.prototype.rcurry = function(/*args...*/) {
var fn = this;
var args = Array.slice(arguments, 0);
return function() {
return fn.apply(this, Array.slice(arguments, 0).concat(args));
};
};
/** Calls the function in the scope of the given object. */
Function.prototype.Context = function(obj) {
var fnReference = this;
return function () {
return typeof fnReference == "function" ? fnReference.apply(obj, arguments) : obj[fnReference].apply(obj, arguments);
};
};
/***********************************
* Functional
***********************************/
/** Returns a new array consisting of f applied to each item of the given array.
@param collection An array or object of items.
@param f If collection is an array, (item index) => newValue.
If collection is an object, (key value index) => [newKey, newValue] or (key value index) => newValue.
*/
var map = function(collection, f, context) {
// error handling
if(!collection) {
throw new Error("Array is null or undefined.");
}
else if(!f || !f.apply) {
throw new Error("You must provide a valid function as the second argument to map: " + f);
}
// array version
if(collection.length || collection.length === 0) {
var newArray = [];
for(var i=0, n=collection.length; i<n; i++) {
newArray[i] = f.apply(context, [collection[i], i]);
}
return newArray;
}
// object version
else {
var newObj = {};
var i=0;
for(var prop in collection) {
var result = f.apply(context, [prop, collection[prop], i])
if(result && result.key && result.value) {
newObj[result.key] = result.value;
}
else {
newObj[prop] = result;
}
i++;
}
return newObj;
}
};
Array.prototype.map = function(f, context) {
return map(this, f, context);
};
/** Returns a new array consisting of a subset of arr for which the given function returns truthy. */
var filter = function(arr, f, context) {
var newArray = [];
for(var i=0, n=arr.length; i<n; i++) {
if(f.apply(context, [arr[i], i])) {
newArray.push(arr[i]);
}
}
return newArray;
};
Array.prototype.filter = function(f, context) {
return filter(this, f, context);
};
/**
* Applies `fn` to `init` and the first element of `sequence`,
* and then to the result and the second element, and so on.
* == reduce(f, init, [x0, x1, x2]) == f(f(f(init, x0), x1), x2)
* :: (a b -> a) a [b] -> a
* >> reduce('x y -> 2*x+y', 0, [1,0,1,0]) -> 10
* (from Oliver Steele's Functional Javascript library)
*/
var reduce = function(fn, init, sequence, context) {
//fn = Function.toFunction(fn);
sequence = [].concat(sequence);
var len = sequence.length,
result = init;
for (var i = 0; i < len; i++)
result = fn.apply(context, [result, sequence[i]]);
return result;
};
/** Returns a list of integers from min (default: 0) to max (inclusive). */
var range = function(min, max) {
// override for 1 argument
if(arguments.length == 1) {
max = min - 1;
min = 0;
}
var arr = [];
for(var i=min; i<=max; i++) {
arr.push(i);
}
return arr;
};
/** Applies the given function to each item in the array. Same as map but doesn't build and return an array. */
var each = function(arr, f, context) {
for(var i=0, n=arr.length; i<n; i++) {
f.apply(context, [arr[i], i]);
}
};
Array.prototype.each = function(f, context) {
return each(this, f, context);
};
/** Returns the first item in arr for which the function f returns true. Returns null if no matches are found. */
var find = function(arr, f, context) {
for(var i=0, n=arr.length; i<n; i++) {
if(f.apply(context, [arr[i], i])) {
return arr[i];
}
}
return null;
};
Array.prototype.find = function(f, context) {
return find(this, f, context);
};
/** Returns true if the array contains the given item (compared by address, but works by value for strings). */
var contains = function(arr, item) {
for(var i=0, n=arr.length; i<n; i++) {
if(arr[0] === item) {
return true;
}
}
return false;
};
Array.prototype.contains = function(item) {
return contains(this, item);
};
/** Returns an array of values of the given property for each item in the array. */
var pluck = function(arr, property) {
return map(arr, function(item) {
return item[property];
});
};
Array.prototype.pluck = function(property) {
return pluck(this, property);
};
/** Group the array of objects by one of the object's properties. Returns a dictionary containing the original arrays items indexed by the property value. */
var group = function(arr, propOrFunc) {
if(!propOrFunc) {
throw new Error("You must specific a property name or mappable function.");
}
var dict = {};
each(arr, function(item) {
if(!(item[propOrFunc] in dict)) {
dict[item[propOrFunc]] = [];
}
dict[item[propOrFunc]].push(item);
});
return dict;
}
Array.prototype.group = function(propOrFunc) {
return group(this, propOrFunc);
};
/***********************************
* Array/Object/Utility
***********************************/
/** Returns an array of the object's keys. */
var keys = function(obj) {
var newArray = [];
for(var property in obj) {
newArray.push(property);
}
return newArray;
};
/** Returns an array of the object's values. */
var values = function(obj) {
var newArray = [];
for(var property in obj) {
newArray.push(obj[property]);
}
return newArray;
};
/** Converts an array to a dictionary given a function that takes an array value and returns a 2-part array of the key and value. */
var toDict = function(arr, makeKeyValue) {
var dict = {};
arr.each(function(item) {
kvp = makeKeyValue(item);
dict[kvp[0]] = kvp[1];
});
return dict;
};
Array.prototype.toDict = function(makeKeyValues) { return toDict(this, makeKeyValues); };
/** Returns a dictionary whose keys are the values of the array and values are the number of instances of that value within the array. */
var tally = function(arr) {
var dict = {};
arr.each(function(value) {
var count = dict[value] || 0;
dict[value] = count + 1;
});
return dict;
};
Array.prototype.tally = function() { return tally(this); };
/** Returns the unique values in the array. */
var unique = function(arr) {
return keys(tally(arr));
};
Array.prototype.unique = function() { return unique(this); };
/** Join the object into a single string with the given separators separating properties from each other as well as values. */
var joinObj = function(obj, propSeparator, valueSeparator) {
var keyValuePairs = [];
for(var prop in obj) {
keyValuePairs.push(prop + valueSeparator + obj[prop]);
}
return keyValuePairs.join(propSeparator);
};
/** Returns true if the object has no non-undefined properties.
@author Douglas Crockford http://javascript.crockford.com/remedial.html
*/
var isEmpty = function(o) {
var i, v;
if (typeOf(o) === 'object') {
for (i in o) {
v = o[i];
if (v !== undefined && typeOf(v) !== 'function') {
return false;
}
}
}
return true;
}
/** Compares two items lexigraphically. Returns 1 if a>b, 0 if a==b, or -1 if a<b. */
var compare = function(a,b) {
return a > b ? 1 :
a == b ? 0 :
-1;
};
/** Returns a function that compares the given property of two items. */
var compareProperty = function(property) {
return function(a,b) {
return compare(a[property], b[property]);
};
};
/** Returns a compare function that can be passed to Array.sort to sort in the order of the given array of properties.
* A property can also be appended with " ASC" or " DESC" to control the sort order.
* */
var dynamicCompare = function(props) {
return function(a,b) {
for(var i=0; i<props.length; i++) {
var aVal, bVal, sortDir;
if(typeof props[i] == "function") {
aVal = props[i](a);
bVal = props[i](b);
sortDir = "asc";
}
// TODO: find a way to support directional sorting without breaking if the property has a space in it
else if(props[i].toLowerCase().indexOf(" ") >= 0) {
var splitVal = props[i].split(" ");
aVal = a[splitVal[0]];
bVal = b[splitVal[0]];
sortDir = splitVal[1].toLowerCase();
}
else {
aVal = a[props[i]];
bVal = b[props[i]];
sortDir = "asc";
}
if(aVal != bVal) {
return sortDir == "asc" ?
aVal > bVal ? 1 : -1 :
aVal < bVal ? -1 : 1;
}
}
return false;
};
};
/** Returns true if all the items in a are equal to all the items in b, recursively. */
var equals = function(a,b) {
// compare arrays
if(a instanceof Array) {
// check if the arrays are the same length
if(a.length !== b.length) {
return false;
}
// check the equality of each item
for(var i=0, l=a.length; i<l; i++) {
if(!b || !b[i] || !equals(a[i], b[i])) {
return false;
}
}
}
// compare objects
if(numProperties(a) > 0 && numProperties(a) == numProperties(b)) {
for(property in a) {
if(!(property in b && b[property] == a[property])) {
return false;
}
}
}
// compare scalars
else {
if(a !== b) {
return false;
}
}
return true;
};
var numProperties = function(o) {
var n = 0;
for(property in o) {
n++;
}
return n;
};
/** Returns true if the given value is not undefined, null, or an empty string. */
var hasValue = function(x) {
return x !== undefined && x !== null && x !== "";
};
/** Returns a new object with the given objects merged onto it. Non-undefined properties on later arguments override identical properties on earlier arguments. */
var merge = function(/*arguments*/) {
var mothership = {};
// iterate through each given object
for(var i=0; i<arguments.length; i++) {
var outlier = arguments[i];
// add each property to the mothership
for(prop in outlier) {
if(typeOf(outlier[prop]) == "object") {
mothership[prop] = merge(mothership[prop], outlier[prop]); // RECURSION
}
else if(outlier[prop] !== undefined) {
mothership[prop] = outlier[prop];
}
}
}
return mothership;
};
/** Returns a function that returns the inverse of the given boolean function. */
var not = function(f) {
return function() {
return !f.apply(this, arguments);
}
};
/** Returns a string representation of the given scalar, array, or dictionary. */
var hash = function(o) {
if(o === undefined) {
return "undefined";
}
else if(o === null) {
return "null";
}
else if(typeof(o) === "string" || typeof(o) === "number") {
return "" + o;
}
else if(typeOf(o) === "array") {
return "_[{0}]_".format(o.map(hash).join(","));
}
else if(typeOf(o) === "object") {
var objString = "";
for(prop in o) {
objString += "{0}_:_{1}".format(prop, hash(o[prop]));
}
return "_{{0}}_".format(objString);
}
else {
throw new Error("Unhashable value: " + o);
}
};
/** Generates a pseudo-random string that can be assumed to be unique. */
var guid = (function() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
return function() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
})();
/** Returns a string representing the type of the object, with special handling for null and arrays.
@author Douglas Crockford http://javascript.crockford.com/remedial.html
*/
var typeOf = function(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length')) &&
typeof value.splice === 'function') {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
};
/** Identity function. */
var I = function(x) { return x; }
/***********************************
* Assertions
***********************************/
/** Asserts that a is truthy. */
var assert = function(a) {
if(!a) {
console.error("Assertion failure: {0}".format(a));
}
};
/** Asserts that a is equal to b, using recursive equality checking for arrays. */
var assertEquals = function(a,b) {
if(!equals(a,b)) {
console.error("Assertion failure: {0} === {1}".format(a,b));
}
};