UNPKG

ts-generic-collections-linq

Version:

TypeScript library provides strongly-typed, queryable collections.

2 lines 16.6 kB
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define("ts-generic-collections-linq",["exports"],r):r(t["ts-generic-collections-linq"]={})}(this,function(t){"use strict";var l=(f.prototype.add=function(t){this.list.push(t)},f.prototype.addRange=function(t){var r=this;t.forEach(function(t){return r.add(t)})},f.prototype.remove=function(r){var n=new Array;this.list.forEach(function(t){r(t)||n.push(t)}),this.list=n},f.prototype.removeAt=function(t){this.list.splice(t,1)},f.prototype.clear=function(){this.list=new Array},f.prototype.asEnumerable=function(){return this},Object.defineProperty(f.prototype,"length",{get:function(){return this.list.length},enumerable:!0,configurable:!0}),f.prototype.elementAt=function(t){try{return this.list[t]}catch(r){return null}},f.prototype.any=function(t){if(!t)return 0<this.list.length;for(var r=0;r<this.list.length;r++)if(t(this.list[r]))return!0;return!1},f.prototype.all=function(t){if(!t)return 0<this.list.length;for(var r=0;r<this.list.length;r++)if(!t(this.list[r]))return!1;return!0},f.prototype.single=function(t){if(void 0===t&&(t=null),this.list.length<=0)throw n;if(t){var r=this.singleOrDefault(t);if(!r)throw n;return r}return this.list[0]},f.prototype.first=function(t){if(void 0===t&&(t=null),this.list.length<=0)throw n;if(t){var r=this.firstOrDefault(t);if(!r)throw n;return r}return this.list[0]},f.prototype.last=function(t){if(void 0===t&&(t=null),this.list.length<=0)throw n;if(t){var r=this.lastOrDefault(t);if(!r)throw n;return r}return this.list[this.list.length-1]},f.prototype.singleOrDefault=function(r){var n=new Array;if(this.list.filter(function(t){r(t)&&n.push(t)}),1<n.length)throw o;return n.length<=0?null:n[0]},f.prototype.firstOrDefault=function(t){for(var r=0;r<this.length;r++){var n=this.list[r];if(t(n))return n}return null},f.prototype.lastOrDefault=function(t){for(var r=this.length;0<=r;r--){var n=this.list[r-1];if(t(n))return n}return null},f.prototype.where=function(r){var n=new f;return this.list.filter(function(t){r(t)&&n.add(t)}),n},f.prototype.select=function(r){var n=new f;return this.forEach(function(t){return n.add(r(t))}),n},f.prototype.forEach=function(r){this.list.forEach(function(t){return r(t)})},f.prototype.toArray=function(){return this.list.slice()},f.prototype.join=function(n,e,i,o,s){void 0===s&&(s=!1);var u=new f;return this.list.forEach(function(r){var t=n.toArray().filter(function(t){return e(r)===i(t)});s&&t&&t.length<=0?u.add(o(r,null)):t.forEach(function(t){return u.add(o(r,t))})}),u},f.prototype.groupBy=function(n){var e={};return this.list.forEach(function(t){var r=JSON.stringify(n(t));e[r]=e[r]||[],e[r].push(t)}),new f(Object.keys(e).map(function(t){var r=t.substr(1,t.length-2);return new i(new f(r.split(",")).select(function(t){return t.replace(/^(")?(.*?)(")?$/gi,"$2")}).toArray(),e[t])}))},f.prototype.selectMany=function(e){return this.list.reduce(function(t,r){var n=e(r);return t.addRange(n),t},new f(new Array))},f.prototype.orderBy=function(n){return new f(this.list.sort(function(t,r){return n.compare(t,r)}))},f.prototype.union=function(t){return this.addRange(t.toArray()),this},f.prototype.reverse=function(){return new f(this.list.slice().reverse())},f.prototype.distinct=function(n){var t=new f;return this.forEach(function(r){0<t.length&&t.any(function(t){return n.equals(r,t)})||t.add(r)}),t},f.prototype.skip=function(t){return 0<t?new f(this.list.slice(t,this.list.length-1)):this},f.prototype.take=function(t){return 0<t?new f(this.list.slice(0,t)):this},f.prototype.sum=function(r){var n=0;return this.list.forEach(function(t){return n+=r(t)}),n},f.prototype.avg=function(t){return this.sum(t)/this.length},f.prototype.min=function(n){var e=0,i=0;return this.list.forEach(function(t){if(0==i)e=n(t);else{var r=n(t);r<e&&(e=r)}i++}),e},f.prototype.max=function(n){var e=0,i=0;return this.list.forEach(function(t){if(0==i)e=n(t);else{var r=n(t);e<r&&(e=r)}i++}),e},f.prototype.count=function(r){if(void 0===r&&(r=null),!r)return this.length;var n=0;return this.list.forEach(function(t){r(t)&&n++}),n},f);function f(t){void 0===t&&(t=null),this.list=new Array,t&&(this.list=t)}var i=function k(t,r){this.list=new l,this.groups=t,this.list=new l(r)},e=function(t,r){if("object"!=typeof t&&"function"!=typeof t&&"object"!=typeof r&&"function"!=typeof r)return t===r;for(var n in t){if(t.hasOwnProperty(n)!==r.hasOwnProperty(n))return!1;switch(typeof t[n]){case"object":if(!e(t[n],r[n]))return!1;break;case"function":if("undefined"==typeof r[n]||"compare"!=n&&t[n].toString()!=r[n].toString())return!1;break;default:if(t[n]!=r[n])return!1}}for(var n in r)if("undefined"==typeof t[n])return!1;return!0},n="Item does not exist.",o="Multiple instances of entity found.",s=(u.prototype.add=function(t,r){var n=new h(t,r);if(this.containsKey(t))throw"Duplicate key. Cannot add.";this.list.push(n)},u.prototype.addRange=function(t){var r=this;t.forEach(function(t){return r.add(t.key,t.value)})},u.prototype.removeAt=function(t){this.list.splice(t,1)},u.prototype.clear=function(){this.list=new Array},u.prototype.remove=function(r){var n=new Array;this.list.forEach(function(t){r(t)||n.push(t)}),this.list=n},u.prototype.containsKey=function(r){return this.any(function(t){return e(t.key,r)})},u.prototype.containsValue=function(r){return this.any(function(t){return e(t.value,r)})},u.prototype.tryGetValue=function(r){var t=this.singleOrDefault(function(t){return e(t.key,r)});return t?t.value:null},u.prototype.asEnumerable=function(){return this},Object.defineProperty(u.prototype,"length",{get:function(){return this.list.length},enumerable:!0,configurable:!0}),u.prototype.elementAt=function(t){try{return this.list[t]}catch(r){return null}},u.prototype.any=function(t){if(!t)return 0<this.list.length;for(var r=0;r<this.list.length;r++)if(t(this.list[r]))return!0;return!1},u.prototype.all=function(t){if(!t)return 0<this.list.length;for(var r=0;r<this.list.length;r++)if(!t(this.list[r]))return!1;return!0},u.prototype.single=function(t){if(void 0===t&&(t=null),this.list.length<=0)throw n;if(t){var r=this.singleOrDefault(t);if(!r)throw n;return r}return this.list[0]},u.prototype.first=function(t){if(void 0===t&&(t=null),this.list.length<=0)throw n;if(t){var r=this.firstOrDefault(t);if(!r)throw n;return r}return this.list[0]},u.prototype.last=function(t){if(this.list.length<=0)throw n;if(t){var r=this.lastOrDefault(t);if(!r)throw n;return r}return this.list[this.list.length-1]},u.prototype.singleOrDefault=function(r){var n=new Array;if(this.list.filter(function(t){r(t)&&n.push(t)}),1<n.length)throw o;return n.length<=0?null:n[0]},u.prototype.firstOrDefault=function(t){for(var r=0;r<this.length;r++){var n=this.list[r];if(t(n))return n}return null},u.prototype.lastOrDefault=function(t){for(var r=this.length;0<=r;r--){var n=this.list[r-1];if(t(n))return n}return null},u.prototype.where=function(r){var n=new u;return this.list.filter(function(t){r(t)&&n.add(t.key,t.value)}),n},u.prototype.select=function(r){var n=new l;return this.forEach(function(t){return n.add(r(t))}),n},u.prototype.forEach=function(r){this.list.forEach(function(t){return r(t)})},u.prototype.toArray=function(){return this.list.slice()},u.prototype.join=function(n,e,i,o,s){void 0===s&&(s=!1);var u=new l;return this.list.forEach(function(r){var t=n.toArray().filter(function(t){return e(r)===i(t)});s&&t&&t.length<=0?u.add(o(r,null)):t.forEach(function(t){return u.add(o(r,t))})}),u},u.prototype.groupBy=function(n){var e={};this.list.forEach(function(t){var r=JSON.stringify(n(t));e[r]=e[r]||[],e[r].push(t)});var t=Object.keys(e).map(function(t){var r=t.substr(1,t.length-2);return new i(new l(r.split(",")).select(function(t){return t.replace(/^(")?(.*?)(")?$/gi,"$2")}).toArray(),e[t])});return new l(t)},u.prototype.selectMany=function(e){return this.list.reduce(function(t,r){var n=e(r);return t.addRange(n),t},new l(new Array))},u.prototype.orderBy=function(n){var t=this.list.sort(function(t,r){return n.compare(t,r)});return new l(t)},u.prototype.distinct=function(n){var t=new l;return this.forEach(function(r){0<t.length&&t.any(function(t){return n.equals(r,t)})||t.add(r)}),t},u.prototype.union=function(t){return this.addRange(t.toArray()),this},u.prototype.reverse=function(){return new l(this.list.slice().reverse())},u.prototype.skip=function(t){return 0<t?new u(this.list.slice(t,this.list.length-1)):this},u.prototype.take=function(t){return 0<t?new u(this.list.slice(0,t)):this},u.prototype.sum=function(r){var n=0;return this.list.forEach(function(t){return n+=r(t)}),n},u.prototype.avg=function(t){return this.sum(t)/this.length},u.prototype.min=function(n){var e=0,i=0;return this.list.forEach(function(t){if(0==i)e=n(t);else{var r=n(t);r<e&&(e=r)}i++}),e},u.prototype.max=function(n){var e=0,i=0;return this.list.forEach(function(t){if(0==i)e=n(t);else{var r=n(t);e<r&&(e=r)}i++}),e},u.prototype.count=function(r){if(void 0===r&&(r=null),!r)return this.length;var n=0;return this.list.forEach(function(t){r(t)&&n++}),n},u);function u(t){void 0===t&&(t=null),this.list=new Array,t&&(this.list=t)}var h=function O(t,r){this.key=t,this.value=r},a=(r.prototype.compare=function(t,r){return r<t?1:t==r?0:-1},r);function r(){}var c=(p.prototype.compare=function(t,r){return r<t?1:t==r?0:-1},p);function p(){}var y=(v.prototype.add=function(t,r){var n=this,e=new h(t,r);if(this.containsKey(t))throw"Duplicate key. Cannot add.";this.list.push(e),this.comparer&&(this.list=this.list.sort(function(t,r){return n.comparer.compare(t.key,r.key)}))},v.prototype.addRange=function(t){var r=this;t.forEach(function(t){return r.add(t.key,t.value)})},v.prototype.removeAt=function(t){this.list.splice(t,1)},v.prototype.clear=function(){this.list=new Array},v.prototype.remove=function(r){var n=new Array;this.list.forEach(function(t){r(t)||n.push(t)}),this.list=n},v.prototype.containsKey=function(r){return this.any(function(t){return e(t.key,r)})},v.prototype.containsValue=function(r){return this.any(function(t){return e(t.value,r)})},v.prototype.tryGetValue=function(r){var t=this.singleOrDefault(function(t){return e(t.key,r)});return t?t.value:null},v.prototype.asEnumerable=function(){return this},Object.defineProperty(v.prototype,"length",{get:function(){return this.list.length},enumerable:!0,configurable:!0}),v.prototype.elementAt=function(t){try{return this.list[t]}catch(r){return null}},v.prototype.any=function(t){if(!t)return 0<this.list.length;for(var r=0;r<this.list.length;r++)if(t(this.list[r]))return!0;return!1},v.prototype.all=function(t){if(!t)return 0<this.list.length;for(var r=0;r<this.list.length;r++)if(!t(this.list[r]))return!1;return!0},v.prototype.single=function(t){if(void 0===t&&(t=null),this.list.length<=0)throw n;if(t){var r=this.singleOrDefault(t);if(!r)throw n;return r}return this.list[0]},v.prototype.first=function(t){if(void 0===t&&(t=null),this.list.length<=0)throw n;if(t){var r=this.firstOrDefault(t);if(!r)throw n;return r}return this.list[0]},v.prototype.last=function(t){if(this.list.length<=0)throw n;if(t){var r=this.lastOrDefault(t);if(!r)throw n;return r}return this.list[this.list.length-1]},v.prototype.singleOrDefault=function(r){var n=new Array;if(this.list.filter(function(t){r(t)&&n.push(t)}),1<n.length)throw o;return n.length<=0?null:n[0]},v.prototype.firstOrDefault=function(t){for(var r=0;r<this.length;r++){var n=this.list[r];if(t(n))return n}return null},v.prototype.lastOrDefault=function(t){for(var r=this.length;0<=r;r--){var n=this.list[r-1];if(t(n))return n}return null},v.prototype.where=function(r){var n=new s;return this.list.filter(function(t){r(t)&&n.add(t.key,t.value)}),n},v.prototype.select=function(r){var n=new l;return this.forEach(function(t){return n.add(r(t))}),n},v.prototype.forEach=function(r){this.list.forEach(function(t){return r(t)})},v.prototype.toArray=function(){return this.list.slice()},v.prototype.join=function(n,e,i,o,s){void 0===s&&(s=!1);var u=new l;return this.list.forEach(function(r){var t=n.toArray().filter(function(t){return e(r)===i(t)});s&&t&&t.length<=0?u.add(o(r,null)):t.forEach(function(t){return u.add(o(r,t))})}),u},v.prototype.groupBy=function(n){var e={};this.list.forEach(function(t){var r=JSON.stringify(n(t));e[r]=e[r]||[],e[r].push(t)});var t=Object.keys(e).map(function(t){var r=t.substr(1,t.length-2);return new i(new l(r.split(",")).select(function(t){return t.replace(/^(")?(.*?)(")?$/gi,"$2")}).toArray(),e[t])});return new l(t)},v.prototype.selectMany=function(e){return this.list.reduce(function(t,r){var n=e(r);return t.addRange(n),t},new l(new Array))},v.prototype.orderBy=function(n){var t=this.list.sort(function(t,r){return n.compare(t,r)});return new l(t)},v.prototype.distinct=function(n){var t=new l;return this.forEach(function(r){0<t.length&&t.any(function(t){return n.equals(r,t)})||t.add(r)}),t},v.prototype.union=function(t){return this.addRange(t.toArray()),this},v.prototype.reverse=function(){return new l(this.list.slice().reverse())},v.prototype.skip=function(t){return 0<t?new s(this.list.slice(t,this.list.length-1)):this},v.prototype.take=function(t){return 0<t?new s(this.list.slice(0,t)):this},v.prototype.sum=function(r){var n=0;return this.list.forEach(function(t){return n+=r(t)}),n},v.prototype.avg=function(t){return this.sum(t)/this.length},v.prototype.min=function(n){var e=0,i=0;return this.list.forEach(function(t){if(0==i)e=n(t);else{var r=n(t);r<e&&(e=r)}i++}),e},v.prototype.max=function(n){var e=0,i=0;return this.list.forEach(function(t){if(0==i)e=n(t);else{var r=n(t);e<r&&(e=r)}i++}),e},v.prototype.count=function(r){if(void 0===r&&(r=null),!r)return this.length;var n=0;return this.list.forEach(function(t){r(t)&&n++}),n},v);function v(t,r){void 0===r&&(r=null);var n=this;if(this.list=new Array,r&&(this.list=r),this.comparer=t,this.list&&0<this.list.length)if(this.comparer)this.list.sort(function(t,r){return n.comparer.compare(t.key,r.key)});else{var e=this.list[0].key;"string"==typeof e?(this.comparer=new a,this.list=this.list.sort(function(t,r){return n.comparer.compare(t.key,r.key)})):"number"==typeof e&&(this.comparer=new c,this.list=this.list.sort(function(t,r){return n.comparer.compare(t.key,r.key)}))}}var d=(g.prototype.clear=function(){this.list.clear()},g.prototype.contains=function(r){return this.list.any(function(t){return e(t,r)})},g.prototype.dequeue=function(){if(0<this.list.length){var t=this.list.elementAt(0);return this.list.removeAt(0),t}return null},g.prototype.enqueue=function(t){this.list.add(t)},g.prototype.peek=function(){return 0<this.list.length?this.list.elementAt(0):null},g.prototype.forEach=function(t){this.list.forEach(t)},g.prototype.toArray=function(){return this.list.toArray()},g);function g(t){void 0===t&&(t=null),this.list=new l,t&&(this.list=new l(t))}var w=(m.prototype.clear=function(){this.list.clear()},m.prototype.contains=function(r){return this.list.any(function(t){return e(t,r)})},m.prototype.dequeue=function(){if(0<this.list.length){var t=this.list.length,r=0<=this.peekIndex?this.peekIndex:Math.floor(Math.random()*(t-0))+0,n=this.list.elementAt(r);return this.list.removeAt(r),this.peekIndex=-1,n}return null},m.prototype.enqueue=function(t){this.list.add(t)},m.prototype.peek=function(){if(0<this.list.length){var t=this.list.length;return this.peekIndex=Math.floor(Math.random()*(t-0))+0,this.list.elementAt(this.peekIndex)}return null},m.prototype.forEach=function(t){this.list.forEach(t)},m.prototype.toArray=function(){return this.list.toArray()},m);function m(t){void 0===t&&(t=null),this.list=new l,this.peekIndex=-1,t&&(this.list=new l(t))}var E=(A.prototype.clear=function(){this.list.clear()},A.prototype.contains=function(r){return this.list.any(function(t){return e(t,r)})},A.prototype.pop=function(){if(0<this.list.length){var t=this.list.elementAt(this.list.length-1);return this.list.removeAt(this.list.length-1),t}return null},A.prototype.push=function(t){this.list.add(t)},A.prototype.peek=function(){return 0<this.list.length?this.list.elementAt(this.list.length-1):null},A.prototype.forEach=function(t){this.list.reverse().forEach(t)},A.prototype.toArray=function(){return new l(this.list.toArray()).reverse().toArray()},A);function A(t){void 0===t&&(t=null),this.list=new l,t&&(this.list=new l(t))}t.Group=i,t.objCompare=e,t.ITEM_NOT_FOUND_MSG=n,t.MULTIPLE_INSTANCES_FOUND_MSG=o,t.List=l,t.Dictionary=s,t.KeyValuePair=h,t.SortedDictionary=y,t.Queue=d,t.RandomizedQueue=w,t.Stack=E,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=ts-generic-collections-linq.umd.min.js.map