-
Notifications
You must be signed in to change notification settings - Fork 11
/
LInQer.slim.min.js
1 lines (1 loc) · 5.21 KB
/
LInQer.slim.min.js
1
"use strict";var Linqer;!function(t){class e{constructor(t){n(t),this._src=t;const e=t[Symbol.iterator];this._generator=e?e.bind(t):t,this._useQuickSort=void 0===t._useQuickSort||t._useQuickSort,this._canSeek=!1,this._count=null,this._tryGetAt=null,this._wasIterated=!1}static from(t){return t instanceof e?t:new e(t)}[Symbol.iterator](){return this._wasIterated=!0,this._generator()}static empty(){const t=new e([]);return t._count=()=>0,t._tryGetAt=t=>null,t._canSeek=!0,t}static range(t,n){const r=new e((function*(){for(let e=0;e<n;e++)yield t+e}));return r._count=()=>n,r._tryGetAt=e=>e>=0&&e<n?{value:t+e}:null,r._canSeek=!0,r}static repeat(t,n){const r=new e((function*(){for(let e=0;e<n;e++)yield t}));return r._count=()=>n,r._tryGetAt=e=>e>=0&&e<n?{value:t}:null,r._canSeek=!0,r}get length(){if(s(this),!this._canSeek)throw new Error("Calling length on this enumerable will iterate it. Use count()");return this.count()}concat(t){n(t);const r=this,o=new e((function*(){for(const t of r)yield t;for(const n of e.from(t))yield n})),i=e.from(t);return o._count=()=>r.count()+i.count(),s(this),s(i),o._canSeek=r._canSeek&&i._canSeek,r._canSeek&&(o._tryGetAt=t=>r._tryGetAt(t)||i._tryGetAt(t-r.count())),o}count(){return i(this),this._count()}distinct(n=t.EqualityComparer.default){const r=this,o=n===t.EqualityComparer.default?function*(){const t=new Set;for(const e of r){const n=t.size;t.add(e),n<t.size&&(yield e)}}:function*(){const t=[];for(const e of r){let r=!0;for(let o=0;o<t.length;o++)if(n(e,t[o])){r=!1;break}r&&(yield e),t.push(e)}};return new e(o)}elementAt(t){s(this);const e=this._tryGetAt(t);if(!e)throw new Error("Index out of range");return e.value}elementAtOrDefault(t){s(this);const e=this._tryGetAt(t);if(e)return e.value}first(){return this.elementAt(0)}firstOrDefault(){return this.elementAtOrDefault(0)}last(){if(s(this),!this._canSeek){let t=null,e=!1;for(const n of this)t=n,e=!0;if(e)return t;throw new Error("The enumeration is empty")}const t=this.count();return this.elementAt(t-1)}lastOrDefault(){if(s(this),!this._canSeek){let t=void 0;for(const e of this)t=e;return t}const t=this.count();return this.elementAtOrDefault(t-1)}stats(e){e?r(e):e=t._defaultComparer;const n={count:0,min:void 0,max:void 0};for(const t of this)(void 0===n.min||e(t,n.min)<0)&&(n.min=t),(void 0===n.max||e(t,n.max)>0)&&(n.max=t),n.count++;return n}min(t){const e=this.stats(t);return 0===e.count?void 0:e.min}max(t){const e=this.stats(t);return 0===e.count?void 0:e.max}select(t){r(t);const n=this,o=new e((function*(){let e=0;for(const r of n)yield t(r,e),e++}));return i(this),o._count=this._count,s(n),o._canSeek=n._canSeek,o._tryGetAt=e=>{const r=n._tryGetAt(e);return r?{value:t(r.value)}:r},o}skip(t){const n=this,r=new e((function*(){let e=t;for(const t of n)e>0?e--:yield t}));return r._count=()=>Math.max(0,n.count()-t),s(this),r._canSeek=this._canSeek,r._tryGetAt=e=>n._tryGetAt(e+t),r}splice(t,e,...n){return this.take(t).concat(n).concat(this.skip(t+e))}sum(){const t=this.sumAndCount();return 0===t.count?void 0:t.sum}sumAndCount(){const t={count:0,sum:0};for(const e of this)t.sum=0===t.count?o(e):t.sum+o(e),t.count++;return t}take(t){const n=this,r=new e((function*(){let e=t;for(const t of n)if(e>0&&(yield t,e--),e<=0)break}));return r._count=()=>Math.min(t,n.count()),s(this),r._canSeek=n._canSeek,n._canSeek&&(r._tryGetAt=e=>e>=t?null:n._tryGetAt(e)),r}toArray(){var t;if(s(this),this._canSeek){const e=new Array(this.count());for(let n=0;n<e.length;n++)e[n]=null===(t=this._tryGetAt(n))||void 0===t?void 0:t.value;return e}let e=0;const n=[];for(const t of this)e===n.length&&(n.length+=64),n[e]=t,e++;return n.length=e,n}toList(){return s(this),this._canSeek?this:e.from(this.toArray())}where(t){r(t);const n=this;return new e((function*(){let e=0;for(const r of n)t(r,e)&&(yield r),e++}))}}function n(t){if(t){if(t[Symbol.iterator])return;if("function"==typeof t&&"GeneratorFunction"===t.constructor.name)return}throw new Error("the argument must be iterable!")}function r(t){if(!t||"function"!=typeof t)throw new Error("the argument needs to be a function!")}function o(t){return"number"==typeof t?t:Number.NaN}function i(t){if(t._count)return;if(t._src instanceof e){const e=t._src;return i(e),void(t._count=()=>e._count())}const n=t._src;"function"==typeof n||"number"!=typeof n.length?"number"!=typeof n.size?t._count=()=>{let e=0;for(const n of t)e++;return e}:t._count=()=>n.size:t._count=()=>n.length}function s(t){if(t._tryGetAt)return;if(t._canSeek=!0,t._src instanceof e){const e=t._src;return s(e),t._tryGetAt=t=>e._tryGetAt(t),void(t._canSeek=e._canSeek)}if("string"==typeof t._src)return void(t._tryGetAt=e=>e<t._src.length?{value:t._src.charAt(e)}:null);if(Array.isArray(t._src))return void(t._tryGetAt=e=>e>=0&&e<t._src.length?{value:t._src[e]}:null);const n=t._src;"function"==typeof t._src||"number"!=typeof n.length?(t._canSeek=!1,t._tryGetAt=e=>{let n=0;for(const r of t){if(e===n)return{value:r};n++}return null}):t._tryGetAt=t=>t<n.length&&void 0!==n[t]?{value:n[t]}:null}t.Enumerable=e,t._ensureIterable=n,t._ensureFunction=r,t._toArray=function(t){return t?Array.isArray(t)?t:Array.from(t):[]},t._ensureInternalCount=i,t._ensureInternalTryGetAt=s,t._defaultComparer=(t,e)=>t>e?1:t<e?-1:0,t.EqualityComparer={default:(t,e)=>t==e,exact:(t,e)=>t===e}}(Linqer||(Linqer={}));