// {{{ Basis Prototype Extensions. String.prototype.toJSON = function() { eval('var tmp = '+this+';'); return tmp; }; String.prototype.alert = function() { alert(this.valueOf()); return this; }; String.prototype.log = function() { console.log(this.valueOf()); return this; }; String.prototype.sprintf = function() { var result = this; arguments.each(function(){ result = result.replace(/%[sd]/,this); }); return result; }; String.prototype.wrap = function(before, after){ if(after == null) return before.replace('|',this.valueOf()); return before + this + after; }; String.prototype.insertInto = function(node){ assert(typeof node == 'object', 'insertInto: wrong parameter.'); if(node.constructor === $().constructor) { node.html(this.valueOf()); } else { node.innerHTML = (this.valueOf()); } return this; }; String.prototype.hash = function() { // a checksum for a string // DEPRECATED! <- DANGEROUS var l = this.length, sum = 0, rand = [2,1,5,3,6,4,7,9,8], rand2 = [1, 1, 1, 2]; for(var i = 0; i 'Hello' Array.prototype.mapTo = function(s) { var self = this, map = {}; s.split('|').each(function(i){ map[this] = self[i]; }); return map; }; Array.prototype.fold = function(template) { var len = this.length; var str = ''; for (var i=0 ; i= maxStages)) { return out; } offset += ' '; var cnt = 1, index; for(index in obj) { if(!(obj instanceof Array && !index.match(/^[0-9]+$/) ) ) // suppress showing Array prototype extensions out += '\n' + offset + '['+index+'] => '+_dump(obj[index], offset, stage + 1); if(maxPerStage) if(cnt >= (maxPerStage+1)) return out + '\n' + offset + '...\n'; cnt++; } return out + '\n'; default: return typeof(obj); } } } function merge(a, b) { function _merge(a, b){ if(b === null) return null; var i; if(typeof(b) == 'object' && b.constructor != Array) { if(typeof(a) != 'object') a = {}; for(i in b) { a[i] = _merge(a[i], b[i]); } } else { a = b; } return a; } return _merge(a, b); } function assert(bCondition, sErrorMessage) { if(!bCondition) { console.error('assertion failed:\n' + sErrorMessage); assertion_failed; // <- deliberately not defined throw new Error(sErrorMessage); } } // }}}