enumerable-js
Version:
Utility functions for all types of collections
1 lines • 3.77 kB
JavaScript
(function(){"use strict";var e=this;var t=function(e){this.result=e};var n=e.enumerable;var r={};r.VERSION="0.0.1";r.noConflict=function(){e.enumerable=n;return r};r.reduce=r.inject=r.foldl=function(t,n,r){r=r||this;this.each(function(e){n=t.call(r,n,e)});return n};r.filter=r.select=r.findAll=function(t,n){n=n||this;var r=[];this.each(function(e){if(t.call(n,e)){r.push(e)}});return r};r.find=function(n,r){r=r||this;var i=null;try{this.each(function(e){if(n.call(r,e)){i=e;throw new t}})}catch(s){if(!(s instanceof t)){throw s}}return i};r.count=function(t,n){n=n||this;var r=0;var i=typeof t==="function"?t:function(e){return e===t};this.each(function(e){t?i.call(n,e)&&r++:r++});return r};r.all=function(n,r){r=r||this;try{this.each(function(e){if(!n.call(r,e)){throw new t}})}catch(i){if(i instanceof t){return false}else{throw i}}return true};r.any=function(t,n){n=n||this;return!this.all(function(e){return!t.call(n,e)})};r.mapInPlace=function(t,n){n=n||this;this.each(function(e){e=t.call(n,e)});return this};r.mapToArray=function(t,n){n=n||this;var r=[];this.each(function(e){r.push(t.call(n,e))});return r};r.extend=function(t,n){if(!n){this.extend(this,t)}else{for(var r in t){n[r]=t[r]}}};r.toArray=function(){return this.mapToArray(function(e){return e})};r.cycle=function(t,n,r){r=r||this;if(typeof n!=="number"){throw new SyntaxError("need a number passed in as the second argument")}n=Math.floor(Math.abs(n));while(n){this.each.call(r,t);n--}return this};r.detect=function(t,n){n=n||this;return this.find(function(e){return!t.call(n,e)})};r.eachCons=function(n,r,i){i=i||this;var s=0;var o=this;try{this.each(function(){var e=[];o.eachUntilN(function(t){e.push(t)},r,this,s);if(e.length!==r){throw new t}n.call(i,e);s++})}catch(u){if(!(u instanceof t)){throw u}}return this};r.eachSlice=function(t,n,r){if(typeof n!=="number"){throw new SyntaxError("need a number passed in as a second argument")}r=r||this;var i=[];this.each(function(e){i.push(e);if(i.length==n){t.call(r,i);i=[]}});if(i.length){t.call(r,i)}return this};r.eachUntilN=function(n,r,i,s){s=s||0;i=i||this;var o=-1;if(typeof r!=="number"||r<0){throw new TypeError("need a positive integer number as a second argument")}try{this.each(function(e){o++;if(o<s){return}n.call(i,e);r--;if(r===0){throw new t}})}catch(u){if(!(u instanceof t)){throw u}}return this};r.first=function(n){var r=0;var i;try{if(typeof n!=="number"){return this.each(function(e){i=e;throw new t})}else{i=[];this.each(function(e){i.push(e);if(i.length==n){throw new t}})}}catch(s){if(!(s instanceof t)){throw s}}return i};r.max=function(t,n){if(typeof t!=="function"){throw new SyntaxError("need a callback argument")}n=n||this;var r,i,s;this.each(function(e){s=t.call(n,e);if(typeof r==="undefined"||s>i){r=e;i=s}});return r};r.min=function(t,n){if(typeof t!=="function"){throw new SyntaxError("need a callback argument")}n=n||this;var r,i,s;this.each(function(e){s=t.call(n,e);if(typeof r==="undefined"||s<i){r=e;i=s}});return r};r.none=function(t,n){n=n||this;return this.all(function(e){return!t.call(n,e)})};r.hasN=function(n,r,i){i=i||this;r=r||1;try{this.each(function(e){if(n.call(i,e)){r--}if(r<0){throw new t}})}catch(s){if(!(s instanceof t)){throw s}}return r==0};r.partition=function(t,n){var r=[];var i=[];this.each(function(e){if(t.call(n,e)){r.push(e)}else{i.push(e)}});return[r,i]};r.reverseEach=function(t,n){var r=this.toArray();for(var i=r.length-1;i>=0;i--){t.call(n,r[i])}return this};r.sort=function(e,t){return this.toArray().sort(function(n,r){var i=e.call(t,n);var s=e.call(t,r);if(i>s){return 1}else if(i<s){return-1}else{return 0}})};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=r}exports.enumerable=r}else{e.enumerable=r}}).call(this)