@moyal/js-linq
Version:
A lightweight, zero-dependency JavaScript library that brings LINQ-style querying capabilities to native arrays. Designed for both clarity and performance, it offers a fluent, chainable API for filtering, projecting, grouping, ordering, and aggregating da
7 lines (6 loc) • 22.3 kB
JavaScript
/*!
* @moyal/js-linq v1.0.4
* (c) 2000–present Ilan Moyal
* Released under the MIT License
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).MoyalLinq={})}(this,(function(e){"use strict";class t{static#e=Object.freeze({ALL_MUST_BE_ITERABLE:"All arguments must be iterable",ARGUMENT_MUST_NOT_BE_NULL_key:"key argument MUST NOT be null",INPUT_SEQUENCE_CONTAINS_MORE_THAN_ONE_ELEMENT:"The input sequence contains more than one element",MORE_THAN_ELEMENT_SEQUENCE_SATISFIES_THE_CONDITION:"More than one element satisfies the condition in predicate",MUST_BE_NON_NEGATIVE_INTEGER_count:"count must be a non negative integer",MUST_BE_NON_NEGATIVE_INTEGER_factor:"`factor` must be a non negative integer",MUST_BE_NON_NEGATIVE_INTEGER_index:"index must be a non negative integer",MUST_BE_FUNCTION_OR_NULLISH_collectionSelector:"collectionSelector must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_comparer:"comparer must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_elementSelector:"elementSelector must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_equalityComparer:"equalityComparer must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_keyEqualityComparer:"keyEqualityComparer must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_keySelector:"keySelector must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_leftKeySelector:"leftKeySelector must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_predicate:"predicate must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_resultSelector:"resultSelector must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_rightKeySelector:"rightKeySelector must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_selector:"selector must be a function or nullish",MUST_BE_FUNCTION_OR_NULLISH_valueSelector:"valueSelector must be a function or nullish",MUST_BE_FUNCTION_accumulator:"accumulator must be a function",MUST_BE_FUNCTION_callback:"callback must be a function",MUST_BE_FUNCTION_predicate:"predicate must be a function",MUST_BE_FUNCTION_transform:"transform must be a function",MUST_BE_INTEGER_count:"count must be an integer",MUST_BE_INTEGER_start:"start must be an integer",MUST_BE_ITERABLE_iterable:"'iterable' argument must be iterable",MUST_BE_ITERABLE_iterable1:"iterable1 must be iterable",MUST_BE_ITERABLE_iterable2:"iterable2 must be iterable",MUST_BE_ITERABLE_rightIterable:"rightIterable must be iterable",MUST_BE_ITERABLE_secondIterable:"secondIterable must be iterable",MUST_BE_NUMBER_all_sequence_elements:"At least one of the sequence's element is not a number",NO_ELEMENT_SATISFIES_THE_CONDITION_IN_predicate:"No element satisfies the condition in predicate",OUT_OF_RANGE_index:"index is out of range",PRODUCES_DUPLICATE_KEYS_keySelector:"keySelector produces duplicate keys",SEQUENCE_IS_EMPTY:"The sequence is empty"});static get Messages(){return this.#e}}class r{static isNumber(e){return"number"==typeof e||"[object Number]"===Object.prototype.toString.call(e)}static isBigInt(e){return"bigint"==typeof e||"[object BigInt]"===Object.prototype.toString.call(e)}static isIntegral(e,t){return(this.isBigInt(e)||this.isNumber(e)&&Math.floor(e)===e)&&(null==t||!0===t(e))}static isIterable(e){return this.isFunctionOrGeneratorFunction(e?.[Symbol.iterator])}static isFunction(e){const t=typeof e;return("object"===t||"function"===t)&&"[object Function]"===Object.prototype.toString.call(e)}static isFunctionOrGeneratorFunction(e){const t=typeof e,r=Object.prototype.toString.call(e);return!("object"!==t&&"function"!==t||"[object Function]"!==r&&"[object GeneratorFunction]"!==r)}}class i{static version="1.0.4"}class s{#t=0;#r=void 0;#i=void 0;#s=void 0;#n=void 0;#a=0;#_=void 0;#o=void 0;#l=void 0;#u=void 0;constructor(e,t,r,i,s,n,a,_,o,l){this.#t=e??0,this.#r=t??void 0,this.#i=r??void 0,this.#s=i??void 0,this.#n=s??void 0,this.#a=n??0,this.#_=null==a?void 0:Array.isArray(a)?a:[a],this.#o=_??void 0,this.#l=o??void 0,this.#u=l??void 0}get count(){return this.#t}get minimum(){return this.#r}get maximum(){return this.#i}get range(){return this.#s}get average(){return this.#n}get summary(){return this.#a}get mode(){return this.#_}get median(){return this.#o}get variance(){return this.#l}get standardDeviation(){return this.#u}static empty(){return new s(0,void 0,void 0,void 0,void 0,0,void 0,void 0,void 0,void 0)}clone(){return new s(this.#t,this.#r,this.#i,this.#s,this.#n,this.#a,null==this.#_?void 0:[...this.#_],this.#o,this.#l,this.#u)}toJSON(){return{count:this.#t,minimum:this.#r,maximum:this.#i,range:this.#s,average:this.#n,summary:this.#a,mode:this.#_,median:this.#o,variance:this.#l,standardDeviation:this.#u}}static fromJSON(e){if("object"!=typeof e||null===e)throw new Error("Invalid JSON object for Statistics.");return new s(e.count??0,e.minimum??void 0,e.maximum??void 0,e.range??void 0,e.average??void 0,e.summary??0,null==e.mode?void 0:Array.isArray(e.mode)?[...e.mode]:[e.mode],e.median??void 0,e.variance??void 0,e.standardDeviation??void 0)}static#h(e,t,r){if(null==e&&null==t)return!0;if(null==e||null==t)return!1;if(null==r)return e===t;if("number"!=typeof r||r<0||!Number.isFinite(r))throw new Error("Invalid n: must be a non-negative finite number");if(!Number.isFinite(e)||!Number.isFinite(t))return e===t;const i=10**r;return Math.round((e+Number.EPSILON)*i)===Math.round((t+Number.EPSILON)*i)}static#c(e,t){if(null==e&&null==t)return!0;if(null==e||null==t)return!1;if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(e[r]!==t[r])return!1;return!0}equal(e,t){return null!=e&&this.count===e.count&&this.minimum===e.minimum&&this.maximum===e.maximum&&s.#h(this.summary,e.summary,t)&&s.#h(this.average,e.average,t)&&s.#h(this.range,e.range,t)&&s.#h(this.median,e.median,t)&&s.#h(this.variance,e.variance,t)&&s.#h(this.standardDeviation,e.standardDeviation,t)&&s.#c(this.mode,e.mode)}}let n,a;class _{static __setup(e,t){n=e,a=t}static#E=(e,t)=>e>t?1:e<t?-1:0;static#f=(e,t)=>e===t;static#m=e=>e;static#N(...e){return e.length>1?e:e[0]}static*#d(e){for(let t of e)yield t}static get Version(){return i.version}#g=null;#S=null;constructor(e,i){if(!r.isIterable(e))throw new Error(t.Messages.MUST_BE_ITERABLE_iterable);this.#g=e,this.#S=i}*[Symbol.iterator](){for(let e of this.#g)yield e}static from(e,t){return new this(e,t)}aggregate(e,t,r){return _.#T(this.#S,e,this,t,r)}static#T(e,i,s,n,a){if(!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_accumulator);if(null!=a&&!r.isFunction(a))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);e=e??this,a=a??this.#N;let _=i,o=0;for(let t of s)_=n.call(e,t,_,o),o++;return a.call(e,_)}all(e){return _.#U(this.#S,this,e)}static#U(e,i,s){if(!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_predicate);e=e??this;let n=0;for(let t of i){if(!0!==s.call(e,t,n))return!1;n++}return!0}any(e){return _.#M(this.#S,this,e)}static#M(e,i,s){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_predicate);e=e??this;let n=0;for(let t of i){if(null==s||!0===s.call(e,t,n))return!0;n++}return!1}append(e){return _.#I(this.#S,this,e)}static#I(e,t,r){return new this(this.#w(e,t,r))}static*#w(e,t,r){for(let e of t)yield e;yield r}average(){return _.#n(this.#S,this)}static#n(e,t){let i=0,s=0;for(let e of t){if(!r.isNumber(e)){i=void 0;break}i+=e,s++}return void 0===i||0===s?void 0:i/s}concat(...e){return _.#p(this.#S,this,...e)}static#p(e,i,...s){for(let e of s)if(!r.isIterable(e))throw new Error(t.Messages.ALL_MUST_BE_ITERABLE);return new this(this.#y(e,i,...s),e)}static*#y(e,...t){for(let e of t)for(let t of e)yield t}contains(e,t=null){return _.#O(this.#S,this,e,t)}static#O(e,i,s,n){if(null!=n&&!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_equalityComparer);n=n??this.#f;let a=0;for(let t of i){if(null==e?!0===n(s,t,a):n.call(e,s,t,a))return!0;a++}return!1}count(){return _.#t(this.#S,this)}static#t(e,t){let r=0;for(let e of t)r++;return r}defaultIfEmpty(e){return _.#L(this.#S,this,e)}static#L(e,t,r){return new this(this.#b(e,t,r),e)}static*#b(e,t,r){let i=!1;for(let e of t)i=!0,yield e;i||(yield r)}distinct(e){return _.#B(this.#S,this,e)}static#B(e,i,s){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_equalityComparer);return new this(this.#v(e,i,s),e)}static*#v(e,t,r){r=r??this.#f;let i=[];for(let n of t){let t=!1;for(var s=0;!t&&s<i.length;s++)t=null==e?!0===r(n,i[s]):!0===r.call(e,n,i[s]);t||i.push(n)}for(let e of i)yield e}duplicate(e,t){return _.#A(this.#S,this,e,t)}static#A(e,i,s,n){if(!r.isNumber(s)||Math.floor(s)!==s||s<0)throw new Error(t.Messages.MUST_BE_NON_NEGATIVE_INTEGER_factor);return new this(this.#F(e,i,s,!0===n),e)}static*#F(e,t,r,i){if(r>=1)if(!0===i)for(let e of t)for(let t=0;t<r;t++)yield e;else for(let e=0;e<r;e++)for(let e of t)yield e}elementAt(e){return _.#C(this.#S,this,e,!1,null)}elementAtOrDefault(e,t){return _.#C(this.#S,this,e,!0,t)}static#C(e,i,s,n,a){if(!r.isNumber(s)||Math.floor(s)!=s||s<0&&!0!==n)throw new Error(t.Messages.MUST_BE_NON_NEGATIVE_INTEGER_index);let _=0;for(let e of i)if(_++===s)return e;if(!0===n)return a;throw new Error(0===_?t.Messages.SEQUENCE_IS_EMPTY:t.Messages.OUT_OF_RANGE_index)}static empty(e){return new this([],e)}except(e,t){return _.#R(this.#S,this,e,t,"except")}static#R(e,i,s,n,a){if(!r.isIterable(s))throw new Error(t.Messages.MUST_BE_ITERABLE_secondIterable);if(null!=n&&!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_equalityComparer);return new this(this.#G(e,i,s,n,a),e)}static*#G(e,t,r,i,s){let n="except"!==s&&("intersect"===s||null);e=e??this,i=i??this.#f;let a,_=Array.from(this.#v(e,r)),o=[];for(let r of t){a=null;for(let e of o)if(r===e){a=!0;break}if(null!=n){if(null==a){a=n;for(let t of _)if(!0===i.call(e,r,t)){a=!n;break}}}else null===a&&(a=!1);!1===a&&(o.push(r),yield r)}if(null===n)for(let t of _){a=!1;for(let r of o)if(!0===i.call(e,r,t)){a=!0;break}!1===a&&(yield t)}}first(e){return _.#k(this.#S,this,e,!1)}firstOrDefault(e,t){return _.#k(this.#S,this,t,!0,e)}static#k(e,i,s,n,a){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_predicate);e=e??this;let _=0;for(let t of i){if(null==s||!0===s.call(e,t,_))return t;_++}if(n)return a;throw new Error(_>0&&null!=s?t.Messages.NO_ELEMENT_SATISFIES_THE_CONDITION_IN_predicate:t.Messages.SEQUENCE_IS_EMPTY)}forEach(e){return _.#H(this.#S,this,e)}static#H(e,i,s){if(!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_callback);e=e??this;let n=0;for(let t of i){let r=s.call(e,t,n);if(n++,!1===r)break}}groupBy(e,t,r,i){return _.#q(this.#S,this,e,t,r,i)}static#q(e,i,s,n,a,_){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_keySelector);if(null!=n&&!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_elementSelector);if(null!=a&&!r.isFunction(a))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);if(null!=_&&!r.isFunction(_))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_keyEqualityComparer);return new this(this.#x(e,i,s,n,a,_),e)}static*#x(e,t,r,i,s,n){r=r??this.#m,i=i??this.#m,s=s??this.#N,n=n??this.#f,e=e??this;let _=[],o=0;for(let s of t){let t=r.call(e,s,o),a=i.call(e,s,o),l=!1;for(let r of _)if(!0===n.call(e,t,r.key,o)){r.elements.push(a),l=!0;break}l||_.push({key:t,elements:[a]}),o++}let l=[];for(let t of _)l.push(s.call(e,new a(t.key,t.elements,e)));yield*l}groupJoin(e,t,r,i,s){return _.#D(this.#S,this,e,t,r,i,s)}static#D(e,i,s,n,a,_,o){if(!r.isIterable(s))throw new Error(t.Messages.MUST_BE_ITERABLE_rightIterable);if(null!=n&&!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_leftKeySelector);if(null!=a&&!r.isFunction(a))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_rightKeySelector);if(null!=_&&!r.isFunction(_))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);if(null!=o&&!r.isFunction(o))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_keyEqualityComparer);return new this(this.#j(e,i,s,n,a,_,o),e)}static*#j(e,t,r,i,s,n,a){i=i??this.#m,s=s??this.#m,n=n??this.#N,a=a??this.#f,e=e??this;for(let o of t){let t=i.call(e,o);yield n.call(e,o,new _(r).where((r=>!0===a.call(e,t,s.call(e,r)))))}}intersect(e,t){return _.#R(this.#S,this,e,t,"intersect")}join(e,t,r,i,s){return _.#P(this.#S,this,e,t,r,i,s)}static#P(e,i,s,n,a,_,o){if(!r.isIterable(s))throw new Error(t.Messages.MUST_BE_ITERABLE_rightIterable);if(null!=n&&!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_leftKeySelector);if(null!=a&&!r.isFunction(a))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_rightKeySelector);if(null!=_&&!r.isFunction(_))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);if(null!=o&&!r.isFunction(o))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_keyEqualityComparer);return new this(this.#K(e,i,s,n,a,_,o),e)}static*#K(e,t,r,i,s,n,a){i=i??this.#m,s=s??this.#m,n=n??this.#N,a=a??this.#f,e=e??this;for(let _ of t){let t=i.call(e,_);for(let i of r){let r=s.call(e,i);!0===a.call(e,t,r)&&(yield n.call(e,_,i))}}}last(e){return _.#W(this.#S,this,e,!1)}lastOrDefault(e,t){return _.#W(this.#S,this,t,!0,e)}static#W(e,i,s,n,a){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_predicate);e=e??this;let _,o=0,l=!1;for(let t of i)null!=s&&!0!==s.call(e,t,o)||(l=!0,_=t),o++;if(l)return _;if(n)return a;throw new Error(o>0&&null!=s?t.Messages.NO_ELEMENT_SATISFIES_THE_CONDITION_IN_predicate:t.Messages.SEQUENCE_IS_EMPTY)}max(e){return _.#J(this.#S,this,e)}static#J(e,t,r){return this.#Q(e,t,r,1)}min(e){return _.#V(this.#S,this,e)}static#V(e,t,r){return this.#Q(e,t,r,-1)}static#Q(e,i,s,n){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_comparer);s=s??this.#E;let a,_=!0,o=0;e=e??this;for(let t of i)!0===_?(a=t,_=!1):a=n*s.call(e,a,t,o)>=0?a:t,o++;return a}orderBy(e,t){return new n(this,e,t,!1,this.#S)}orderByDescending(e,t){return new n(this,e,t,!0,this.#S)}prepend(e){return _.#Y(this.#S,this,e)}static#Y(e,t,r){return new this(this.#z(e,t,r),e)}static*#z(e,t,r){yield r;for(let e of t)yield e}static range(e,i,s){if(!r.isNumber(e)||Math.floor(e)!==e)throw new Error(t.Messages.MUST_BE_INTEGER_start);if(!r.isNumber(i)||Math.floor(i)!==i||i<0)throw new Error(t.Messages.MUST_BE_NON_NEGATIVE_INTEGER_count);return new this(this.#$(e,i),s)}static*#$(e,t){for(;t-- >0;)yield e++}removeNullishes(){return _.#X(this.#S,this)}static#X(e,t){return new this(this.#Z(e,t),e)}static*#Z(e,t){for(let e of t)null!==(e??null)&&(yield e)}static repeat(e,i,s){if(!r.isNumber(i)||Math.floor(i)!==i||i<0)throw new Error(t.Messages.MUST_BE_NON_NEGATIVE_INTEGER_count);return new this(this.#ee(s,e,i),s)}static*#ee(e,t,r){for(let e=0;e<r;e++)yield t}reverse(){return _.#te(this.#S,this)}static#te(e,t){return new this(this.#re(e,t),e)}static*#re(e,t){let r=[];for(let e of t)r.push(e);for(let e=r.length-1;e>=0;e--)yield r[e]}select(e){return _.#ie(this.#S,this,e)}static#ie(e,i,s){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_selector);return new this(this.#se(e,i,s),e)}static*#se(e,t,r){e=e??this,r=r??this.#m;let i=0;for(let s of t)yield r.call(e,s,i),i++}selectMany(e,t){return _.#ne(this.#S,this,e,t)}static#ne(e,i,s,n){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_collectionSelector);if(null!=n&&!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);return new this(this.#ae(e,i,s,n),e)}static*#ae(e,t,r,i){r=r??this.#m,i=i??this.#m,e=e??this;let s=0;for(let n of t){let t=r.call(e,n,s),a=0;for(let r of t)yield i.call(e,r,a,n,s),a++;s++}}sequenceEqual(e,t){return _.#_e(this.#S,this,e,t)}static#_e(e,i,s,n){if(!r.isIterable(s))throw new Error(t.Messages.MUST_BE_ITERABLE_secondIterable);if(null!=n&&!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_equalityComparer);n=n??this.#f;let a=this.#d(i),_=this.#d(s);e=e??this;let o,l,u=0;do{if(o=a.next(),l=_.next(),o.done^l.done||!1===o.done&&!1===l.done&&!0!==n.call(e,o.value,l.value,u))return!1;u++}while(!o.done);return!0}single(e){return _.#oe(this.#S,this,e,!1)}singleOrDefault(e,t){return _.#oe(this.#S,this,t,!0,e)}static#oe(e,i,s,n,a){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_predicate);let _,o=0,l=0;e=e??this;for(let r of i){if(null==s||!0===s.call(e,r,l)){if(o++,o>1)throw new Error(null==s?t.Messages.INPUT_SEQUENCE_CONTAINS_MORE_THAN_ONE_ELEMENT:t.Messages.MORE_THAN_ELEMENT_SEQUENCE_SATISFIES_THE_CONDITION);_=r}l++}if(0===o){if(!0!==n)throw new Error(0===l?t.Messages.SEQUENCE_IS_EMPTY:t.Messages.NO_ELEMENT_SATISFIES_THE_CONDITION_IN_predicate);_=a}return _}skip(e){return _.#le(this.#S,this,e)}static#le(e,i,s){if(!r.isIntegral(s))throw new Error(t.Messages.MUST_BE_INTEGER_count);return new this(this.#ue(e,i,s),e)}static*#ue(e,t,r){let i=0;for(let e of t)i++<r||(yield e)}skipLast(e){return _.#he(this.#S,this,e)}static#he(e,i,s){if(!r.isIntegral(s))throw new Error(t.Messages.MUST_BE_INTEGER_count);return new this(this.#ce(e,i,s),e)}static*#ce(e,t,r){if(r<=0)yield*t;else{let e=Array.from(t);for(let t=0;t<e.length-r;t++)yield e[t]}}skipWhile(e){return _.#Ee(this.#S,this,e)}static#Ee(e,i,s){if(!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_predicate);return new this(this.#fe(e,i,s),e)}static*#fe(e,t,r){let i=!0,s=0;e=e??this;for(let n of t)i?!0!==r.call(e,n,s++)&&(i=!1,yield n):yield n}statistics(e){return _.#me(this.#S,this,e)}static#me(e,i,s){const n={count:0,minimum:void 0,maximum:void 0,summary:0,average:void 0,range:void 0,mode:void 0,median:void 0,variance:void 0,standardDeviation:void 0},a=!0===s||s?.median?[]:null,o=!0===s||s?.mode?new Map:null;let l=0,u=0,h=0,c=0;for(let e of i){if(!r.isNumber(e))throw new Error(t.Messages.MUST_BE_NUMBER_all_sequence_elements);0===c?(n.minimum=e,n.maximum=e):(n.minimum=Math.min(n.minimum,e),n.maximum=Math.max(n.maximum,e)),n.summary+=e;const i=e-u;if(u+=i/(c+1),h+=i*(e-u),a&&a.push(e),o){let t=o.get(e)||0;t++,o.set(e,t),(0==l||t>l)&&(l=t)}c++}if(n.count=c,n.count>0){if(n.average=n.summary/n.count,n.range=n.maximum-n.minimum,n.count>1?(n.variance=h/n.count,n.standardDeviation=Math.sqrt(n.variance)):(n.variance=0,n.standardDeviation=0),a){a.sort(((e,t)=>e-t));const e=Math.floor(a.length/2);n.median=a.length%2!=0?a[e]:(a[e-1]+a[e])/2}if(o)if(l<=1)n.mode=void 0;else{const e=[];let t=0;for(const[r,i]of o.entries())t++,i===l&&e.push(r);e.length===t?n.mode=void 0:n.mode=e.sort(((e,t)=>e-t))}}return _.Statistics.fromJSON(n)}sum(){return _.#Ne(this.#S,this)}static#Ne(e,t){let i,s=0;for(let e of t){if(!r.isNumber(e))return;i=s+=e}return i}take(e){return _.#de(this.#S,this,e)}static#de(e,i,s){if(!r.isIntegral(s))throw new Error(t.Messages.MUST_BE_INTEGER_count);return new this(this.#ge(e,i,s),e)}static*#ge(e,t,r){let i=0;for(let e of t){if(!(i++<r))break;yield e}}takeLast(e){return _.#Se(this.#S,this,e)}static#Se(e,i,s){if(!r.isIntegral(s))throw new Error(t.Messages.MUST_BE_INTEGER_count);return new this(this.#Te(e,i,s),e)}static*#Te(e,t,r){if(r>0){let e=Array.from(t);for(let t=Math.max(0,e.length-r);t<e.length;t++)yield e[t]}}takeWhile(e){return _.#Ue(this.#S,this,e)}static#Ue(e,i,s){if(!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_predicate);return new this(this.#Me(e,i,s),e)}static*#Me(e,t,r){let i=0;e=e??this;for(let s of t){if(!0!==r.call(e,s,i))break;yield s,i++}}toArray(){return _.#Ie(this.#S,this)}static#Ie(e,t){let r=[];for(let e of t)r.push(e);return r}toMap(e,t){return _.#we(this.#S,this,e,t,!1)}toDictionary(e,t){return _.#we(this.#S,this,e,t,!1)}toSet(e){return _.#we(this.#S,this,e,null,!0)}static#we(e,i,s,n,a,_){if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_keySelector);if(null!=n&&!r.isFunction(n))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_valueSelector);s=s??this.#m,n=n??this.#m;let o=!0===a?new Set:new Map,l=0;e=e??this;for(let r of i){let i=s.call(e,r,l);if(o.has(i))throw new Error(t.Messages.PRODUCES_DUPLICATE_KEYS_keySelector);!0===a?o.add(i):o.set(i,n.call(e,r,l)),l++}return o}where(e){return _.#pe(this.#S,this,e)}static#pe(e,i,s){if(!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_predicate);return new this(this.#ye(e,i,s),e)}static*#ye(e,t,r){let i=0;e=e??this;for(let s of t)!0===r.call(e,s,i)&&(yield s),i++}union(e,t){return _.#R(this.#S,this,e,t,"union")}static zip(e,i,s,n){if(!r.isIterable(e))throw new Error(t.Messages.MUST_BE_ITERABLE_iterable1);if(!r.isIterable(i))throw new Error(t.Messages.MUST_BE_ITERABLE_iterable2);if(!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_transform);return new this(this.#Oe(n,e,i,s),n)}static*#Oe(e,t,r,i){let s,n,a=this.#d(t),_=this.#d(r),o=0;for(e=e??this,s=a.next(),n=_.next();!s.done&&!n.done;)yield i.call(e,s.value,n.value,o),s=a.next(),n=_.next(),o++;a.return(),_.return()}}_.Statistics=s;class o extends _{static#E=(e,t)=>e>t?1:e<t?-1:0;#S=null;#g=null;#Le=[];constructor(e,i,s,n,a){if(super(e,a),null!=i&&!r.isFunction(i))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_keySelector);if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_comparer);this.#g=e,this.#S=a,this.#Le=[o.#be(i,s,n?-1:1)]}static#Be(e,i,s,n){if(null!=i&&!r.isFunction(i))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_keySelector);if(null!=s&&!r.isFunction(s))throw new Error(t.Messages.MUST_BE_FUNCTION_OR_NULLISH_comparer);return e.#Le.push(this.#be(i,s,n?-1:1)),e}static#be(e,t,r){return t=t??this.#E,null==e?function(e,i){return r*t(e,i)}:function(i,s){return r*t(e(i),e(s))}}#ve(e,t){return function(r,i){for(const s of t){const t=s.call(e,r,i);if(0!=t)return t}return 0}}*[Symbol.iterator](){yield*Array.from(this.#g).sort(this.#ve(this.#S??this,this.#Le))}thenBy(e,t){return o.#Be(this,e,t,!1)}thenByDescending(e,t){return o.#Be(this,e,t,!0)}}_.__setup(o,class extends _{#Ae=null;constructor(e,r,i){if(null==e)throw new Error(t.Messages.ARGUMENT_MUST_NOT_BE_NULL_key);super(r,i),this.#Ae=e}get key(){return this.#Ae}toString(){return`LinqGroup (key=${this.#Ae})`}}),e.Linq=_,e.Statistics=s}));