1 // timestamp: Tue, 24 Apr 2007 09:57:15
\r
3 base2.js - copyright 2007, Dean Edwards
\r
4 http://www.opensource.org/licenses/mit-license
\r
9 // You know, writing a javascript library is awfully time consuming.
\r
11 new function(_) { //////////////////// BEGIN: CLOSURE ////////////////////
\r
13 // =========================================================================
\r
15 // =========================================================================
\r
19 var Base = function() {
\r
20 // call this method from any other method to invoke that method's ancestor
\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
37 value.method = method;
\r
38 value.ancestor = ancestor;
\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
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
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
63 Base.extend = function(_instance, _static) { // subclass
\r
64 var extend = Base.prototype.extend;
\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
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
81 var object = arguments[0];
\r
82 if (object != null) {
\r
83 (object.extend || extend).call(object, proto);
\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
103 Base = Base.extend({
\r
104 constructor: function() {
\r
105 this.extend(arguments[0]);
\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
116 // add the interface using the extend() method
\r
117 this.prototype.extend(_interface);
\r
123 // =========================================================================
\r
125 // =========================================================================
\r
127 var Legacy = typeof $Legacy == "undefined" ? {} : $Legacy;
\r
129 var K = function(k) {return k};
\r
131 var assert = function(condition, message, Err) {
\r
133 throw new (Err || Error)(message || "Assertion failed.");
\r
137 var assertType = function(object, type, message) {
\r
139 var condition = typeof type == "function" ? instanceOf(object, type) : typeof object == type;
\r
140 assert(condition, message || "Invalid type.", TypeError);
\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
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
168 return typeof object == typeof klass.prototype.valueOf();
\r
170 // this is the only troublesome one
\r
171 return !!(object.join && object.splice && !arguments.callee(object, Function));
\r
173 return !!object.getTimezoneOffset;
\r
175 return String(object.constructor.prototype) == String(new RegExp);
\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
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
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
198 var TRIM = /^\s+|\s+$/g;
\r
199 var trim = function(string) {
\r
200 return String(string).replace(TRIM, "");
\r
203 // =========================================================================
\r
205 // =========================================================================
\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
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
217 // =========================================================================
\r
218 // lang/assignID.js
\r
219 // =========================================================================
\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
228 // =========================================================================
\r
230 // =========================================================================
\r
232 if (typeof StopIteration == "undefined") {
\r
233 StopIteration = new Error("StopIteration");
\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
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
245 } else if (typeof object.length == "number") {
\r
246 // the object is array-like
\r
247 forEach.Array(object, block, context);
\r
250 forEach.Function(fn || Object, object, block, context);
\r
253 // these are the two core enumeration methods. all other forEach methods
\r
254 // eventually call one of these two.
\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
263 for (i = 0; i < length; i++) {
\r
264 block.call(context, array[i], i, array);
\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
278 // =========================================================================
\r
279 // base2/Base/forEach.js
\r
280 // =========================================================================
\r
282 Base.forEach = function(object, block, context) {
\r
283 forEach.Function(this, object, block, context);
\r
286 // =========================================================================
\r
287 // base2/../Function.js
\r
288 // =========================================================================
\r
290 // some browsers don't define this
\r
292 Function.prototype.prototype = {};
\r
295 // =========================================================================
\r
296 // base2/../String.js
\r
297 // =========================================================================
\r
299 // fix String.replace (Safari/IE5.0)
\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
311 regexp = new RegExp(rescape(expression));
\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
319 return result + string;
\r
321 return base(this, arguments);
\r
326 // =========================================================================
\r
327 // base2/Abstract.js
\r
328 // =========================================================================
\r
330 var Abstract = Base.extend({
\r
331 constructor: function() {
\r
332 throw new TypeError("Class cannot be instantiated.");
\r
336 // =========================================================================
\r
338 // =========================================================================
\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
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
349 // Modules cannot be instantiated. Static properties and methods are inherited.
\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
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
370 implement: function(_interface) {
\r
371 // implement an interface on BOTH the instance and static interfaces
\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
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
390 if (!Module[name]) extend(this, name, property);
\r
391 }, module.prototype);
\r
392 // add the static interface
\r
393 extend(module, _interface);
\r
400 // =========================================================================
\r
401 // base2/Enumerable.js
\r
402 // =========================================================================
\r
404 var Enumerable = Module.extend({
\r
405 every: function(object, test, context) {
\r
408 this.forEach (object, function(value, key) {
\r
409 result = test.call(context, value, key, object);
\r
410 if (!result) throw StopIteration;
\r
413 if (error != StopIteration) throw error;
\r
415 return !!result; // cast to boolean
\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
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
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
445 pluck: function(object, key) {
\r
446 return this.map(object, function(item) {
\r
447 if (item != null) return item[key];
\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
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
467 // =========================================================================
\r
469 // =========================================================================
\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
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
484 copy: function(array) {
\r
485 return this.concat(array);
\r
488 contains: function(array, item) {
\r
489 return this.indexOf(array, item) != -1;
\r
492 forEach: forEach.Array,
\r
494 indexOf: function(array, item, fromIndex) {
\r
495 var length = array.length;
\r
496 if (fromIndex == null) {
\r
498 } else if (fromIndex < 0) {
\r
499 fromIndex = Math.max(0, length + fromIndex);
\r
501 for (var i = fromIndex; i < length; i++) {
\r
502 if (array[i] === item) return i;
\r
507 insertAt: function(array, item, index) {
\r
508 this.splice(array, index, 0, item);
\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
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
526 for (var i = fromIndex; i >= 0; i--) {
\r
527 if (array[i] === item) return i;
\r
532 remove: function(array, item) {
\r
533 var index = this.indexOf(array, item);
\r
534 if (index != -1) this.removeAt(array, index);
\r
538 removeAt: function(array, index) {
\r
539 var item = array[index];
\r
540 this.splice(array, index, 1);
\r
545 IArray.prototype.forEach = function(block, context) {
\r
546 forEach.Array(this, block, context);
\r
549 IArray.implement(Enumerable);
\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
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
561 // expose IArray.prototype so that it can be extended
\r
562 Array2.prototype = IArray.prototype;
\r
564 forEach (IArray, function(method, name, proto) {
\r
566 IArray[name] = Array[name];
\r
567 delete IArray.prototype[name];
\r
569 Array2[name] = IArray[name];
\r
572 // =========================================================================
\r
574 // =========================================================================
\r
576 var HASH = "#" + Number(new Date);
\r
577 var KEYS = HASH + "keys";
\r
578 var VALUES = HASH + "values";
\r
580 var Hash = Base.extend({
\r
581 constructor: function(values) {
\r
582 this[KEYS] = new Array2;
\r
584 this.merge(values);
\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
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
601 fetch: function(key) {
\r
602 return this[VALUES][HASH + key];
\r
605 forEach: function(block, context) {
\r
606 forEach (this[KEYS], function(key) {
\r
607 block.call(context, this.fetch(key), key, this);
\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
620 merge: function(values) {
\r
621 forEach (arguments, function(values) {
\r
622 forEach (values, function(value, key) {
\r
623 this.store(key, value);
\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
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
642 // create the new entry (or overwrite the old entry)
\r
643 this[VALUES][HASH + key] = value;
\r
647 toString: function() {
\r
648 return String(this[KEYS]);
\r
651 union: function(values) {
\r
652 return this.merge.apply(this.copy(), arguments);
\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
665 Hash.implement(Enumerable);
\r
667 // =========================================================================
\r
668 // base2/Collection.js
\r
669 // =========================================================================
\r
671 // A Hash that is more array-like (accessible by index).
\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
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
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
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
696 count: function() {
\r
697 return this[KEYS].length;
\r
700 indexOf: function(key) {
\r
701 return this[KEYS].indexOf(String(key));
\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
710 item: function(index) {
\r
711 return this.fetch(this[KEYS][index]);
\r
714 removeAt: function(index) {
\r
715 return this.remove(this[KEYS][index]);
\r
718 reverse: function() {
\r
719 this[KEYS].reverse();
\r
723 sort: function(compare) {
\r
726 this[KEYS].sort(function(key1, key2) {
\r
727 return compare(self.fetch(key1), self.fetch(key2), key1, key2);
\r
729 } else this[KEYS].sort();
\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
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
746 Item: null, // if specified, all members of the Collection must be instances of Item
\r
748 create: function(key, item) {
\r
749 if (this.Item && !instanceOf(item, this.Item)) {
\r
750 item = new this.Item(key, item);
\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
760 klass.Item = this.Item;
\r
761 } else if (typeof klass.Item != "function") {
\r
762 klass.Item = (this.Item || Base).extend(klass.Item);
\r
764 if (typeof klass.init == "function") klass.init();
\r
769 // =========================================================================
\r
771 // =========================================================================
\r
773 var RegGrp = Collection.extend({
\r
774 constructor: function(values, flags) {
\r
776 if (typeof flags == "string") {
\r
777 this.global = /g/.test(flags);
\r
778 this.ignoreCase = /i/.test(flags);
\r
782 global: true, // global is the default setting
\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
799 return replacement.apply(null, slice(arguments, offset));
\r
801 return arguments[offset + replacement];
\r
803 return replacement;
\r
805 // no? then skip over references to sub-expressions
\r
806 } else offset += match.length + 1;
\r
810 var flags = (this.global ? "g" : "") + (this.ignoreCase ? "i" : "");
\r
811 return String(string).replace(new RegExp(this, flags), replacement);
\r
814 test: function(string) {
\r
815 return this.exec(string) != string;
\r
818 toString: function() {
\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
825 length += item.length + 1;
\r
827 }).join(")|(") + ")";
\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
838 return base(this, arguments);
\r
840 }, this.prototype);
\r
844 // =========================================================================
\r
845 // base2/RegGrp/Item.js
\r
846 // =========================================================================
\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
853 expression = instanceOf(expression, RegExp) ? expression.source : String(expression);
\r
855 if (typeof replacement == "number") replacement = String(replacement);
\r
856 else if (replacement == null) replacement = "";
\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
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
877 this.replacement = replacement;
\r
878 this.toString = function() {
\r
879 return expression || "";
\r
887 // =========================================================================
\r
888 // base2/Namespace.js
\r
889 // =========================================================================
\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
899 if (typeof this.init == "function") this.init();
\r
901 if (this.name != "base2") {
\r
902 this.namespace = format("var %1=base2.%1;", this.name);
\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
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
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
922 if (this.name != "base2") {
\r
923 base2.namespace += format("var %1=base2.%1;", this.name);
\r
933 base2 = new Namespace(this, {
\r
935 version: "0.8 (alpha)",
\r
936 exports: "Base,Abstract,Module,Enumerable,Array2,Hash,Collection,RegGrp,Namespace"
\r
939 base2.toString = function() {
\r
943 eval(this.exports);
\r
945 // =========================================================================
\r
946 // base2/lang/namespace.js
\r
947 // =========================================================================
\r
949 var lang = new Namespace(this, {
\r
951 version: base2.version,
\r
952 exports: "K,assert,assertType,assignID,instanceOf,extend,format,forEach,match,rescape,slice,trim",
\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
962 this.exports += "," + name;
\r
968 eval(this.exports);
\r
970 base2.namespace += lang.namespace;
\r
972 }; //////////////////// END: CLOSURE /////////////////////////////////////
\r