UNPKG

curray

Version:

Curray is an extension library for the native JavaScript Array object.

8 lines (7 loc) 5.54 kB
/*! * curray v1.0.12 * (c) Roger Far * Released under the MIT License. */ !function(t){"function"==typeof define&&define.amd?define(t):t()}((function(){"use strict";var t=function(r,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])},t(r,n)};"function"==typeof SuppressedError&&SuppressedError;var r=function(t,r){return function(n,e){return t(n,e)||r(n,e)}},n=function(t,r){return void 0===r&&(r=!1),function(n,e){var o=t(n),i=t(e);return o>i?r?-1:1:o<i?r?1:-1:0}},e=function(e){function o(t,r){var n=e.apply(this,t)||this;return n._comparer=r,Object.setPrototypeOf(n,o.prototype),n.sort(n._comparer),n}return function(r,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function e(){this.constructor=r}t(r,n),r.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}(o,e),Object.defineProperty(o,Symbol.species,{get:function(){return Array},enumerable:!1,configurable:!0}),o.prototype.thenBy=function(t){return new o(this,r(this._comparer,n(t,!1)))},o.prototype.thenByDescending=function(t){return new o(this,r(this._comparer,n(t,!0)))},o}(Array);Array.prototype.addRange=function(t){this.push.apply(this,t)},Array.prototype.aggregate=function(t,r){return this.reduce(t,r)},Array.prototype.all=function(t){return this.every(t)},Array.prototype.any=function(t){return t?this.some(t):this.length>0},Array.prototype.average=function(t){return this.sum(t)/this.length},Array.prototype.cast=function(){return this},Array.prototype.contains=function(t){return this.some((function(r){return r===t}))},Array.prototype.count=function(t){return t?this.where(t).count():this.length},Array.prototype.defaultIfEmpty=function(t){return this.count()?this:[t]},Array.prototype.distinctBy=function(t){var r=this.groupBy(t);return Object.keys(r).reduce((function(t,n){var e=r[n];return null!=e&&e.length>0&&t.push(e[0]),t}),new Array)},Array.prototype.elementAt=function(t){if(t<this.count()&&t>=0)return this[t];throw new Error("ArgumentOutOfRangeException: index is less than 0 or greater than or equal to the number of elements in source.")},Array.prototype.elementAtOrDefault=function(t){return void 0!==this.elementAt(t)&&this.elementAt(t)},Array.prototype.except=function(t){return this.where((function(r){return!t.contains(r)}))},Array.prototype.first=function(t){if(this.count())return t?this.where(t).first():this[0];throw new Error("InvalidOperationException: The source sequence is empty.")},Array.prototype.firstOrDefault=function(t){return this.count(t)?this.first(t):null},Array.prototype.groupBy=function(t,r){var n=null!=r?r:function(t){return t};return this.aggregate((function(r,e){var o=t(e),i=r[o],u=n(e);return i?i.push(u):r[o]=[u],r}),{})},Array.prototype.groupJoin=function(t,r,n,e){return this.select((function(o){return e(o,t.where((function(t){return r(o)===n(t)})))}))},Array.prototype.intersect=function(t){return this.where((function(r){return t.contains(r)}))},Array.prototype.last=function(t){if(this.count())return t?this.where(t).last():this[this.count()-1];throw Error("InvalidOperationException: The source sequence is empty.")},Array.prototype.lastOrDefault=function(t){return this.count(t)?this.last(t):null},Array.prototype.max=function(t){return Math.max.apply(Math,this.map(t||function(t){return t}))},Array.prototype.min=function(t){return Math.min.apply(Math,this.map(t||function(t){return t}))},Array.prototype.ofType=function(t){var r;switch(t){case Number:r="number";break;case String:r="string";break;case Boolean:r="boolean";break;case Function:r="function";break;default:r=null}return null===r?this.where((function(r){return r instanceof t})).cast():this.where((function(t){return typeof t===r})).cast()},Array.prototype.orderBy=function(t){return new e(this,n(t,!1))},Array.prototype.orderByDescending=function(t){return new e(this,n(t,!0))},Array.prototype.removeAll=function(t){return this.where(function(t){return function(r,n,e){return!t(r,n,e)}}(t))},Array.prototype.select=function(t){return this.map(t)},Array.prototype.selectMany=function(t){return this.reduce((function(r,n,e){return r.concat(t(n,e))}),[])},Array.prototype.sequenceEqual=function(t){return!!this.reduce((function(r,n,e){return t[e]===n?r:void 0}))},Array.prototype.single=function(t){if(1!==this.count(t))throw new Error("InvalidOperationException: The collection does not contain exactly one element.");return this.first(t)},Array.prototype.singleOrDefault=function(t){return this.count(t)?this.single(t):null},Array.prototype.skip=function(t){return this.slice(Math.max(0,t))},Array.prototype.skipWhile=function(t){var r=this;return this.skip(this.aggregate((function(n){return t(r.elementAt(n))?++n:n}),0))},Array.prototype.sum=function(t){return t?this.select(t).sum():this.aggregate((function(t,r){return t+ +r}),0)},Array.prototype.take=function(t){return this.slice(0,Math.max(0,t))},Array.prototype.takeWhile=function(t){var r=this;return this.take(this.aggregate((function(n){return t(r.elementAt(n))?++n:n}),0))},Array.prototype.union=function(t){return this.concat(t).distinctBy((function(t){return t}))},Array.prototype.where=function(t){return this.filter(t)},Array.prototype.zip=function(t,r){var n=this;return t.count()<this.count()?t.select((function(t,e){return r(n.elementAt(e),t)})):this.select((function(n,e){return r(n,t.elementAt(e))}))}})); //# sourceMappingURL=index.umd.min.js.map