-
Notifications
You must be signed in to change notification settings - Fork 13
/
Arrays.min.js
1 lines (1 loc) · 6.33 KB
/
Arrays.min.js
1
Array.prototype.add=function(r){var t=this;return t.push(r),t},Array.prototype.addif=function(r,t){var n=this;if(t)if(n.contains(r[t],t)){var e=n.where(t,r[t]);e.count()>0&&(e[0]=r)}else n.add(r);else n.contains(r)||n.add(r);return n},Array.prototype.toggle=function(r,t){var n=this;return t?n.contains(r[t],t)?n.remove(r[t],t):n.add(r):n.contains(r)?n.remove(r):n.add(r),n},Array.prototype.addRange=function(r){for(var t=this,n=0;n<r.length;n++)t.add(r[n]);return t},Array.prototype.from=function(r){var t=this,n=[];r=r||0;for(var e=r;e<t.length;e++)n.push(t[e]);return n},Array.prototype.first=function(r){var t=this.valueOf();if(void 0==r)return t.length>0?t[0]:{};for(var n=[],e=0;e<Math.min(r,t.count());e++)n.add(t[e]);return n},Array.prototype.last=function(r){var t=this;if(void 0==r)return t.length>0?t[t.length-1]:{};for(var n=[],e=Math.max(t.length-r,0);e<t.length;e++)n.add(t[e]);return n},Array.prototype.selectWhere=function(r,t){for(var n=this,e=[],o=0;o<n.length;o++)n[o][r]==t&&e.add(n[o]);return e},Array.prototype.where=function(r,t){var n=this;return"string"==typeof r?n.selectWhere(r,t):"function"==typeof r?n.selectByFunction(r):void 0},Array.prototype.selectWheres=function(r){for(var t=this,n=[],e=0;e<t.length;e++){for(var o=!0,a=0;a<r.length;a++){var i=r[a].prop,u=r[a].val;t[e][i]!=u&&(o=!1)}o&&n.add(t[e])}return n},Array.prototype.selectByFunction=function(r){for(var t=this,n=[],e=0;e<t.length;e++)r(e)&&n.add(t[e]);return n},Array.prototype.sort=function(r){var t,n=this.valueOf();do{t=!1;for(var e,o=0;o<n.length-1;o++)if(e=void 0==r||"asc"==r?n[o]>n[o+1]:n[o]<n[o+1]){var a=n[o];n[o]=n[o+1],n[o+1]=a,t=!0}}while(t);return n},Array.prototype.isort=function(r){var t=this.valueOf();t.length>1&&t.push(t[0]);for(var n=[],e=0;e<t.length;e++){for(var o=0,a=!0,i=1;i<n.length;i++){if(n[i]>=t[e]){o=i,a=!1;break}a=!1}0===n.length?n.push(t[e]):0===o||1==a?n.push(t[e]):n=n.insert(t[e],o)}if(n.length>1){n[0];n=n.splice(1,n.length-1)}return n},Array.prototype.bsearch=function(r){var t=this.valueOf();if(0==t.length)return-1;for(var n=0,e=t.length-1;e>n;){var o=Math.floor(Number(n+e)/2);if(r<t[o])e=o;else{if(!(r>t[o]))return o;n=o}}return console.log(t[n]),console.log(n),r==t[n]?n:-1},Array.prototype.sortBy=function(r,t){var n=this;if(!r)return n.sort(t);var e;do{e=!1;for(var o=0;o<n.length-1;o++){var a;if(a=void 0==t||"asc"==t?n[o][r]>n[o+1][r]:n[o][r]<n[o+1][r]){var i=n[o];n[o]=n[o+1],n[o+1]=i,e=!0}}}while(e)},Array.prototype.sortfn=function(r){r=r||function(r,t){return r>t},"function"!=typeof r&&(r=function(r,t){return r>t});var t,n=this;do{t=!1;for(var e=0;e<n.length-1;e++)if(r(n[e],n[e+1])){var o=n[e];n[e]=n[e+1],n[e+1]=o,t=!0}}while(t);return n},Array.prototype.isSorted=function(r){for(var t=this,n=1;n<t.length;n++){var e;if(e=void 0==r||"asc"==r?t[n]<t[n-1]:t[n]>t[n-1])return!1}return!0},Array.prototype.insert=function(r,t){var n=[],e=this;if(void 0==t)return e.push(r),e;if(t>e.count()-1)return e;for(var o=0;t>o;o++)n.add(e[o]);n.add(r);for(var o=t;o<e.length;o++)n.add(e[o]);return e.clear(),e=n,n},Array.prototype.clear=function(){for(var r=this;r.count()>0;)r.pop();return r},Array.prototype.count=function(){return this.length},Array.prototype.indexOfProp=function(r,t){var n=this;if(!t)return n.indexOf(r);for(var e=0;e<n.length;e++)if(n[e][t]==r)return e;return-1},Array.prototype.getAt=function(r){for(var t=this,n=0;n<t.length;n++)if(n==r)return t[n]},Array.prototype.contains=function(r,t){var n=this;if(void 0==t||null==t){for(var e=0;e<n.length;e++)if(n[e]==r)return!0}else for(var e=0;e<n.length;e++)if(n[e][t]==r)return!0;return!1},Array.prototype.distinct=function(r){for(var t=this.valueOf(),n=[],e=0;e<t.length;e++){var o;o=void 0==r?!n.contains(t[e]):!n.contains(t[e][r],r),o&&n.push(t[e])}return n},Array.prototype.remove=function(r,t){var n=this,e=[];if(void 0==t||null==t)for(var o=0;o<n.count();o++)n[o]!=r&&e.push(n[o]);else for(var o=0;o<n.count();o++)n[o][t]!=r&&e.push(n[o]);return n.clear(),n.addRange(e),n},Array.prototype.removeAt=function(r){for(var t=this,n=[],e=0;e<t.length;e++)e!=r&&n.add(t[e]);return t.clear(),t.addRange(n),t},Array.prototype.shuffle=function(){for(var r=this.valueOf(),t=[];t.length<r.length;){var n=r[Math.floor(Number(Math.random()*r.length))];t.indexOf(n)>=0||t.push(n)}return t},Array.prototype.create=function(r){for(var t=[],n=0;n<r.length;n++)t.add(r[n]);return t},Array.prototype.range=function(r,t){var n=[];if(r=r||0,t)for(var e=r;t>=e;e++)n.add(e);else for(var e=0;r>e;e++)n.add(e);return n},Array.range=function(r,t){return[].range(r,t)},Array.prototype.select=function(r){var t=this;if(!r)return this.valueOf();var n=[];return t.selectByFunction(function(e){n.push(t[e][r])}),n},Array.prototype.isEmpty=function(){return 0==this.length},Array.prototype.each=function(r){for(var t=this,n=0;n<t.length;n++)t.index=n,r(t[n]);return delete t.index,t},Array.prototype.flatten=function(){var r=this.valueOf(),t=[];return r.each(function(r){Array.isArray(r)?r.each(function(r){t.add(r)}):t.add(r)}),r.clear(),r=[].create(t)},Array.prototype.string=function(r){var t=this,n="";return r=r||"",t.each(function(t){n+=t+r}),n},Array.prototype.paginate=function(r){r||(r=10);var t=this,n=[],e=0,o=[];return t.each(function(t){r>e?(o.add(t),e+=1):(n.add([].create(o)),o.clear(),o.add(t),e=1)}),o.isEmpty()||n.add([].create(o)),n},Array.prototype.min=function(){var r=this;if(r.isEmpty())return 0;var t=r.first();return r.count()>0?(r.each(function(r){t=t>r?r:t}),t):void 0},Array.prototype.max=function(){var r=this;if(r.isEmpty())return 0;var t=r.first();return r.count()>0?(r.each(function(r){t=r>t?r:t}),t):void 0},Array.prototype.average=function(){var r=this,t=0;return r.each(function(r){Number(r)&&(t+=Number(r))}),t/r.count()},Array.prototype.sum=function(){var r=this,t=0;return r.each(function(r){t+=r}),t},Array.prototype.sync=function(r,t){var n=this;return t?r.each(function(r){n.addif(r,t)}):r.each(function(r){n.addif(r)}),n},Array.prototype.random=function(r){var t=this.valueOf();if(!r||!Number.isSafeInteger(r))return t.shuffle().first();for(var n=0;r>n;n++)return t.shuffle().first(r)},Array.prototype.all=function(r){var t=!0,n=this;return r=r||function(r){return!0},n.each(function(n){0==r(n)&&(t=!1)}),t},Array.prototype.frequency=function(r,t){var n=this;if(t){var e=0;return n.each(function(n){n[t]==r&&e++}),e}var e=0;return n.each(function(t){t==r&&e++}),e},Array.prototype.trim=function(){var r=this,t=[];return r.each(function(r){r&&t.add(r)}),t};