@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.1 kB
JavaScript
/*!
* @moyal/js-linq v1.0.4
* (c) 2000–present Ilan Moyal
* Released under the MIT License
*/
class e{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 t{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 r{static version="1.0.4"}class i{#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 i(0,void 0,void 0,void 0,void 0,0,void 0,void 0,void 0,void 0)}clone(){return new i(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 i(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&&i.#h(this.summary,e.summary,t)&&i.#h(this.average,e.average,t)&&i.#h(this.range,e.range,t)&&i.#h(this.median,e.median,t)&&i.#h(this.variance,e.variance,t)&&i.#h(this.standardDeviation,e.standardDeviation,t)&&i.#c(this.mode,e.mode)}}let s,n;class a{static __setup(e,t){s=e,n=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 r.version}#g=null;#S=null;constructor(r,i){if(!t.isIterable(r))throw new Error(e.Messages.MUST_BE_ITERABLE_iterable);this.#g=r,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 a.#T(this.#S,e,this,t,r)}static#T(r,i,s,n,a){if(!t.isFunction(n))throw new Error(e.Messages.MUST_BE_FUNCTION_accumulator);if(null!=a&&!t.isFunction(a))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);r=r??this,a=a??this.#N;let _=i,o=0;for(let e of s)_=n.call(r,e,_,o),o++;return a.call(r,_)}all(e){return a.#U(this.#S,this,e)}static#U(r,i,s){if(!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_predicate);r=r??this;let n=0;for(let e of i){if(!0!==s.call(r,e,n))return!1;n++}return!0}any(e){return a.#M(this.#S,this,e)}static#M(r,i,s){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_predicate);r=r??this;let n=0;for(let e of i){if(null==s||!0===s.call(r,e,n))return!0;n++}return!1}append(e){return a.#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 a.#n(this.#S,this)}static#n(e,r){let i=0,s=0;for(let e of r){if(!t.isNumber(e)){i=void 0;break}i+=e,s++}return void 0===i||0===s?void 0:i/s}concat(...e){return a.#p(this.#S,this,...e)}static#p(r,i,...s){for(let r of s)if(!t.isIterable(r))throw new Error(e.Messages.ALL_MUST_BE_ITERABLE);return new this(this.#y(r,i,...s),r)}static*#y(e,...t){for(let e of t)for(let t of e)yield t}contains(e,t=null){return a.#O(this.#S,this,e,t)}static#O(r,i,s,n){if(null!=n&&!t.isFunction(n))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_equalityComparer);n=n??this.#f;let a=0;for(let e of i){if(null==r?!0===n(s,e,a):n.call(r,s,e,a))return!0;a++}return!1}count(){return a.#t(this.#S,this)}static#t(e,t){let r=0;for(let e of t)r++;return r}defaultIfEmpty(e){return a.#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 a.#b(this.#S,this,e)}static#b(r,i,s){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_equalityComparer);return new this(this.#v(r,i,s),r)}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.#A(this.#S,this,e,t)}static#A(r,i,s,n){if(!t.isNumber(s)||Math.floor(s)!==s||s<0)throw new Error(e.Messages.MUST_BE_NON_NEGATIVE_INTEGER_factor);return new this(this.#F(r,i,s,!0===n),r)}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 a.#C(this.#S,this,e,!1,null)}elementAtOrDefault(e,t){return a.#C(this.#S,this,e,!0,t)}static#C(r,i,s,n,a){if(!t.isNumber(s)||Math.floor(s)!=s||s<0&&!0!==n)throw new Error(e.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===_?e.Messages.SEQUENCE_IS_EMPTY:e.Messages.OUT_OF_RANGE_index)}static empty(e){return new this([],e)}except(e,t){return a.#R(this.#S,this,e,t,"except")}static#R(r,i,s,n,a){if(!t.isIterable(s))throw new Error(e.Messages.MUST_BE_ITERABLE_secondIterable);if(null!=n&&!t.isFunction(n))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_equalityComparer);return new this(this.#G(r,i,s,n,a),r)}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 a.#k(this.#S,this,e,!1)}firstOrDefault(e,t){return a.#k(this.#S,this,t,!0,e)}static#k(r,i,s,n,a){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_predicate);r=r??this;let _=0;for(let e of i){if(null==s||!0===s.call(r,e,_))return e;_++}if(n)return a;throw new Error(_>0&&null!=s?e.Messages.NO_ELEMENT_SATISFIES_THE_CONDITION_IN_predicate:e.Messages.SEQUENCE_IS_EMPTY)}forEach(e){return a.#H(this.#S,this,e)}static#H(r,i,s){if(!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_callback);r=r??this;let n=0;for(let e of i){let t=s.call(r,e,n);if(n++,!1===t)break}}groupBy(e,t,r,i){return a.#q(this.#S,this,e,t,r,i)}static#q(r,i,s,n,a,_){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_keySelector);if(null!=n&&!t.isFunction(n))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_elementSelector);if(null!=a&&!t.isFunction(a))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);if(null!=_&&!t.isFunction(_))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_keyEqualityComparer);return new this(this.#x(r,i,s,n,a,_),r)}static*#x(e,t,r,i,s,a){r=r??this.#m,i=i??this.#m,s=s??this.#N,a=a??this.#f,e=e??this;let _=[],o=0;for(let s of t){let t=r.call(e,s,o),n=i.call(e,s,o),l=!1;for(let r of _)if(!0===a.call(e,t,r.key,o)){r.elements.push(n),l=!0;break}l||_.push({key:t,elements:[n]}),o++}let l=[];for(let t of _)l.push(s.call(e,new n(t.key,t.elements,e)));yield*l}groupJoin(e,t,r,i,s){return a.#D(this.#S,this,e,t,r,i,s)}static#D(r,i,s,n,a,_,o){if(!t.isIterable(s))throw new Error(e.Messages.MUST_BE_ITERABLE_rightIterable);if(null!=n&&!t.isFunction(n))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_leftKeySelector);if(null!=a&&!t.isFunction(a))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_rightKeySelector);if(null!=_&&!t.isFunction(_))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);if(null!=o&&!t.isFunction(o))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_keyEqualityComparer);return new this(this.#j(r,i,s,n,a,_,o),r)}static*#j(e,t,r,i,s,n,_){i=i??this.#m,s=s??this.#m,n=n??this.#N,_=_??this.#f,e=e??this;for(let o of t){let t=i.call(e,o);yield n.call(e,o,new a(r).where((r=>!0===_.call(e,t,s.call(e,r)))))}}intersect(e,t){return a.#R(this.#S,this,e,t,"intersect")}join(e,t,r,i,s){return a.#P(this.#S,this,e,t,r,i,s)}static#P(r,i,s,n,a,_,o){if(!t.isIterable(s))throw new Error(e.Messages.MUST_BE_ITERABLE_rightIterable);if(null!=n&&!t.isFunction(n))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_leftKeySelector);if(null!=a&&!t.isFunction(a))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_rightKeySelector);if(null!=_&&!t.isFunction(_))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);if(null!=o&&!t.isFunction(o))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_keyEqualityComparer);return new this(this.#K(r,i,s,n,a,_,o),r)}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 a.#W(this.#S,this,e,!1)}lastOrDefault(e,t){return a.#W(this.#S,this,t,!0,e)}static#W(r,i,s,n,a){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_predicate);r=r??this;let _,o=0,l=!1;for(let e of i)null!=s&&!0!==s.call(r,e,o)||(l=!0,_=e),o++;if(l)return _;if(n)return a;throw new Error(o>0&&null!=s?e.Messages.NO_ELEMENT_SATISFIES_THE_CONDITION_IN_predicate:e.Messages.SEQUENCE_IS_EMPTY)}max(e){return a.#J(this.#S,this,e)}static#J(e,t,r){return this.#Q(e,t,r,1)}min(e){return a.#V(this.#S,this,e)}static#V(e,t,r){return this.#Q(e,t,r,-1)}static#Q(r,i,s,n){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_comparer);s=s??this.#E;let a,_=!0,o=0;r=r??this;for(let e of i)!0===_?(a=e,_=!1):a=n*s.call(r,a,e,o)>=0?a:e,o++;return a}orderBy(e,t){return new s(this,e,t,!1,this.#S)}orderByDescending(e,t){return new s(this,e,t,!0,this.#S)}prepend(e){return a.#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(r,i,s){if(!t.isNumber(r)||Math.floor(r)!==r)throw new Error(e.Messages.MUST_BE_INTEGER_start);if(!t.isNumber(i)||Math.floor(i)!==i||i<0)throw new Error(e.Messages.MUST_BE_NON_NEGATIVE_INTEGER_count);return new this(this.#$(r,i),s)}static*#$(e,t){for(;t-- >0;)yield e++}removeNullishes(){return a.#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(r,i,s){if(!t.isNumber(i)||Math.floor(i)!==i||i<0)throw new Error(e.Messages.MUST_BE_NON_NEGATIVE_INTEGER_count);return new this(this.#ee(s,r,i),s)}static*#ee(e,t,r){for(let e=0;e<r;e++)yield t}reverse(){return a.#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 a.#ie(this.#S,this,e)}static#ie(r,i,s){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_selector);return new this(this.#se(r,i,s),r)}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 a.#ne(this.#S,this,e,t)}static#ne(r,i,s,n){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_collectionSelector);if(null!=n&&!t.isFunction(n))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_resultSelector);return new this(this.#ae(r,i,s,n),r)}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 a.#_e(this.#S,this,e,t)}static#_e(r,i,s,n){if(!t.isIterable(s))throw new Error(e.Messages.MUST_BE_ITERABLE_secondIterable);if(null!=n&&!t.isFunction(n))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_equalityComparer);n=n??this.#f;let a=this.#d(i),_=this.#d(s);r=r??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(r,o.value,l.value,u))return!1;u++}while(!o.done);return!0}single(e){return a.#oe(this.#S,this,e,!1)}singleOrDefault(e,t){return a.#oe(this.#S,this,t,!0,e)}static#oe(r,i,s,n,a){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_predicate);let _,o=0,l=0;r=r??this;for(let t of i){if(null==s||!0===s.call(r,t,l)){if(o++,o>1)throw new Error(null==s?e.Messages.INPUT_SEQUENCE_CONTAINS_MORE_THAN_ONE_ELEMENT:e.Messages.MORE_THAN_ELEMENT_SEQUENCE_SATISFIES_THE_CONDITION);_=t}l++}if(0===o){if(!0!==n)throw new Error(0===l?e.Messages.SEQUENCE_IS_EMPTY:e.Messages.NO_ELEMENT_SATISFIES_THE_CONDITION_IN_predicate);_=a}return _}skip(e){return a.#le(this.#S,this,e)}static#le(r,i,s){if(!t.isIntegral(s))throw new Error(e.Messages.MUST_BE_INTEGER_count);return new this(this.#ue(r,i,s),r)}static*#ue(e,t,r){let i=0;for(let e of t)i++<r||(yield e)}skipLast(e){return a.#he(this.#S,this,e)}static#he(r,i,s){if(!t.isIntegral(s))throw new Error(e.Messages.MUST_BE_INTEGER_count);return new this(this.#ce(r,i,s),r)}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 a.#Ee(this.#S,this,e)}static#Ee(r,i,s){if(!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_predicate);return new this(this.#fe(r,i,s),r)}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 a.#me(this.#S,this,e)}static#me(r,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},_=!0===s||s?.median?[]:null,o=!0===s||s?.mode?new Map:null;let l=0,u=0,h=0,c=0;for(let r of i){if(!t.isNumber(r))throw new Error(e.Messages.MUST_BE_NUMBER_all_sequence_elements);0===c?(n.minimum=r,n.maximum=r):(n.minimum=Math.min(n.minimum,r),n.maximum=Math.max(n.maximum,r)),n.summary+=r;const i=r-u;if(u+=i/(c+1),h+=i*(r-u),_&&_.push(r),o){let e=o.get(r)||0;e++,o.set(r,e),(0==l||e>l)&&(l=e)}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),_){_.sort(((e,t)=>e-t));const e=Math.floor(_.length/2);n.median=_.length%2!=0?_[e]:(_[e-1]+_[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 a.Statistics.fromJSON(n)}sum(){return a.#Ne(this.#S,this)}static#Ne(e,r){let i,s=0;for(let e of r){if(!t.isNumber(e))return;i=s+=e}return i}take(e){return a.#de(this.#S,this,e)}static#de(r,i,s){if(!t.isIntegral(s))throw new Error(e.Messages.MUST_BE_INTEGER_count);return new this(this.#ge(r,i,s),r)}static*#ge(e,t,r){let i=0;for(let e of t){if(!(i++<r))break;yield e}}takeLast(e){return a.#Se(this.#S,this,e)}static#Se(r,i,s){if(!t.isIntegral(s))throw new Error(e.Messages.MUST_BE_INTEGER_count);return new this(this.#Te(r,i,s),r)}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 a.#Ue(this.#S,this,e)}static#Ue(r,i,s){if(!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_predicate);return new this(this.#Me(r,i,s),r)}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 a.#Ie(this.#S,this)}static#Ie(e,t){let r=[];for(let e of t)r.push(e);return r}toMap(e,t){return a.#we(this.#S,this,e,t,!1)}toDictionary(e,t){return a.#we(this.#S,this,e,t,!1)}toSet(e){return a.#we(this.#S,this,e,null,!0)}static#we(r,i,s,n,a,_){if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_keySelector);if(null!=n&&!t.isFunction(n))throw new Error(e.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;r=r??this;for(let t of i){let i=s.call(r,t,l);if(o.has(i))throw new Error(e.Messages.PRODUCES_DUPLICATE_KEYS_keySelector);!0===a?o.add(i):o.set(i,n.call(r,t,l)),l++}return o}where(e){return a.#pe(this.#S,this,e)}static#pe(r,i,s){if(!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_predicate);return new this(this.#ye(r,i,s),r)}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 a.#R(this.#S,this,e,t,"union")}static zip(r,i,s,n){if(!t.isIterable(r))throw new Error(e.Messages.MUST_BE_ITERABLE_iterable1);if(!t.isIterable(i))throw new Error(e.Messages.MUST_BE_ITERABLE_iterable2);if(!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_transform);return new this(this.#Oe(n,r,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()}}a.Statistics=i;class _ extends a{static#E=(e,t)=>e>t?1:e<t?-1:0;#S=null;#g=null;#Le=[];constructor(r,i,s,n,a){if(super(r,a),null!=i&&!t.isFunction(i))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_keySelector);if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_comparer);this.#g=r,this.#S=a,this.#Le=[_.#Be(i,s,n?-1:1)]}static#be(r,i,s,n){if(null!=i&&!t.isFunction(i))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_keySelector);if(null!=s&&!t.isFunction(s))throw new Error(e.Messages.MUST_BE_FUNCTION_OR_NULLISH_comparer);return r.#Le.push(this.#Be(i,s,n?-1:1)),r}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 _.#be(this,e,t,!1)}thenByDescending(e,t){return _.#be(this,e,t,!0)}}a.__setup(_,class extends a{#Ae=null;constructor(t,r,i){if(null==t)throw new Error(e.Messages.ARGUMENT_MUST_NOT_BE_NULL_key);super(r,i),this.#Ae=t}get key(){return this.#Ae}toString(){return`LinqGroup (key=${this.#Ae})`}});export{a as Linq,i as Statistics};