Skip to content

Instantly share code, notes, and snippets.

@giginet
Created November 12, 2011 16:24
Show Gist options
  • Save giginet/1360765 to your computer and use it in GitHub Desktop.
Save giginet/1360765 to your computer and use it in GitHub Desktop.
Array.prototype._index=function(a){var b;if(a<0){b=this.length;return b+a}return a};Array.prototype.at=function(a){return this[this._index(a)]};Array.prototype.map=function(a){var c,b;return([].splice.apply(this,[0,this.length-0].concat(b=(function(){var f,e,d;d=[];for(f=0,e=this.length;f<e;f++){c=this[f];d.push(a(c))}return d}).call(this))),b)};Array.prototype.clone=function(){return this.dup()};Array.prototype.dup=function(){return this.slice(0,this.length)};Array.prototype.each=function(b){var a,c;for(a=0,c=this.length;0<=c?a<c:a>c;0<=c?a++:a--){b(this[a],a)}return this};Array.prototype.deleteAt=function(a){a=this._index(a);if(a>=this.length){return}return this.splice(a,1)};Array.prototype.deleteIf=function(b){var a;return this.replace((function(){var d,c;c=[];for(a=0,d=this.length;0<=d?a<d:a>d;0<=d?a++:a--){if(!b(this[a],a)){c.push(this[a])}}return c}).call(this))};Array.prototype.reject=function(a){var b;b=this.length;this.deleteIf(a);if(b===this.length){return}return this};Array.prototype.isEmpty=function(){return this.length===0};Array.prototype.isEql=function(a,d){var b,c;if(d==null){d=function(f,e){return f===e}}if(this.length!==a.length){return false}for(b=0,c=this.length;0<=c?b<c:b>c;0<=c?b++:b--){if(!d(this[b],a[b])){return false}}return true};Array.prototype.fill=function(d,e,a){var b,c;if(e==null){e=0}if(a==null){a=this.length-1}e=this._index(e);a=this._index(a);[].splice.apply(this,[e,a-e+1].concat(c=(function(){var f;f=[];for(b=e;e<=a?b<=a:b>=a;e<=a?b++:b--){f.push(d)}return f})())),c;return this};Array.prototype.first=function(){return this[0]};Array.prototype.last=function(){var a;a=this._index(-1);return this[a]};Array.prototype.uniq=function(e){var d,c,b,a;if(e==null){e=function(g,f){return g===f}}d=[];for(b=0,a=this.length;b<a;b++){c=this[b];if(!d.isInclude(c,e)){d.push(c)}}return this.replace(d)};Array.prototype.index=function(c,d){var a,b;if(d==null){d=function(f,e){return f===e}}for(a=0,b=this.length;0<=b?a<b:a>b;0<=b?a++:a--){if(d(this[a],c)){return a}}return};Array.prototype.indexes=function(){var c,d,b,a;a=[];for(d=0,b=arguments.length;d<b;d++){c=arguments[d];a.push(this.at(c))}return a};Array.prototype.rindex=function(c,d){var a,b;if(d==null){d=function(f,e){return f===e}}for(a=b=this.length;b<=0?a<0:a>0;b<=0?a++:a--){if(d(this[a],c)){return a}}return};Array.prototype.flatten=function(){return this.replace(this.reduce(function(d,c){return d.concat(c instanceof Array?c.flatten():c)},[]))};Array.prototype.transpose=function(){var e,d,c,b,a,f;b=this.isEmpty()?0:this.length;e=this.first() instanceof Array?this.first().length:0;if(!b||!e){return this}c=(function(){var g;g=[];for(d=0;0<=e?d<e:d>e;0<=e?d++:d--){g.push([])}return g})();for(a=0;0<=b?a<b:a>b;0<=b?a++:a--){for(f=0;0<=e?f<e:f>e;0<=e?f++:f--){c[f][a]=this[a][f]}}return this.replace(c)};Array.prototype.compact=function(){return this.deleteIf(function(a){return a===void 0})};Array.prototype.isInclude=function(d,e){var b,c,a;if(e==null){e=function(g,f){return g===f}}for(c=0,a=this.length;c<a;c++){b=this[c];if(e(b,d)){return true}}return false};Array.prototype.size=function(){return this.length};Array.prototype.swap=function(d,c){var e;d=this._index(d);c=this._index(c);e=this[d];this[d]=this[c];this[c]=e;return this};Array.prototype.shuffle=function(){var a,b;for(a=0,b=this.length;0<=b?a<b:a>b;0<=b?a++:a--){this.swap(a,Math.floor(Math.random()*this.length))}return this};Array.prototype.choice=function(){return this[Math.floor(Math.random()*this.length)]};Array.prototype.count=function(d,e){var a,b,c;if(e==null){e=function(g,f){return g===f}}b=0;for(a=0,c=this.length;0<=c?a<c:a>c;0<=c?a++:a--){if(e(this[a],d)){++b}}return b};Array.prototype.replace=function(a){var c,d,b;this.clear();for(d=0,b=a.length;d<b;d++){c=a[d];this.push(c)}return this};Array.prototype.nitems=function(){return this.clone().compact().size()};Array.prototype.insert=function(){var c,d,b,a,e;c=Array.prototype.slice.call(arguments,0,arguments.length);if(c.size()<=1){return this}b=this._index(c[0]);a=c.slice(1,this.length);for(d=0,e=a.length;0<=e?d<e:d>e;0<=e?d++:d--){this.splice(b+d,0,a[d])}return this};Array.prototype.clear=function(){var a;a=[];while(!this.isEmpty()){a.push(this.deleteAt(0))}return a};Array.prototype.max=function(b){var a;if(b==null){b=function(d,c){if(d===c){return 0}if(d<c){return -1}else{return 1}}}a=this.first();this.reduce(function(d,c){if(b(a,c)<0){return a=c}});return a};Array.prototype.min=function(b){var a;if(b==null){b=function(d,c){if(d===c){return 0}if(d<c){return -1}else{return 1}}}a=this.first();this.reduce(function(d,c){if(b(a,c)>=0){return a=c}});return a};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment