forked from debrouwere/Extendables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminified.jsx
2411 lines (2038 loc) · 66 KB
/
minified.jsx
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// we need to log somethings before the log module is loaded,
// so we buffer these messages
var log_buffer = [];
/*
* Patches for functional programming.
* Inspired by and sometimes copied from underscore.js
*/
/**
* @desc Merge two objects together. This modifies the original object.
* First use :func:`Object#clone` on the object if you want to keep the original object intact.
*
* @param {Object} obj The object to merge into this one.
*
* @returns {Object} Returns the merged object (``this``);
*/
Object.prototype.merge = function (obj) {
if (!obj) return;
var merged_obj = this;
for (var name in obj) {
merged_obj[name] = obj[name];
}
return merged_obj;
}
/**
* @function
* @desc An alias for :func:`Object#merge`
*/
Object.prototype.extend = Object.prototype.merge
/**
* @desc Creates and returns a clone of the object.
*/
Object.prototype.clone = function () {
// speeds things up if we're cloning an array
if (this instanceof Array) return this.slice(0);
if (this instanceof String) return this.substring(0);
// the normal route for any other object
// though it might not work on some built-in
// application-specific objects
return new this.constructor().merge(this);
}
/**
* @desc
* Returns only the keys (also known as 'names') of an object or associative array.
* Will filter out any functions, as these are presumed to be object methods.
* @returns {Array} An array with all the keys.
*/
Object.prototype.keys = function () {
var keys = [];
for (var key in this) {
if (this.hasOwnProperty(key) && !(this[key] instanceof Function)) keys.push(key);
}
return keys;
}
/**
* @desc Returns only the values of an object or associative array.
* @returns {Array} An array with all the values.
*
* @example
* > var nation = {'name': 'Belgium', 'continent': 'Europe'}
* > nation.values();
* ['Belgium', 'Europe']
*/
Object.prototype.values = function () {
var self = this;
return this.keys().map(function (key) {
return self[key];
});
}
/**
* @desc An alias for ``this instanceof type``.
* @returns {Bool} True or false.
*
* @example
* > [].is(Array);
* true
*/
Object.prototype.is = function(type) {
return this instanceof type;
}
/**
* @desc Checks whether the object has a value for the specified property.
* @returns {Bool} True or false.
*/
Object.prototype.has = function (key) {
// could be just null or an invalid object
// either way, has() should return false
if (this == null || this[key] == null) return false;
if (key in this) {
return new Boolean(this[key]) != false;
} else {
return false;
}
}
/**
* @desc Alias for ``obj.hasOwnProperty``
* @returns {Bool} True or false.
*/
Object.prototype.has_own = function (key) {
return this.hasOwnProperty(key);
}
/**
* @desc A debugging utility. When used without the ``dump`` argument,
* equivalent to ``$.writeln(obj.toString())``.
* @param {Bool} [dump=false]
* Dump all properties of this object;
* otherwise just returns a string representation.
*/
Object.prototype.to_console = function (dump) {
if (dump) {
var obj = this;
var out = obj.reflect.properties.map(function (property) {
return property.name + "\t => " + obj[property.name];
}).join("\n");
} else {
var out = this.toString();
}
return $.writeln(out);
}
/**
* @desc This is a simple string formatting method, loosely inspired on the one in Python 3.
*
* * In unnamed mode, specify placeholders with the **{}** symbol.
* * In named mode, specify placeholders with **{propname}**.
*
* @param {String} replacements
* For each **{}** symbol in the text, ``format`` expects a replacement argument.
* Calls `.toString()` on each replacement, so you can pass in any data type.
* You may also specify a single replacement object, which will do named formatting.
*
* @example
* > var person = {'salutation': 'mister', 'name': 'John Smith'};
* > var hello = "Hello there, {}, I've heard your name is {}!".format(person.salutation, person.name);
* > $.writeln(hello);
* "Hello there, mister, I've heard your name is John Smith"
*
* @example
* > var person = {'salutation': 'mister', 'name': 'John Smith'};
* > var hello = "Hello there, {salutation}, I've heard your name is {name}!".format(person);
* > $.writeln(hello);
* "Hello there, mister, I've heard your name is John Smith"
*/
String.prototype.format = function() {
var str = this;
var replacements = arguments.to('array');
var named = replacements.length == 1 && replacements[0].reflect.name == 'Object';
if (named) {
var dict = replacements[0];
dict.keys().forEach(function (key) {
// replace globally (flagged g)
str = str.replace("{" + key + "}", dict[key], "g");
});
return str;
} else {
// split the string into parts around the substring replacement symbols ({}).
var chunks = str.split("{}");
// fill in the replacements
for (var i in chunks) {
var replacement = replacements.shift();
if (replacement) chunks[i] += replacement.toString();
}
// join everything together
return chunks.join('');
}
}
/**
* @desc Tests whether the string starts with the specified substring.
* @param {String} substring
* @returns {Bool} True or false.
*/
String.prototype.startswith = function (substring) {
return new Boolean(this.length && this.indexOf(substring) === 0).valueOf();
}
/**
* @desc Tests whether the string ends with the specified substring.
* @param {String} substring
* @returns {Bool} True or false.
*/
String.prototype.endswith = function (substring) {
return new Boolean(this.length && this.indexOf(substring) == (this.length - substring.length)).valueOf();
}
/**
* @desc Tests whether the string contains the specified substring.
* This is equal to ``str.indexOf(substring) != -1``.
* @param {String} substring
* @returns {Bool} True or false.
*/
String.prototype.contains = function (substring) {
return this.indexOf(substring) != -1;
}
/**
* @desc Does what it says.
* Does not check whether the string actually extends beyond the the substring.
*/
String.prototype.indexAfter = function (substring) {
var index = this.indexOf(substring);
if (index == -1) {
return index;
} else {
return index + substring.length;
}
}
/**
* @desc Removes leading whitespace characters, including tabs, line endings and the like.
* @param {String} [character] if specified, removes leading characters matching the parameter
* instead of whitespace.
*
* @example
* > $.writeln(" hello there ".trim());
* "hello there "
*/
String.prototype.ltrim = function(character) {
if (character) {
if (this.endswith(character) == true) {
return this.substr(1).ltrim(character);
} else {
return this;
}
} else {
return this.replace(/^\s+/, "");
}
}
/**
* @desc Removes trailing whitespace characters, including tabs, line endings and the like.
* @param {String} [character] if specified, removes trailing characters matching the parameter
* instead of whitespace.
*
* @example
* > $.writeln(" hello there ".trim());
* " hello there"
*/
String.prototype.rtrim = function (character) {
if (character) {
if (this.endswith(character) == true) {
return this.slice(0, -1).rtrim(character);
} else {
return this;
}
} else {
return this.replace(/\s+$/, "");
}
}
/**
* @desc Removes leading and trailing whitespace characters, including tabs, line endings and the like.
* @param {String} [character] if specified, removes leading and trailing characters matching the
* parameter instead of whitespace.
*
* @example
* > $.writeln(" hello there ".trim());
* "hello there"
*/
String.prototype.trim = function(character) {
if (character) {
return this.ltrim(character).rtrim(character);
} else {
return this.replace(/^\s+|\s+$/g, "");
}
}
// note: the Mozilla stuff is MIT licensed!
/* Javascript 1.6 Array extras, courtesy of Mozilla */
/**
* @desc Returns the first index at which a given element can be found in the array, or -1 if it is not present.
*
* @param {Object} element
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
*/
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
/**
* @desc Does what it says.
* Does not check whether there is actually a next element, that's up to you.
*/
Array.prototype.indexAfter = function (element) {
var index = this.indexOf(element);
if (index == -1) {
return index;
} else {
return index + 1;
}
}
/**
* @desc Returns the last index at which a given element can be found in the array,
* or -1 if it is not present. The array is searched backwards, starting at from_index.
*
* @param {Object} element
* @param {Number} from_index
*
* @see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/lastIndexOf
*/
Array.prototype.lastIndexOf = function(elt /*, from*/)
{
var len = this.length;
var from = Number(arguments[1]);
if (isNaN(from))
{
from = len - 1;
}
else
{
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
else if (from >= len)
from = len - 1;
}
for (; from > -1; from--)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
/**
* @desc Tests whether all elements in the array pass the test implemented by the provided function.
*
* @param {Function} function
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
*/
Array.prototype.every = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this &&
!fun.call(thisp, this[i], i, this))
return false;
}
return true;
};
/**
* @desc Creates a new array with all elements that pass the test implemented by the provided function.
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
*/
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
/**
* @desc Executes a provided function once per array element.
*
* @param {Function} function
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
*/
Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
/**
* @desc Creates a new array with the results of calling a provided function on every element in this array.
*
* @param {Function} function
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
*/
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
/**
* @desc Tests whether some element in the array passes the test implemented by the provided function.
*
* @param {Function} function
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
*/
Array.prototype.some = function(fun /*, thisp*/)
{
var i = 0,
len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (; i < len; i++)
{
if (i in this &&
fun.call(thisp, this[i], i, this))
return true;
}
return false;
};
/* Javascript 1.8 Array extras, courtesy of Mozilla */
/**
* @desc Apply a function against an accumulator and
* each value of the array (from left-to-right) as to reduce it to a single value.
*
* @param {Function} function
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
*/
Array.prototype.reduce = function(fun /*, initial*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
// no value to return if no initial value and an empty array
if (len == 0 && arguments.length == 1)
throw new TypeError();
var i = 0;
if (arguments.length >= 2)
{
var rv = arguments[1];
}
else
{
do
{
if (i in this)
{
var rv = this[i++];
break;
}
// if array contains no values, no initial value to return
if (++i >= len)
throw new TypeError();
}
while (true);
}
for (; i < len; i++)
{
if (i in this)
rv = fun.call(undefined, rv, this[i], i, this);
}
return rv;
};
/**
* @desc Apply a function simultaneously against two values of the array (from right-to-left)
* as to reduce it to a single value.
*
* @param {Function} function
*
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/ReduceRight
*/
Array.prototype.reduceRight = function(fun /*, initial*/)
{
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
// no value to return if no initial value, empty array
if (len == 0 && arguments.length == 1)
throw new TypeError();
var i = len - 1;
if (arguments.length >= 2)
{
var rv = arguments[1];
}
else
{
do
{
if (i in this)
{
var rv = this[i--];
break;
}
// if array contains no values, no initial value to return
if (--i < 0)
throw new TypeError();
}
while (true);
}
for (; i >= 0; i--)
{
if (i in this)
rv = fun.call(undefined, rv, this[i], i, this);
}
return rv;
};
/**
* @desc Allows you to quickly pluck a single attribute from an array of objects.
*
* @example
* > var people = [{'name': 'Alfred', age: 33}, {'name': 'Zed', age: 45}];
* > people.pluck('age');
* [33,45]
* > people.pluck('age').sum();
* 78
* > people.sum('age');
* 78
* > people.sum(function (person) { return person.age });
* 78
*/
Array.prototype.pluck = function (name) {
return this.map(function (item) {
return item[name];
});
}
/**
* @desc Returns the maximum value in an array.
*
* @param {Function|String} [salient_feature] ``min`` can also order objects
* if you provide a salient feature for it to work on, either a function
* or the name of an object property
*
* @example
* > var people = [{'name': 'Alfred'}, {'name': 'Zed'}];
* > people.max(function (obj) {
* ... return obj.name;
* ... });
* {'name': 'Zed'}
*/
Array.prototype.max = function (salient) {
if (salient && salient.is(String)) {
var mapper = function (obj) { return obj[salient]; }
} else {
var mapper = salient || function (obj) { return obj; }
}
function fn (a, b) {
return mapper(a) > mapper(b);
}
var array = this.clone();
array.sort(fn);
return array.pop();
};
/**
* @returns The minimum value in an array. Works like :func:`Array#max`
*
* @param {Function|String} [salient_feature] See ``max``.
*/
Array.prototype.min = function (salient) {
if (salient && salient.is(String)) {
var mapper = function (obj) { return obj[salient]; }
} else {
var mapper = salient || function (obj) { return obj; }
}
function fn (a, b) {
return mapper(a) > mapper(b);
}
var array = this.clone();
array.sort(fn);
return array.shift();
}
/**
* @returns The sum of all array values.
*
* @param {Function|String} [salient_feature] See ``max``.
*
* @example
* > var persons = [
* ... {'name': 'Abraham', 'children': 5},
* ... {'name': 'Joe', 'children': 3},
* ... {'name': 'Zed', 'children': 0}
* ... ];
* > persons.sum('children');
* 8
*/
Array.prototype.sum = function (salient) {
if (salient && salient.is(String)) {
var mapper = function (obj) { return obj[salient]; }
} else {
var mapper = salient || function (obj) { return obj; }
}
var features = this.map(mapper);
return features.reduce(function (a, b) { return a + b; });
}
/**
* @desc Alias for :func:`Array#filter`
* @function
*/
Array.prototype.select = Array.prototype.filter
/**
* @desc Does exactly the inverse of :func:`Array#filter` and its alias :func:`Array#select`.
*
* @param {Function} fn
*/
Array.prototype.reject = function (fn) {
return this.select(function (value) {
return !fn(value);
});
}
/**
* @desc Flattens nested arrays.
*
* @example
* > var list = [[1, 2, [3, 4, 5], 6], [7, 8], 9];
* > list.flatten();
* [1,2,3,4,5,6,7,8,9];
*/
Array.prototype.flatten = function () {
return this.reduce(function(memo, value) {
if (value instanceof Array) return memo.concat(value.flatten());
memo.push(value);
return memo;
}, []);
};
/**
* @desc Returns a copy of the array with all falsy values removed.
* This includes ``false``, ``null``, ``0``, ``""``, ``undefined`` and ``NaN``.
*/
Array.prototype.compact = function () {
return this.reject(function (value) {
return new Boolean(value) == false;
});
}
/**
* @desc Returns the first item of this array
*/
Array.prototype.first = function () {
return this[0];
}
/**
* @desc Returns the last item of this array
*/
Array.prototype.last = function () {
return this.slice(-1)[0];
}
/**
* @desc Similar to indexOf
*/
Array.prototype.contains = function (obj) {
return this.indexOf(obj) != -1;
}
var exports = {};
var base64 = exports;
exports.encode64 = encoder('+/');
exports.decode64 = decoder('+/');
exports.urlsafeEncode64 = encoder('-_');
exports.urlsafeDecode64 = decoder('-_');
// base64.js - Base64 encoding and decoding functions
//
// Copyright (c) 2007, David Lindquist <david.lindquist@gmail.com>
// Released under the MIT license
//
// Modified by TJ Holowaychuk for CommonJS module support.
// Modified by Ben Weaver to use any alphabet.
// Modified by Stijn Debrouwere for ExtendScript support.
function encoder(extra) {
var chars = alphabet(extra);
return function(str) {
str = str.toString();
var encoded = [];
var c = 0;
while (c < str.length) {
var b0 = str.charCodeAt(c++);
var b1 = str.charCodeAt(c++);
var b2 = str.charCodeAt(c++);
var buf = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0);
var i0 = (buf & (63 << 18)) >> 18;
var i1 = (buf & (63 << 12)) >> 12;
var i2 = isNaN(b1) ? 64 : (buf & (63 << 6)) >> 6;
var i3 = isNaN(b2) ? 64 : (buf & 63);
encoded[encoded.length] = chars.charAt(i0);
encoded[encoded.length] = chars.charAt(i1);
encoded[encoded.length] = chars.charAt(i2);
encoded[encoded.length] = chars.charAt(i3);
}
return encoded.join('');
};
}
function decoder(extra) {
var chars = alphabet(extra),
invalid_char = new RegExp('[^' + regexp_escape(chars) + ']');
return function(str) {
var invalid = {
strlen: (str.length % 4 != 0),
chars: invalid_char.test(str),
equals: (new RegExp("/=/").test(str) && (new RegExp("/=[^=]/").test(str) || new RegExp("/={3}/").test(str)))
};
if (invalid.strlen || invalid.chars || invalid.equals)
throw new Error('Invalid base64 data');
var decoded = [];
var c = 0;
while (c < str.length) {
var i0 = chars.indexOf(str.charAt(c++));
var i1 = chars.indexOf(str.charAt(c++));
var i2 = chars.indexOf(str.charAt(c++));
var i3 = chars.indexOf(str.charAt(c++));
var buf = (i0 << 18) + (i1 << 12) + ((i2 & 63) << 6) + (i3 & 63);
var b0 = (buf & (255 << 16)) >> 16;
var b1 = (i2 == 64) ? -1 : (buf & (255 << 8)) >> 8;
var b2 = (i3 == 64) ? -1 : (buf & 255);
decoded[decoded.length] = String.fromCharCode(b0);
if (b1 >= 0) decoded[decoded.length] = String.fromCharCode(b1);
if (b2 >= 0) decoded[decoded.length] = String.fromCharCode(b2);
}
return decoded.join('');
};
}
/// --- Aux
function alphabet(extra) {
return 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
+ extra
+ '=';
}
function regexp_escape(expr) {
return expr.replace(/([\^\$\/\.\*\-\+\?\|\(\)\[\]\{\}\\])/, '\\$1');
}
/*
http://www.JSON.org/json2.js
2011-10-19
Public Domain.
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
See http://www.JSON.org/js.html
This code should be minified before deployment.
See http://javascript.crockford.com/jsmin.html
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
NOT CONTROL.
This file creates a global JSON object containing two methods: stringify
and parse.
JSON.stringify(value, replacer, space)
value any JavaScript value, usually an object or array.
replacer an optional parameter that determines how object
values are stringified for objects. It can be a
function or an array of strings.
space an optional parameter that specifies the indentation
of nested structures. If it is omitted, the text will
be packed without extra whitespace. If it is a number,
it will specify the number of spaces to indent at each
level. If it is a string (such as '\t' or ' '),
it contains the characters used to indent at each level.
This method produces a JSON text from a JavaScript value.
When an object value is found, if the object contains a toJSON
method, its toJSON method will be called and the result will be
stringified. A toJSON method does not serialize: it returns the
value represented by the name/value pair that should be serialized,
or undefined if nothing should be serialized. The toJSON method
will be passed the key associated with the value, and this will be
bound to the value
For example, this would serialize Dates as ISO strings.
Date.prototype.toJSON = function (key) {
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
return this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z';
};
You can provide an optional replacer method. It will be passed the
key and value of each member, with this bound to the containing
object. The value that is returned from your method will be
serialized. If your method returns undefined, then the member will
be excluded from the serialization.
If the replacer parameter is an array of strings, then it will be
used to select the members to be serialized. It filters the results
such that only members with keys listed in the replacer array are
stringified.
Values that do not have JSON representations, such as undefined or
functions, will not be serialized. Such values in objects will be
dropped; in arrays they will be replaced with null. You can use
a replacer function to replace those with JSON values.
JSON.stringify(undefined) returns undefined.
The optional space parameter produces a stringification of the
value that is filled with line breaks and indentation to make it
easier to read.
If the space parameter is a non-empty string, then that string will
be used for indentation. If the space parameter is a number, then
the indentation will be that many spaces.
Example:
text = JSON.stringify(['e', {pluribus: 'unum'}]);
// text is '["e",{"pluribus":"unum"}]'
text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
text = JSON.stringify([new Date()], function (key, value) {
return this[key] instanceof Date ?
'Date(' + this[key] + ')' : value;
});
// text is '["Date(---current time---)"]'
JSON.parse(text, reviver)
This method parses a JSON text to produce an object or array.
It can throw a SyntaxError exception.
The optional reviver parameter is a function that can filter and
transform the results. It receives each of the keys and values,
and its return value is used instead of the original value.
If it returns what it received, then the structure is not modified.
If it returns undefined then the member is deleted.
Example:
// Parse the text. Values that look like ISO date strings will
// be converted to Date objects.
myData = JSON.parse(text, function (key, value) {
var a;
if (typeof value === 'string') {
a =
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
if (a) {
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
+a[5], +a[6]));
}
}
return value;
});
myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
var d;
if (typeof value === 'string' &&
value.slice(0, 5) === 'Date(' &&
value.slice(-1) === ')') {
d = new Date(value.slice(5, -1));
if (d) {
return d;
}
}
return value;
});