-
Notifications
You must be signed in to change notification settings - Fork 0
/
base2-jsb-fp.js
6786 lines (5853 loc) · 208 KB
/
base2-jsb-fp.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
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
/*
base2 - Copyright 2007-2010, Dean Edwards
http://code.google.com/p/base2/
License:
http://www.opensource.org/licenses/mit-license.php
Contributors:
Doeke Zanstra
*/
// timestamp: Mon, 11 Jan 2010 15:44:59
var base2 = {
name: "base2",
version: "2.0 (alpha1)"
};
(function(global) { // begin: package
// =========================================================================
// base2/header.js
// =========================================================================
/*@cc_on @*/
var undefined,
base2 = global.base2,
document = global.document,
Undefined = K(),
Null = K(null),
True = K(true),
False = K(false),
This = function(){return this};
base2.namespace = "";
var _Object_prototype = Object.prototype,
_Function_prototype = Function.prototype,
_Array_prototype = Array.prototype,
_concat = _Array_prototype.concat,
_slice = _Array_prototype.slice,
_toString = _Object_prototype.toString,
_Object_toString = _toString.call({}),
_Array_toString = _toString.call([]),
_Date_toString = _toString.call(new Date),
_RegExp_toString = _toString.call(/./);
var _PRIMITIVE_TYPE = /boolean|number|string/,
_FORMAT = /%([1-9])/g,
_LTRIM = /^\s\s*/,
_RTRIM = /\s\s*$/,
_DECOMPILATION = /try/.test(detect), // some platforms don't allow decompilation
_TEST_TRUE = {test: True},
_BASE = _DECOMPILATION ? /\bbase\b/ : _TEST_TRUE,
_THIS = _DECOMPILATION ? /\bthis\b/ : _TEST_TRUE,
_MUTABLE = ["constructor", "toLocaleString", "toString"], // only override these when prototyping
_OBJECT_HIDDEN = {
constructor: 1,
hasOwnProperty: 1,
isPrototypeOf: 1,
propertyIsEnumerable: 1,
toLocaleString: 1,
toString: 1,
valueOf: 1
},
_FUNCTION_HIDDEN = extend(pcopy(_OBJECT_HIDDEN), {
apply: 1,
bind: 1,
call: 1,
length: 1,
prototype: 1
}),
_BASE_HIDDEN = extend(pcopy(_OBJECT_HIDDEN), {
base: 1,
extend: 1
});
_Object_forEach_check();
// Private data
var _private = global.$$base2;
if (!_private) {
_private = global.$$base2 = {"0": global, inc: 1, anon: []};
}
// Packages and Modules can be anonymous but still define namespaces.
function Anonymous(object) {
object.toString = K("[$$base2.anon[" + _private.anon.length + "]]");
_private.anon.push(object);
return object;
};
// =========================================================================
// base2/Base.js
// =========================================================================
// http://dean.edwards.name/weblog/2006/03/base/
var Base_static = {
ancestor: Object,
ancestorOf: function(klass) {
return klass && klass.prototype instanceof this;
},
base: function() {
// Call this method from any other method to invoke that method's ancestor.
},
extend: function(_instance, _static) {
// Build the prototype.
base2.__prototyping = this.prototype;
var _prototype = new this;
if (_instance) extend(_prototype, _instance);
_prototype.base = function() {
// Call this method from any other method to invoke that method's ancestor.
};
delete base2.__prototyping;
// Create the wrapper for the constructor function.
var _constructor = _prototype.constructor;
function _base2_constructor() {
// Don't call the constructor function when prototyping.
if (!base2.__prototyping) {
if (this.constructor == _base2_constructor || this.__constructing) {
// Instantiation.
this.__constructing = true;
_constructor.apply(this, arguments);
delete this.__constructing;
} else {
// Casting.
var object = arguments[0];
// Convert primitives to objects.
if (_PRIMITIVE_TYPE.test(typeof object)) {
object = new object.constructor(object);
}
base2.__casting = true;
extend(object, _prototype);
delete base2.__casting;
return object;
}
}
return this; // for strict engines
};
_prototype.constructor = _base2_constructor;
// Build the static interface.
for (var i in Base_static) _base2_constructor[i] = this[i];
if (_static) extend(_base2_constructor, _static);
_base2_constructor.ancestor = this;
_base2_constructor.base = _prototype.base;
_base2_constructor.prototype = _prototype;
if (_base2_constructor.init) _base2_constructor.init();
// introspection (removed when packed)
;;; _base2_constructor["#implements"] = [];
;;; _base2_constructor["#implemented_by"] = [];
return _base2_constructor;
},
forEach: function(object, eacher, context) {
Object_forEach(object, eacher, context, this);
},
implement: function(_interface) {
if (typeof _interface == "function") {
;;; if (_interface.prototype instanceof Base) {
// introspection (removed when packed)
;;; this["#implements"].push(_interface);
;;; _interface["#implemented_by"].push(this);
;;; }
_interface = _interface.prototype;
}
// Add the interface using the extend() function.
extend(this.prototype, _interface);
return this;
}
};
var Base = Base_static.extend.call(Object, {
constructor: function(properties) {
if (properties) extend(this, properties);
},
extend: delegate(extend),
toString: function() {
if (this.constructor.toString == _Function_prototype.toString) {
return "[object base2.Base]";
} else {
return "[object " + this.constructor.toString().slice(1, -1) + "]";
}
}
}, Base_static);
// =========================================================================
// base2/Abstract.js
// =========================================================================
var Abstract = Base.extend({
constructor: function() {
throw new TypeError("Abstract class cannot be instantiated.");
}
});
// =========================================================================
// base2/Package.js
// =========================================================================
var Package = Base.extend({
constructor: function(properties) {
var names = {};
if (properties) {
extend(this, properties);
if (properties != base2 && !("parent" in properties)) {
this.parent = base2;
}
}
var packageID = this.name;
if (packageID) {
if (this.parent instanceof Package) {
this.parent.addName(packageID, this);
packageID = this.parent.toString().slice(1, -1) + "." + packageID;
}
this.toString = K("[" + packageID + "]");
if (packageID != this.name) this.namespace = "var " + this.name + "=" + packageID + ";";
} else {
Anonymous(this);
packageID = this.toString().slice(1, -1);
}
var self = this;
function addName(value, name) {
self[name] = value;
if (!names[name]) {
names[name] = true;
if (self.exports) self.exports += ",";
self.exports += name;
self.namespace += "var " + name + "=" + packageID + "." + name + ";";
}
// Provide objects and classes with pretty toString methods
if (value && value.ancestorOf) { // it's a class
var classID = packageID + "." + name;
if (value.namespace) {
var anonID = value.toString().slice(1, -1);
value.namespace = "var " + name + "=" + classID + ";" +
value.namespace.replace(new RegExp(rescape(anonID), "g"), name);
}
value.toString = K("[" + classID + "]");
}
return value;
};
var exports = this.exports;
this.exports = "";
if (typeof exports == "object") forEach(exports, addName);
this.addName = flip(addName);
},
exports: "",
name: "",
namespace: "",
parent: null
});
// =========================================================================
// base2/Module.js
// =========================================================================
var Module = Abstract.extend(null, {
namespace: "",
extend: function(methods, _static) {
// Extend a module to create a new module.
var module = Anonymous(this.base());
// Inherit static methods.
for (var name in this) {
var method = this[name];
if (typeof method == "function" && !method._isModuleMethod && !Base_static[name]) {
module[name] = method;
}
}
module.namespace = "";
// Inherit module methods.
module.implement(this);
// Implement module methods.
if (methods) module.implement(methods);
// Implement static properties and methods.
if (_static) extend(module, _static);
return module;
},
forEach: function(eacher, context) {
// Members of a Module are the methods that it provides.
for (var name in this) {
var method = this[name];
if (method && method._isModuleMethod) {
eacher.call(context, method, name, this);
}
}
},
implement: function(methods) {
var moduleID = this.toString().slice(1, -1);
if (typeof methods == "object") {
// Add new methods from an object literal.
extendModule(this, methods, true);
// Rebuild the namespace
this.namespace = "";
// Loop through module methods to build the namespace.
for (name in this) {
method = this[name];
if (method && method._isModuleMethod) {
this.namespace += "var " + name + "=" + moduleID + "." + name + ";";
if (_THIS.test(method)) {
method = this[name] = bind(method, this);
method._isModuleMethod = true;
}
}
}
} else if (Module.ancestorOf(methods)) {
// Implement the methods from another Module.
this.base(methods);
for (var name in methods) {
var method = methods[name];
if (method && method._isModuleMethod) {
this[name] = method;
this.namespace += "var " + name + "=" + moduleID + "." + name + ";";
}
}
}
return this;
},
partial: function() {
// Return a clone of the Module that contains all of its methods after
// partial evaluation.
return extendModule(Module.extend(), map(this, partial), true);
}
});
function extendModule(module, methods, detected) {
// Extend a Module with an object literal.
// Allow for base2's detect() notation.
var proto = module.prototype;
for (var name in methods) {
var method = methods[name];
if (name.indexOf("@") === 0) { // object detection
extendModule(module, method, detected && detect(name.slice(1)));
} else if (method instanceof Function) {
if (!module[name] && (!detected || _THIS.test(method))) {
module[name] = createDelegate(name, method);
}
if (detected) {
if (_BASE.test(method)) {
method = _override(module, name, method);
} else {
module[name] = method;
}
proto[name] = createModuleMethod(module, name);
}
method._isModuleMethod = true;
}
}
return module;
};
function createModuleMethod(module, name) {
// Pass "this" and all other arguments to the underlying module method.
return function() {
return module[name].apply(module, _concat.apply([this], arguments));
};
};
function createDelegate(name, method) {
function _delegate(object) {
var ancestor = object[name].ancestor;
if (ancestor) object.base = ancestor;
var method = ancestor ? "base" : name;
var args = _slice.call(arguments, 1);
/*@if (@_jscript_version < 5.8)
return object[method](args[0], args[1], args[2], args[3]);
@else @*/
return object[method].apply(object, args);
/*@end @*/
};
_delegate._isModuleMethod = true;
return _delegate;
};
// =========================================================================
// base2/Enumerable.js
// =========================================================================
var Enumerable_methods = {
every: every,
filter: filter,
invoke: invoke,
map: map,
plant: plant,
pluck: pluck,
reduce: reduce,
some: some
};
var Enumerable = Module.extend(Enumerable_methods);
function every(enumerable, checker, context) {
var result = true;
try {
forEach (enumerable, function(item, key) {
result = checker.call(context, item, key, enumerable);
if (!result) throw StopIteration;
});
} catch (error) {
if (error != StopIteration) throw error;
}
return !!result; // cast to boolean
};
function filter(enumerable, checker, context) {
if (enumerable instanceof Map) {
return enumerable.filter(checker, context);
} else if (Array2_like(enumerable)) {
return Array2.filter(enumerable, checker, context);
} else {
var result = {};
forEach (enumerable, function(item, key) {
if (checker.call(context, item, key, enumerable)) {
result[key] = item;
}
});
return result;
}
};
function invoke(enumerable, method) {
// Apply a method to each item in the enumerated enumerable.
var args = _slice.call(arguments, 2);
return map (enumerable, typeOf(method) == "function" ? function(item) {
return item == null ? undefined : method.apply(item, args);
} : function(item) {
return item == null ? undefined : item[method].apply(item, args);
});
};
function map(enumerable, mapper, context) {
if (enumerable instanceof Map) {
return enumerable.map(mapper, context);
} else {
var result = Array2_like(enumerable) ? [] : {};
forEach (enumerable, function(item, key) {
result[key] = mapper.call(context, item, key, enumerable);
});
return result;
}
};
function plant(enumerable, propertyName, value) {
forEach (enumerable, typeOf(value) == "function" ? function(item) {
if (item != null) item[propertyName] = value(item, propertyName);
} : function(item) {
if (item != null) item[propertyName] = value;
});
};
function pluck(enumerable, propertyName) {
return map(enumerable, function(item) {
return item == null ? undefined : item[propertyName];
});
};
function reduce(enumerable, reducer, result) {
var initialised = arguments.length > 2;
forEach (enumerable, function(item, key) {
if (initialised) {
result = reducer(result, item, key, enumerable);
} else {
result = item;
initialised = true;
}
});
if (!initialised) throw new TypeError("Nothing to reduce.");
return result;
};
function some(enumerable, checker, context) {
return !every(enumerable, not(checker), context);
};
// =========================================================================
// base2/Map.js
// =========================================================================
// http://wiki.ecmascript.org/doku.php?id=proposals:dictionary
var HASH = "#";
var VALUES = HASH;
var Map = Base.extend({
constructor: function(values) {
this[VALUES] = {};
if (values) this.merge(values);
},
clear: function() {
this[VALUES] = {};
return this;
},
copy: function() {
var result = copy(this, true);
result[VALUES] = copy(this[VALUES]);
return result;
},
forEach: function(eacher, context) {
var values = this[VALUES];
for (var HASH_key in values) {
eacher.call(context, values[HASH_key], HASH_key.slice(1), this);
}
},
get: function(key) {
// Avoid warnings in strict engines.
var value = this[VALUES][HASH + key];
return value;
},
getKeys: function() {
var result = [], i = 0;
for (var HASH_key in this[VALUES]) {
result[i++] = HASH_key.slice(1);
}
return result; // returns an Array
},
getValues: function() {
var result = [], i = 0,
values = this[VALUES];
for (var HASH_key in values) {
result[i++] = values[HASH_key];
}
return result; // returns an Array
},
has: function(key) {
return HASH + key in this[VALUES];
},
merge: function(values /*, values2, values3, .. ,valuesN */) {
for (var i = 0; i < arguments.length; i++) {
values = arguments[i];
if (values && typeof values == "object" && values != this) {
if (values instanceof Map) {
values = values[VALUES];
for (var HASH_key in values) {
this.put(HASH_key.slice(1), values[HASH_key]);
}
} else {
for (var key in values) if (!_OBJECT_HIDDEN[key]) {
this.put(key, values[key]);
}
}
}
}
return this;
},
put: function(key, value) {
if (arguments.length == 1) value = key;
// Create the new entry (or overwrite the old entry).
return this[VALUES][HASH + key] = value;
},
remove: function(key) {
delete this[VALUES][HASH + key];
},
size: function() {
// This is expensive because we are not storing the keys.
var size = 0;
for (var HASH_key in this[VALUES]) size++;
return size;
},
union: function(values /*, values2, values3, .. ,valuesN */) {
return this.merge.apply(this.copy(), arguments);
}
});
Map.implement(Enumerable);
// Optimise map/filter methods.
Map.implement({
filter: function(checker, context) {
// Returns a clone of the current object with its members filtered by "checker".
var result = copy(this, true),
resultValues = result[VALUES] = {},
values = this[VALUES];
for (var HASH_key in values) {
var value = values[HASH_key];
if (checker.call(context, value, HASH_key.slice(1), this)) {
resultValues[HASH_key] = value;
}
}
return result; // returns a Map
},
map: function(mapper, context) {
// Returns a new Map containing the mapped values.
var result = new Map,
resultValues = result[VALUES],
values = this[VALUES];
for (var HASH_key in values) {
resultValues[HASH_key] = mapper.call(context, values[HASH_key], HASH_key.slice(1), this);
}
return result; // returns a Map
}
});
// =========================================================================
// base2/Collection.js
// =========================================================================
// A Map that is more array-like (accessible by index).
// Collection classes have a special (optional) property: Item
// The Item property points to a constructor function.
// Members of the collection must be an instance of Item.
// The static createItem() method is responsible for construction of collection items.
// Instance methods that add new items (add, put, insertAt, putAt) pass their arguments
// to the static createItem() method. If you want to modify the way collection items are
// created then you only need to override the createItem() method for custom collections.
var KEYS = ".";
var ITEMS = VALUES;
var ERR_DUPLICATE_KEY = new ReferenceError("Duplicate key.");
var ERR_INDEX_OUT_OF_BOUNDS = new ReferenceError("Index out of bounds.");
var Collection = Map.extend({
constructor: function(items) {
this[KEYS] = [];
this.base(items);
},
add: function(key, item) {
// Duplicates not allowed using add().
// But you can still overwrite entries using put().
if (HASH + key in this[ITEMS]) throw ERR_DUPLICATE_KEY;
return this.put.apply(this, arguments);
},
clear: function() {
this[KEYS].length = 0;
this[ITEMS] = {};
return this;
},
copy: function() {
var result = this.base();
result[KEYS] = this[KEYS].concat();
return result;
},
filter: function(checker, context) {
// Returns a clone of the current object with its members filtered by "test".
var keys = this[KEYS],
items = this[ITEMS],
size = keys.length,
result = copy(this, true),
resultKeys = result[KEYS] = [],
resultItems = result[ITEMS] = {};
for (var i = 0, j = 0, key, HASH_key; i < size; i++) {
var item = items[HASH_key = HASH + (key = keys[i])];
if (checker.call(context, item, key, this)) {
resultKeys[j++] = key;
resultItems[HASH_key] = item;
}
}
return result;
},
forEach: function(eacher, context) {
var keys = this[KEYS].concat(), key,
items = this[ITEMS],
size = keys.length;
for (var i = 0; i < size; i++) {
eacher.call(context, items[HASH + (key = keys[i])], key, this);
}
},
getAt: function(index) {
var key = Array2_item(this[KEYS], index); // Allow negative indexes.
return key == null ? undefined : this[ITEMS][HASH + key];
},
getKeys: function() {
return this[KEYS].concat(); // returns an Array
},
getValues: function() {
var keys = this[KEYS],
items = this[ITEMS],
i = keys.length,
result = [];
while (i--) result[i] = items[HASH + keys[i]];
return result; // returns an Array
},
imap: function(mapper, context) { // the same as Array.map(this.getValues(), ..) but faster.
var keys = this[KEYS],
items = this[ITEMS],
size = keys.length,
result = [];
for (var i = 0; i < size; i++) {
result[i] = mapper.call(context, items[HASH + keys[i]], i, this);
}
return result; // returns an Array
},
indexOf: function(key) {
return HASH + key in this[ITEMS] ? Array2.indexOf(this[KEYS], String(key)) : -1;
},
insertAt: function(index, key, item) {
var keys = this[KEYS],
items = this[ITEMS],
HASH_key = HASH + key;
if (HASH_key in items) throw ERR_DUPLICATE_KEY;
if (Array2_item(keys, index) == null) throw ERR_INDEX_OUT_OF_BOUNDS;
keys.splice(index, 0, String(key));
items[HASH_key] = "{placeholder}";
return this.put.apply(this, _slice.call(arguments, 1));
},
item: function(keyOrIndex) {
return this[typeof keyOrIndex == "number" ? "getAt" : "get"](keyOrIndex);
},
join: function(separator) {
return this.getValues().join(separator);
},
keyAt: function(index) {
return Array2_item(this[KEYS], index); // Allow negative indexes.
},
map: function(mapper, context) {
// Returns a new Collection containing the mapped items.
var keys = this[KEYS], key, HASH_key,
items = this[ITEMS],
size = keys.length,
result = new Collection,
resultItems = result[ITEMS];
result[KEYS] = keys.concat();
for (var i = 0; i < size; i++) {
resultItems[HASH_key = HASH + (key = keys[i])] =
mapper.call(context, items[HASH_key], key, this);
}
return result; // returns a Collection
},
put: function(key, item) {
if (arguments.length == 1) item = key;
var klass = this.constructor;
if (klass.Item && !(item instanceof klass.Item)) {
item = klass.createItem.apply(klass, arguments);
}
var keys, items = this[ITEMS];
if (!(HASH + key in items)) {
(keys = this[KEYS])[keys.length] = String(key);
}
items[HASH + key] = item;
return item;
},
putAt: function(index, item) {
var key = Array2_item(this[KEYS], index); // Allow negative indexes.
if (key == null) throw ERR_INDEX_OUT_OF_BOUNDS;
arguments[0] = key;
return this.put.apply(this, arguments);
},
remove: function(key) {
// The remove() method can be slow so check if the key exists first.
var items = this[ITEMS];
if (HASH + key in items) {
delete items[HASH + key];
var keys = this[KEYS],
index = Array2.indexOf(keys, String(key));
if (index !== -1) keys.splice(index, 1);
}
},
removeAt: function(index) {
var removed = this[KEYS].splice(index, 1);
if (removed.length) {
delete this[ITEMS][HASH + removed[0]];
}
},
reverse: function() {
this[KEYS].reverse();
return this;
},
size: function() {
return this[KEYS].length;
},
slice: function(start, end) {
var result = this.copy();
if (arguments.length > 0) {
var removed = result[KEYS],
size = removed.length;
start = clamp(start, size);
end = isNaN(end) ? size : clamp(end, size);
var keys = removed.splice(start, end - start),
i = removed.length,
resultItems = result[ITEMS];
while (i--) delete resultItems[HASH + removed[i]];
result[KEYS] = keys;
}
return result;
},
sort: function(compare) {
if (compare) {
var items = this[ITEMS];
this[KEYS].sort(function(key1, key2) {
return compare(items[HASH + key1], items[HASH + key2], key1, key2);
});
} else {
this[KEYS].sort();
}
return this;
},
toString: function() {
return "(" + (this[KEYS] || "") + ")";
}
}, {
Item: null, // If specified, all members of the collection must be instances of Item.
createItem: function(key, item) {
return this.Item ? new this.Item(key, item) : item;
},
extend: function(_instance, _static) {
var klass = this.base(_instance);
klass.createItem = this.createItem;
if (_static) extend(klass, _static);
if (!klass.Item) {
klass.Item = this.Item;
} else if (typeof klass.Item != "function") {
klass.Item = (this.Item || Base).extend(klass.Item);
}
if (klass.init) klass.init();
return klass;
}
});
// =========================================================================
// base2/RegGrp.js
// =========================================================================
// A collection of regular expressions and their associated replacement values.
// A Base class for creating parsers.
var PATTERNS = KEYS;
var COMPILED = "_";
var REGGRP_BACK_REF = /\\(\d+)/g,
REGGRP_ESCAPE_COUNT = /\[(\\.|[^\]\\])+\]|\\.|\(\?/g,
REGGRP_PAREN = /\(/g,
REGGRP_LOOKUP = /\$(\d+)/,
REGGRP_LOOKUP_SIMPLE = /^\$\d+$/,
REGGRP_LOOKUPS = /(\[(\\.|[^\]\\])+\]|\\.|\(\?)|\(/g,
REGGRP_DICT_ENTRY = /^<#\w+>$/,
REGGRP_DICT_ENTRIES = /<#(\w+)>/g;
var RegGrp = Collection.extend({
constructor: function(dictionary, values) {
if (arguments.length == 1) {
values = dictionary;
dictionary = null;
}
if (dictionary) {
this.dictionary = new RegGrp.Dict(dictionary);
}
this.base(values);
},
dictionary: null,
ignoreCase: false,
clear: _recompile,
copy: function() {
var result = this.base();
if (this.dictionary) {
result.dictionary = this.dictionary.copy();
}
return result;
},
exec: function(string, _replacement /* optional */) {
string += "";
var group = this,
patterns = group[PATTERNS],
items = group[ITEMS];
if (!patterns.length) return string;
if (!group[COMPILED] || group[COMPILED].ignoreCase != group.ignoreCase) {
group[COMPILED] = new RegExp(group, group.ignoreCase ? "gi" : "g");
}
return string.replace(group[COMPILED], function(match) {
var args = [], item, offset = 1, i = arguments.length;
while (--i) args[i] = arguments[i] || ""; // some platforms return null/undefined for non-matching sub-expressions
// Loop through the RegGrp items.
while ((item = items[HASH + patterns[i++]])) {
var next = offset + item.length + 1;
if (args[offset]) { // do we have a result?
var replacement = _replacement == null ? item.replacement : _replacement;
switch (typeof replacement) {
case "function":
return replacement.apply(group, args.slice(offset, next));
case "number":
return args[offset + replacement];
case "object":
if (replacement instanceof RegGrp) {
return replacement.exec(args[offset]);
}
default:
return replacement;
}
}
offset = next;
}
return match;
});
},
reverse: _recompile,
sort: _recompile,
test: function(string) { // The slow way to do it. Hopefully, this isn't called too often. :)
return this.exec(string) != string;
},
toString: function() {
var patterns = this[PATTERNS].concat();
var string = patterns.join(")|(");
if (/\\\d/.test(string)) { // back refs
var offset = 1,
items = this[ITEMS], item;
for (var i = 0; item = items[HASH + patterns[i]]; i++) {
patterns[i] = item.source.replace(REGGRP_BACK_REF, function(match, index) {
return "\\" + (offset + ~~index);
});
offset += item.length + 1;
}
string = patterns.join(")|(");
}
return "(" + string + ")";
}
}, {
IGNORE: null, // a null replacement value means that there is no replacement.
Item: {
constructor: function(source, replacement) {
if (replacement == null) {
replacement = 0;
} else if (typeof replacement.replacement != "undefined") {
replacement = replacement.replacement;
} else if (typeof replacement == "number") {
replacement = String(replacement);
}
var length = source.indexOf("(") === -1 ? 0 : RegGrp.count(source);
// Does the expression use sub-expression lookups?
if (typeof replacement == "string" && REGGRP_LOOKUP.test(replacement)) {
if (REGGRP_LOOKUP_SIMPLE.test(replacement)) { // A simple lookup? (e.g. "$2").
// Store the index (used for fast retrieval of matched strings).
var index = replacement.slice(1) - 0;