ee57e5d643861ed6bd85ecfb48042485f57d8df2
[jquery.git] / build / js / base2.js
1 // timestamp: Tue, 24 Apr 2007 09:57:15\r
2 /*\r
3         base2.js - copyright 2007, Dean Edwards\r
4         http://www.opensource.org/licenses/mit-license\r
5 */\r
6 \r
7 var base2 = {};\r
8 \r
9 // You know, writing a javascript library is awfully time consuming.\r
10 \r
11 new function(_) { ////////////////////  BEGIN: CLOSURE  ////////////////////\r
12 \r
13 // =========================================================================\r
14 // base2/Base.js\r
15 // =========================================================================\r
16 \r
17 // version 1.1\r
18 \r
19 var Base = function() {\r
20         // call this method from any other method to invoke that method's ancestor\r
21 };\r
22 \r
23 Base.prototype = {      \r
24         extend: function(source) {\r
25                 if (arguments.length > 1) { // extending with a name/value pair\r
26                         var ancestor = this[source];\r
27                         var value = arguments[1];\r
28                         if (typeof value == "function" && ancestor && /\bbase\b/.test(value)) {\r
29                                 var method = value;                             \r
30                                 value = function() { // override\r
31                                         var previous = this.base;\r
32                                         this.base = ancestor;\r
33                                         var returnValue = method.apply(this, arguments);\r
34                                         this.base = previous;\r
35                                         return returnValue;\r
36                                 };\r
37                                 value.method = method;\r
38                                 value.ancestor = ancestor;\r
39                         }\r
40                         this[source] = value;\r
41                 } else if (source) { // extending with an object literal\r
42                         var extend = Base.prototype.extend;\r
43                         if (Base._prototyping) {\r
44                                 var key, i = 0, members = ["constructor", "toString", "valueOf"];\r
45                                 while (key = members[i++]) if (source[key] != Object.prototype[key]) {\r
46                                         extend.call(this, key, source[key]);\r
47                                 }\r
48                         } else if (typeof this != "function") {\r
49                                 // if the object has a customised extend() method then use it\r
50                                 extend = this.extend || extend;\r
51                         }                       \r
52                         // copy each of the source object's properties to this object\r
53                         for (key in source) if (!Object.prototype[key]) {\r
54                                 extend.call(this, key, source[key]);\r
55                         }\r
56                 }\r
57                 return this;\r
58         },\r
59 \r
60         base: Base\r
61 };\r
62 \r
63 Base.extend = function(_instance, _static) { // subclass\r
64         var extend = Base.prototype.extend;\r
65         \r
66         // build the prototype\r
67         Base._prototyping = true;\r
68         var proto = new this;\r
69         extend.call(proto, _instance);\r
70         delete Base._prototyping;\r
71         \r
72         // create the wrapper for the constructor function\r
73         var constructor = proto.constructor;\r
74         var klass = proto.constructor = function() {\r
75                 if (!Base._prototyping) {\r
76                         if (this._constructing || this.constructor == klass) { // instantiation\r
77                                 this._constructing = true;\r
78                                 constructor.apply(this, arguments);\r
79                                 delete this._constructing;\r
80                         } else { // casting\r
81                                 var object = arguments[0];\r
82                                 if (object != null) {\r
83                                         (object.extend || extend).call(object, proto);\r
84                                 }\r
85                                 return object;\r
86                         }\r
87                 }\r
88         };\r
89         \r
90         // build the class interface\r
91         for (var i in Base) klass[i] = this[i];\r
92         klass.ancestor = this;\r
93         klass.base = Base.base;\r
94         klass.prototype = proto;\r
95         klass.toString = this.toString;\r
96         extend.call(klass, _static);\r
97         // class initialisation\r
98         if (typeof klass.init == "function") klass.init();\r
99         return klass;\r
100 };\r
101 \r
102 // initialise\r
103 Base = Base.extend({\r
104         constructor: function() {\r
105                 this.extend(arguments[0]);\r
106         }\r
107 }, {\r
108         ancestor: Object,\r
109         base: Base,\r
110         \r
111         implement: function(_interface) {\r
112                 if (typeof _interface == "function") {\r
113                         // if it's a function, call it\r
114                         _interface(this.prototype);\r
115                 } else {\r
116                         // add the interface using the extend() method\r
117                         this.prototype.extend(_interface);\r
118                 }\r
119                 return this;\r
120         }\r
121 });\r
122 \r
123 // =========================================================================\r
124 // lang/main.js\r
125 // =========================================================================\r
126 \r
127 var Legacy = typeof $Legacy == "undefined" ? {} : $Legacy;\r
128 \r
129 var K = function(k) {return k};\r
130 \r
131 var assert = function(condition, message, Err) {\r
132         if (!condition) {\r
133                 throw new (Err || Error)(message || "Assertion failed.");\r
134         }\r
135 };\r
136 \r
137 var assertType = function(object, type, message) {\r
138         if (type) {\r
139                 var condition = typeof type == "function" ? instanceOf(object, type) : typeof object == type;\r
140                 assert(condition, message || "Invalid type.", TypeError);\r
141         }\r
142 };\r
143 \r
144 var format = function(string) {\r
145         // replace %n with arguments[n]\r
146         // e.g. format("%1 %2%3 %2a %1%3", "she", "se", "lls");\r
147         // ==> "she sells sea shells"\r
148         // only supports nine replacements: %1 - %9\r
149         var args = arguments;\r
150         return String(string).replace(/%([1-9])/g, function(match, index) {\r
151                 return index < args.length ? args[index] : match;\r
152         });\r
153 };\r
154 \r
155 var $instanceOf = Legacy.instanceOf || new Function("o,k", "return o instanceof k");\r
156 var instanceOf = function(object, klass) {\r
157         assertType(klass, "function", "Invalid 'instanceOf' operand.");\r
158         if ($instanceOf(object, klass)) return true;\r
159         // handle exceptions where the target object originates from another frame\r
160         //  this is handy for JSON parsing (amongst other things)\r
161         if (object != null) switch (klass) {\r
162                 case Object:\r
163                         return true;\r
164                 case Number:\r
165                 case Boolean:\r
166                 case Function:\r
167                 case String:\r
168                         return typeof object == typeof klass.prototype.valueOf();\r
169                 case Array:\r
170                         // this is the only troublesome one\r
171                         return !!(object.join && object.splice && !arguments.callee(object, Function));\r
172                 case Date:\r
173                         return !!object.getTimezoneOffset;\r
174                 case RegExp:\r
175                         return String(object.constructor.prototype) == String(new RegExp);\r
176         }\r
177         return false;\r
178 };\r
179         \r
180 var match = function(string, expression) {\r
181         // same as String.match() except that this function will return an empty \r
182         // array if there is no match\r
183         return String(string).match(expression) || [];\r
184 };\r
185 \r
186 var RESCAPE = /([\/()[\]{}|*+-.,^$?\\])/g;\r
187 var rescape = function(string) {\r
188         // make a string safe for creating a RegExp\r
189         return String(string).replace(RESCAPE, "\\$1");\r
190 };\r
191 \r
192 var $slice = Array.prototype.slice;\r
193 var slice = function(object) {\r
194         // slice an array-like object\r
195         return $slice.apply(object, $slice.call(arguments, 1));\r
196 };\r
197 \r
198 var TRIM = /^\s+|\s+$/g;\r
199 var trim = function(string) {\r
200         return String(string).replace(TRIM, "");        \r
201 };\r
202 \r
203 // =========================================================================\r
204 // lang/extend.js\r
205 // =========================================================================\r
206 \r
207 var base = function(object, args) {\r
208         // invoke the base method with all supplied arguments\r
209         return object.base.apply(object, args);\r
210 };\r
211 \r
212 var extend = function(object) {\r
213         assert(object != Object.prototype, "Object.prototype is verboten!");\r
214         return Base.prototype.extend.apply(object, slice(arguments, 1));\r
215 };\r
216 \r
217 // =========================================================================\r
218 // lang/assignID.js\r
219 // =========================================================================\r
220 \r
221 var $ID = 1;\r
222 var assignID = function(object) {\r
223         // assign a unique id\r
224         if (!object.base2ID) object.base2ID = "b2_" + $ID++;\r
225         return object.base2ID;\r
226 };\r
227 \r
228 // =========================================================================\r
229 // lang/forEach.js\r
230 // =========================================================================\r
231 \r
232 if (typeof StopIteration == "undefined") {\r
233         StopIteration = new Error("StopIteration");\r
234 }\r
235 \r
236 var forEach = function(object, block, context) {\r
237         if (object == null) return;\r
238         if (typeof object == "function") {\r
239                 // functions are a special case\r
240                 var fn = Function;\r
241         } else if (typeof object.forEach == "function" && object.forEach != arguments.callee) {\r
242                 // the object implements a custom forEach method\r
243                 object.forEach(block, context);\r
244                 return;\r
245         } else if (typeof object.length == "number") {\r
246                 // the object is array-like\r
247                 forEach.Array(object, block, context);\r
248                 return;\r
249         }\r
250         forEach.Function(fn || Object, object, block, context);\r
251 };\r
252 \r
253 // these are the two core enumeration methods. all other forEach methods\r
254 //  eventually call one of these two.\r
255 \r
256 forEach.Array = function(array, block, context) {\r
257         var i, length = array.length; // preserve\r
258         if (typeof array == "string") {\r
259                 for (i = 0; i < length; i++) {\r
260                         block.call(context, array.charAt(i), i, array);\r
261                 }\r
262         } else {\r
263                 for (i = 0; i < length; i++) {\r
264                         block.call(context, array[i], i, array);\r
265                 }\r
266         }\r
267 };\r
268 \r
269 forEach.Function = Legacy.forEach || function(fn, object, block, context) {\r
270         // enumerate an object and compare its keys with fn's prototype\r
271         for (var key in object) {\r
272                 if (fn.prototype[key] === undefined) {\r
273                         block.call(context, object[key], key, object);\r
274                 }\r
275         }\r
276 };\r
277 \r
278 // =========================================================================\r
279 // base2/Base/forEach.js\r
280 // =========================================================================\r
281 \r
282 Base.forEach = function(object, block, context) {\r
283         forEach.Function(this, object, block, context);\r
284 };\r
285 \r
286 // =========================================================================\r
287 // base2/../Function.js\r
288 // =========================================================================\r
289 \r
290 // some browsers don't define this\r
291 \r
292 Function.prototype.prototype = {};\r
293 \r
294 \r
295 // =========================================================================\r
296 // base2/../String.js\r
297 // =========================================================================\r
298 \r
299 // fix String.replace (Safari/IE5.0)\r
300 \r
301 if ("".replace(/^/, String)) {\r
302         extend(String.prototype, "replace", function(expression, replacement) {\r
303                 if (typeof replacement == "function") { // Safari doesn't like functions\r
304                         if (instanceOf(expression, RegExp)) {\r
305                                 var regexp = expression;\r
306                                 var global = regexp.global;\r
307                                 if (global == null) global = /(g|gi)$/.test(regexp);\r
308                                 // we have to convert global RexpExps for exec() to work consistently\r
309                                 if (global) regexp = new RegExp(regexp.source); // non-global\r
310                         } else {\r
311                                 regexp = new RegExp(rescape(expression));\r
312                         }\r
313                         var match, string = this, result = "";\r
314                         while (string && (match = regexp.exec(string))) {\r
315                                 result += string.slice(0, match.index) + replacement.apply(this, match);\r
316                                 string = string.slice(match.index + match[0].length);\r
317                                 if (!global) break;\r
318                         }\r
319                         return result + string;\r
320                 } else {\r
321                         return base(this, arguments);\r
322                 }\r
323         });\r
324 }\r
325 \r
326 // =========================================================================\r
327 // base2/Abstract.js\r
328 // =========================================================================\r
329 \r
330 var Abstract = Base.extend({\r
331         constructor: function() {\r
332                 throw new TypeError("Class cannot be instantiated.");\r
333         }\r
334 });\r
335 \r
336 // =========================================================================\r
337 // base2/Module.js\r
338 // =========================================================================\r
339 \r
340 // based on ruby's Module class and Mozilla's Array generics:\r
341 //   http://www.ruby-doc.org/core/classes/Module.html\r
342 //   http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6#Array_and_String_generics\r
343 \r
344 // A Module is used as the basis for creating interfaces that can be\r
345 // applied to other classes. *All* properties and methods are static.\r
346 // When a module is used as a mixin, methods defined on what would normally be\r
347 // the instance interface become instance methods of the target object.\r
348 \r
349 // Modules cannot be instantiated. Static properties and methods are inherited.\r
350 \r
351 var Module = Abstract.extend(null, {\r
352         extend: function(_interface, _static) {\r
353                 // extend a module to create a new module\r
354                 var module = this.base();\r
355                 // inherit static methods\r
356                 forEach (this, function(property, name) {\r
357                         if (!Module[name] && name != "init") {\r
358                                 extend(module, name, property);\r
359                         }\r
360                 });\r
361                 // implement module (instance AND static) methods\r
362                 module.implement(_interface);\r
363                 // implement static properties and methods\r
364                 extend(module, _static);\r
365                 // Make the submarine noises Larry!\r
366                 if (typeof module.init == "function") module.init();\r
367                 return module;\r
368         },\r
369         \r
370         implement: function(_interface) {\r
371                 // implement an interface on BOTH the instance and static interfaces\r
372                 var module = this;\r
373                 if (typeof _interface == "function") {\r
374                         module.base(_interface);\r
375                         forEach (_interface, function(property, name) {\r
376                                 if (!Module[name] && name != "init") {\r
377                                         extend(module, name, property);\r
378                                 }\r
379                         });\r
380                 } else {\r
381                         // create the instance interface\r
382                         Base.forEach (extend({}, _interface), function(property, name) {\r
383                                 // instance methods call the equivalent static method\r
384                                 if (typeof property == "function") {\r
385                                         property = function() {\r
386                                                 base; // force inheritance\r
387                                                 return module[name].apply(module, [this].concat(slice(arguments)));\r
388                                         };\r
389                                 }\r
390                                 if (!Module[name]) extend(this, name, property);\r
391                         }, module.prototype);\r
392                         // add the static interface\r
393                         extend(module, _interface);\r
394                 }\r
395                 return module;\r
396         }\r
397 });\r
398 \r
399 \r
400 // =========================================================================\r
401 // base2/Enumerable.js\r
402 // =========================================================================\r
403 \r
404 var Enumerable = Module.extend({\r
405         every: function(object, test, context) {\r
406                 var result = true;\r
407                 try {\r
408                         this.forEach (object, function(value, key) {\r
409                                 result = test.call(context, value, key, object);\r
410                                 if (!result) throw StopIteration;\r
411                         });\r
412                 } catch (error) {\r
413                         if (error != StopIteration) throw error;\r
414                 }\r
415                 return !!result; // cast to boolean\r
416         },\r
417         \r
418         filter: function(object, test, context) {\r
419                 return this.reduce(object, new Array2, function(result, value, key) {\r
420                         if (test.call(context, value, key, object)) {\r
421                                 result[result.length] = value;\r
422                         }\r
423                         return result;\r
424                 });\r
425         },\r
426 \r
427         invoke: function(object, method) {\r
428                 // apply a method to each item in the enumerated object\r
429                 var args = slice(arguments, 2);\r
430                 return this.map(object, (typeof method == "function") ? function(item) {\r
431                         if (item != null) return method.apply(item, args);\r
432                 } : function(item) {\r
433                         if (item != null) return item[method].apply(item, args);\r
434                 });\r
435         },\r
436         \r
437         map: function(object, block, context) {\r
438                 var result = new Array2;\r
439                 this.forEach (object, function(value, key) {\r
440                         result[result.length] = block.call(context, value, key, object);\r
441                 });\r
442                 return result;\r
443         },\r
444         \r
445         pluck: function(object, key) {\r
446                 return this.map(object, function(item) {\r
447                         if (item != null) return item[key];\r
448                 });\r
449         },\r
450         \r
451         reduce: function(object, result, block, context) {\r
452                 this.forEach (object, function(value, key) {\r
453                         result = block.call(context, result, value, key, object);\r
454                 });\r
455                 return result;\r
456         },\r
457         \r
458         some: function(object, test, context) {\r
459                 return !this.every(object, function(value, key) {\r
460                         return !test.call(context, value, key, object);\r
461                 });\r
462         }\r
463 }, {\r
464         forEach: forEach\r
465 });\r
466 \r
467 // =========================================================================\r
468 // base2/Array2.js\r
469 // =========================================================================\r
470 \r
471 // The IArray module implements all Array methods.\r
472 // This module is not public but its methods are accessible through the Array2 object (below). \r
473 \r
474 var IArray = Module.extend({\r
475         combine: function(keys, values) {\r
476                 // combine two arrays to make a hash\r
477                 if (!values) values = keys;\r
478                 return this.reduce(keys, {}, function(object, key, index) {\r
479                         object[key] = values[index];\r
480                         return object;\r
481                 });\r
482         },\r
483         \r
484         copy: function(array) {\r
485                 return this.concat(array);\r
486         },\r
487         \r
488         contains: function(array, item) {\r
489                 return this.indexOf(array, item) != -1;\r
490         },\r
491         \r
492         forEach: forEach.Array,\r
493         \r
494         indexOf: function(array, item, fromIndex) {\r
495                 var length = array.length;\r
496                 if (fromIndex == null) {\r
497                         fromIndex = 0;\r
498                 } else if (fromIndex < 0) {\r
499                         fromIndex = Math.max(0, length + fromIndex);\r
500                 }\r
501                 for (var i = fromIndex; i < length; i++) {\r
502                         if (array[i] === item) return i;\r
503                 }\r
504                 return -1;\r
505         },\r
506         \r
507         insertAt: function(array, item, index) {\r
508                 this.splice(array, index, 0, item);\r
509                 return item;\r
510         },\r
511         \r
512         insertBefore: function(array, item, before) {\r
513                 var index = this.indexOf(array, before);\r
514                 if (index == -1) this.push(array, item);\r
515                 else this.splice(array, index, 0, item);\r
516                 return item;\r
517         },\r
518         \r
519         lastIndexOf: function(array, item, fromIndex) {\r
520                 var length = array.length;\r
521                 if (fromIndex == null) {\r
522                         fromIndex = length - 1;\r
523                 } else if (from < 0) {\r
524                         fromIndex = Math.max(0, length + fromIndex);\r
525                 }\r
526                 for (var i = fromIndex; i >= 0; i--) {\r
527                         if (array[i] === item) return i;\r
528                 }\r
529                 return -1;\r
530         },\r
531         \r
532         remove: function(array, item) {\r
533                 var index = this.indexOf(array, item);\r
534                 if (index != -1) this.removeAt(array, index);\r
535                 return item;\r
536         },\r
537         \r
538         removeAt: function(array, index) {\r
539                 var item = array[index];\r
540                 this.splice(array, index, 1);\r
541                 return item;\r
542         }\r
543 });\r
544 \r
545 IArray.prototype.forEach = function(block, context) {\r
546         forEach.Array(this, block, context);\r
547 };\r
548 \r
549 IArray.implement(Enumerable);\r
550 \r
551 forEach ("concat,join,pop,push,reverse,shift,slice,sort,splice,unshift".split(","), function(name) {\r
552         IArray[name] = function(array) {\r
553                 return Array.prototype[name].apply(array, slice(arguments, 1));\r
554         };\r
555 });\r
556 \r
557 // create a faux constructor that augments the built-in Array object\r
558 var Array2 = function() {\r
559         return IArray(this.constructor == IArray ? Array.apply(null, arguments) : arguments[0]);\r
560 };\r
561 // expose IArray.prototype so that it can be extended\r
562 Array2.prototype = IArray.prototype;\r
563 \r
564 forEach (IArray, function(method, name, proto) {\r
565         if (Array[name]) {\r
566                 IArray[name] = Array[name];\r
567                 delete IArray.prototype[name];\r
568         }\r
569         Array2[name] = IArray[name];\r
570 });\r
571 \r
572 // =========================================================================\r
573 // base2/Hash.js\r
574 // =========================================================================\r
575 \r
576 var HASH = "#" + Number(new Date);\r
577 var KEYS = HASH + "keys";\r
578 var VALUES = HASH + "values";\r
579 \r
580 var Hash = Base.extend({\r
581         constructor: function(values) {\r
582                 this[KEYS] = new Array2;\r
583                 this[VALUES] = {};\r
584                 this.merge(values);\r
585         },\r
586 \r
587         copy: function() {\r
588                 var copy = new this.constructor(this);\r
589                 Base.forEach (this, function(property, name) {\r
590                         if (typeof property != "function" && name.charAt(0) != "#") {\r
591                                 copy[name] = property;\r
592                         }\r
593                 });\r
594                 return copy;\r
595         },\r
596 \r
597         // ancient browsers throw an error when we use "in" as an operator \r
598         //  so we must create the function dynamically\r
599         exists: Legacy.exists || new Function("k", format("return('%1'+k)in this['%2']", HASH, VALUES)),\r
600 \r
601         fetch: function(key) {\r
602                 return this[VALUES][HASH + key];\r
603         },\r
604 \r
605         forEach: function(block, context) {\r
606                 forEach (this[KEYS], function(key) {\r
607                         block.call(context, this.fetch(key), key, this);\r
608                 }, this);\r
609         },\r
610 \r
611         keys: function(index, length) {\r
612                 var keys = this[KEYS] || new Array2;\r
613                 switch (arguments.length) {\r
614                         case 0: return keys.copy();\r
615                         case 1: return keys[index];\r
616                         default: return keys.slice(index, length);\r
617                 }\r
618         },\r
619 \r
620         merge: function(values) {\r
621                 forEach (arguments, function(values) {\r
622                         forEach (values, function(value, key) {\r
623                                 this.store(key, value);\r
624                         }, this);\r
625                 }, this);\r
626                 return this;\r
627         },\r
628 \r
629         remove: function(key) {\r
630                 var value = this.fetch(key);\r
631                 this[KEYS].remove(String(key));\r
632                 delete this[VALUES][HASH + key];\r
633                 return value;\r
634         },\r
635 \r
636         store: function(key, value) {\r
637                 if (arguments.length == 1) value = key;\r
638                 // only store the key for a new entry\r
639                 if (!this.exists(key)) {\r
640                         this[KEYS].push(String(key));\r
641                 }\r
642                 // create the new entry (or overwrite the old entry)\r
643                 this[VALUES][HASH + key] = value;\r
644                 return value;\r
645         },\r
646 \r
647         toString: function() {\r
648                 return String(this[KEYS]);\r
649         },\r
650 \r
651         union: function(values) {\r
652                 return this.merge.apply(this.copy(), arguments);\r
653         },\r
654 \r
655         values: function(index, length) {\r
656                 var values = this.map(K);\r
657                 switch (arguments.length) {\r
658                         case 0: return values;\r
659                         case 1: return values[index];\r
660                         default: return values.slice(index, length);\r
661                 }\r
662         }\r
663 });\r
664 \r
665 Hash.implement(Enumerable);\r
666 \r
667 // =========================================================================\r
668 // base2/Collection.js\r
669 // =========================================================================\r
670 \r
671 // A Hash that is more array-like (accessible by index).\r
672 \r
673 // Collection classes have a special (optional) property: Item\r
674 // The Item property points to a constructor function.\r
675 // Members of the collection must be an instance of Item.\r
676 // e.g.\r
677 //     var Dates = Collection.extend();                 // create a collection class\r
678 //     Dates.Item = Date;                               // only JavaScript Date objects allowed as members\r
679 //     var appointments = new Dates();                  // instantiate the class\r
680 //     appointments.add(appointmentId, new Date);       // add a date\r
681 //     appointments.add(appointmentId, "tomorrow");     // ERROR!\r
682 \r
683 // The static create() method is responsible for all construction of collection items.\r
684 // Instance methods that add new items (add, store, insertAt, replaceAt) pass *all* of their arguments\r
685 // to the static create() method. If you want to modify the way collection items are \r
686 // created then you only need to override this method for custom collections.\r
687 \r
688 var Collection = Hash.extend({\r
689         add: function(key, item) {\r
690                 // Duplicates not allowed using add().\r
691                 //  - but you can still overwrite entries using store()\r
692                 assert(!this.exists(key), "Duplicate key.");\r
693                 return this.store.apply(this, arguments);\r
694         },\r
695 \r
696         count: function() {\r
697                 return this[KEYS].length;\r
698         },\r
699 \r
700         indexOf: function(key) {\r
701                 return this[KEYS].indexOf(String(key));\r
702         },\r
703 \r
704         insertAt: function(index, key, item) {\r
705                 assert(!this.exists(key), "Duplicate key.");\r
706                 this[KEYS].insertAt(index, String(key));\r
707                 return this.store.apply(this, slice(arguments, 1));\r
708         },\r
709 \r
710         item: function(index) {\r
711                 return this.fetch(this[KEYS][index]);\r
712         },\r
713 \r
714         removeAt: function(index) {\r
715                 return this.remove(this[KEYS][index]);\r
716         },\r
717 \r
718         reverse: function() {\r
719                 this[KEYS].reverse();\r
720                 return this;\r
721         },\r
722 \r
723         sort: function(compare) {\r
724                 if (compare) {\r
725                         var self = this;\r
726                         this[KEYS].sort(function(key1, key2) {\r
727                                 return compare(self.fetch(key1), self.fetch(key2), key1, key2);\r
728                         });\r
729                 } else this[KEYS].sort();\r
730                 return this;\r
731         },\r
732 \r
733         store: function(key, item) {\r
734                 if (arguments.length == 1) item = key;\r
735                 item = this.constructor.create.apply(this.constructor, arguments);\r
736                 return this.base(key, item);\r
737         },\r
738 \r
739         storeAt: function(index, item) {\r
740                 //-dean: get rid of this?\r
741                 assert(index < this.count(), "Index out of bounds.");\r
742                 arguments[0] = this[KEYS][index];\r
743                 return this.store.apply(this, arguments);\r
744         }\r
745 }, {\r
746         Item: null, // if specified, all members of the Collection must be instances of Item\r
747         \r
748         create: function(key, item) {\r
749                 if (this.Item && !instanceOf(item, this.Item)) {\r
750                         item = new this.Item(key, item);\r
751                 }\r
752                 return item;\r
753         },\r
754         \r
755         extend: function(_instance, _static) {\r
756                 var klass = this.base(_instance);\r
757                 klass.create = this.create;\r
758                 extend(klass, _static);\r
759                 if (!klass.Item) {\r
760                         klass.Item = this.Item;\r
761                 } else if (typeof klass.Item != "function") {\r
762                         klass.Item = (this.Item || Base).extend(klass.Item);\r
763                 }\r
764                 if (typeof klass.init == "function") klass.init();\r
765                 return klass;\r
766         }\r
767 });\r
768 \r
769 // =========================================================================\r
770 // base2/RegGrp.js\r
771 // =========================================================================\r
772 \r
773 var RegGrp = Collection.extend({\r
774         constructor: function(values, flags) {\r
775                 this.base(values);\r
776                 if (typeof flags == "string") {\r
777                         this.global = /g/.test(flags);\r
778                         this.ignoreCase = /i/.test(flags);\r
779                 }\r
780         },\r
781 \r
782         global: true, // global is the default setting\r
783         ignoreCase: false,\r
784 \r
785         exec: function(string, replacement) {\r
786                 if (arguments.length == 1) {\r
787                         var keys = this[KEYS];\r
788                         var values = this[VALUES];\r
789                         replacement = function(match) {\r
790                                 if (!match) return "";\r
791                                 var offset = 1, i = 0;\r
792                                 // loop through the values\r
793                                 while (match = values[HASH + keys[i++]]) {\r
794                                         // do we have a result?\r
795                                         if (arguments[offset]) {\r
796                                                 var replacement = match.replacement;\r
797                                                 switch (typeof replacement) {\r
798                                                         case "function":\r
799                                                                 return replacement.apply(null, slice(arguments, offset));\r
800                                                         case "number":\r
801                                                                 return arguments[offset + replacement];\r
802                                                         default:\r
803                                                                 return replacement;\r
804                                                 }\r
805                                         // no? then skip over references to sub-expressions\r
806                                         } else offset += match.length + 1;\r
807                                 }\r
808                         };\r
809                 }\r
810                 var flags = (this.global ? "g" : "") + (this.ignoreCase ? "i" : "");\r
811                 return String(string).replace(new RegExp(this, flags), replacement);\r
812         },\r
813 \r
814         test: function(string) {\r
815                 return this.exec(string) != string;\r
816         },\r
817         \r
818         toString: function() {\r
819                 var length = 0;\r
820                 return "(" + this.map(function(item) {\r
821                         // fix back references\r
822                         var expression = String(item).replace(/\\(\d+)/g, function($, index) {\r
823                                 return "\\" + (1 + Number(index) + length);\r
824                         });\r
825                         length += item.length + 1;\r
826                         return expression;\r
827                 }).join(")|(") + ")";\r
828         }\r
829 }, {\r
830         IGNORE: "$0",\r
831         \r
832         init: function() {\r
833                 forEach ("add,exists,fetch,remove,store".split(","), function(name) {\r
834                         extend(this, name, function(expression) {\r
835                                 if (instanceOf(expression, RegExp)) {\r
836                                         expression = expression.source;\r
837                                 }\r
838                                 return base(this, arguments);\r
839                         });\r
840                 }, this.prototype);\r
841         }\r
842 });\r
843 \r
844 // =========================================================================\r
845 // base2/RegGrp/Item.js\r
846 // =========================================================================\r
847 \r
848 RegGrp.Item = Base.extend({\r
849         constructor: function(expression, replacement) {\r
850                 var ESCAPE = /\\./g;\r
851                 var STRING = /(['"])\1\+(.*)\+\1\1$/;\r
852         \r
853                 expression = instanceOf(expression, RegExp) ? expression.source : String(expression);\r
854                 \r
855                 if (typeof replacement == "number") replacement = String(replacement);\r
856                 else if (replacement == null) replacement = "";\r
857                 \r
858                 // count the number of sub-expressions\r
859                 //  - add one because each pattern is itself a sub-expression\r
860                 this.length = match(expression.replace(ESCAPE, "").replace(/\[[^\]]+\]/g, ""), /\(/g).length;\r
861                 \r
862                 // does the pattern use sub-expressions?\r
863                 if (typeof replacement == "string" && /\$(\d+)/.test(replacement)) {\r
864                         // a simple lookup? (e.g. "$2")\r
865                         if (/^\$\d+$/.test(replacement)) {\r
866                                 // store the index (used for fast retrieval of matched strings)\r
867                                 replacement = parseInt(replacement.slice(1));\r
868                         } else { // a complicated lookup (e.g. "Hello $2 $1")\r
869                                 // build a function to do the lookup\r
870                                 var i = this.length + 1;\r
871                                 var Q = /'/.test(replacement.replace(ESCAPE, "")) ? '"' : "'";\r
872                                 replacement = replacement.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\$(\d+)/g, Q +\r
873                                         "+(arguments[$1]||" + Q+Q + ")+" + Q);\r
874                                 replacement = new Function("return " + Q + replacement.replace(STRING, "$1") + Q);\r
875                         }\r
876                 }\r
877                 this.replacement = replacement;\r
878                 this.toString = function() {\r
879                         return expression || "";\r
880                 };\r
881         },\r
882         \r
883         length: 0,\r
884         replacement: ""\r
885 });\r
886 \r
887 // =========================================================================\r
888 // base2/Namespace.js\r
889 // =========================================================================\r
890 \r
891 var Namespace = Base.extend({\r
892         constructor: function(_private, _public) {\r
893                 this.extend(_public);\r
894                 this.toString = function() {\r
895                         return format("[base2.%1]", this.name);\r
896                 };\r
897                 \r
898                 // initialise\r
899                 if (typeof this.init == "function") this.init();\r
900                 \r
901                 if (this.name != "base2") {\r
902                         this.namespace = format("var %1=base2.%1;", this.name);\r
903                 }\r
904                 \r
905                 var namespace = "var base=" + base + ";";\r
906                 var imports = ("base2,lang," + this.imports).split(",");\r
907                 _private.imports = Enumerable.reduce(imports, namespace, function(namespace, name) {\r
908                         if (base2[name]) namespace += base2[name].namespace;\r
909                         return namespace;\r
910                 });\r
911                 \r
912                 var namespace = format("base2.%1=%1;", this.name);\r
913                 var exports = this.exports.split(",");\r
914                 _private.exports = Enumerable.reduce(exports, namespace, function(namespace, name) {\r
915                         if (name) {\r
916                                 this.namespace += format("var %2=%1.%2;", this.name, name);\r
917                                 namespace += format("if(!%1.%2)%1.%2=%2;base2.%2=%1.%2;", this.name, name);\r
918                         }\r
919                         return namespace;\r
920                 }, this);\r
921                 \r
922                 if (this.name != "base2") {\r
923                         base2.namespace += format("var %1=base2.%1;", this.name);\r
924                 }\r
925         },\r
926 \r
927         exports: "",\r
928         imports: "",\r
929         namespace: "",\r
930         name: ""\r
931 });\r
932 \r
933 base2 = new Namespace(this, {\r
934         name:    "base2",\r
935         version: "0.8 (alpha)",\r
936         exports: "Base,Abstract,Module,Enumerable,Array2,Hash,Collection,RegGrp,Namespace"\r
937 });\r
938 \r
939 base2.toString = function() {\r
940         return "[base2]";\r
941 };\r
942 \r
943 eval(this.exports);\r
944 \r
945 // =========================================================================\r
946 // base2/lang/namespace.js\r
947 // =========================================================================\r
948 \r
949 var lang = new Namespace(this, {\r
950         name:    "lang",\r
951         version: base2.version,\r
952         exports: "K,assert,assertType,assignID,instanceOf,extend,format,forEach,match,rescape,slice,trim",\r
953         \r
954         init: function() {\r
955                 this.extend = extend;\r
956                 // add the Enumerable methods to the lang object\r
957                 forEach (Enumerable.prototype, function(method, name) {\r
958                         if (!Module[name]) {\r
959                                 this[name] = function() {\r
960                                         return Enumerable[name].apply(Enumerable, arguments);\r
961                                 };\r
962                                 this.exports += "," + name;\r
963                         }\r
964                 }, this);\r
965         }\r
966 });\r
967 \r
968 eval(this.exports);\r
969 \r
970 base2.namespace += lang.namespace;\r
971 \r
972 }; ////////////////////  END: CLOSURE  /////////////////////////////////////\r