import {Arrays} from "./arrays"; import {ChainObject} from "./chainObject"; export class Chain { constructor(private _value: any[]) { } public arrayify(val: any): this { this._value = Arrays.arrayify(this._value); return this } public clone(): this { this._value = Arrays.clone(this._value); return this; } public compact(): this { this._value = Arrays.compact(this._value); return this; } public removeBy(criteria: (value: S, i?: number) => boolean): this { Arrays.removeBy(this._value, criteria); return this; } public remove(item: K): this { Arrays.remove(this._value, item) return this; } public chunk(size: number): this { this._value = Arrays.chunk(this._value, size); return this; } public flat(): Chain { this._value = Arrays.flat(this._value); return this as any; } public flatDeep(depth: number = 1): Chain { this._value = Arrays.flatDeep(this._value, depth); return this as any; } public sortBy(criteria: (value: S) => any): this { this._value = Arrays.sortBy(this._value, criteria); return this; } public sort(): this { this._value = Arrays.sort(this._value); return this; } public zip(...args: Array>): this { this._value = Arrays.zip(this._value, ...args); return this; } public uniqBy(criteria: (value: S, i?: number) => any): this { this._value = Arrays.uniqBy(this._value, criteria); return this } public uniq(): this { this._value = Arrays.uniq(this._value); return this } public sum(): number { return Arrays.sum(this._value); } public sumBy(criteria: (value: S, i?: number) => any): number { return Arrays.sumBy(this._value, criteria); } public difference(arr2: S[]): this { this._value = Arrays.difference(this._value, arr2); return this } public intersection(arr2: S[]): this { this._value = Arrays.intersection(this._value, arr2); return this } public union(arr2: S[]): this { this._value = Arrays.union(this._value, arr2); return this } public differenceBy(arr2: S[], criteria: (value: S, i?: number) => any): this { this._value = Arrays.differenceBy(this._value, arr2, criteria); return this } public intersectionBy(arr2: S[], criteria: (value: S, i?: number) => any): this { this._value = Arrays.intersectionBy(this._value, arr2, criteria); return this } public unionBy(arr2: S[], criteria: (value: S, i?: number) => any): this { this._value = Arrays.unionBy(this._value, arr2, criteria); return this } public map(predicate: (value: S, index: number, array: S[]) => U): Chain { this._value = this._value.map(predicate); return this as any } public filter(predicate: (value: S, index?: number, array?: S[]) => boolean): this { this._value = this._value.filter(predicate); return this } public find(predicate: (value: S, index?: number, array?: S[]) => boolean): S | undefined { return this._value.find(predicate); } public forEach(predicate: (value: S, index?: number, array?: S[]) => void): void { this._value.forEach(predicate); } public includes(searchElement: S, fromIndex?: number): boolean { return this._value.includes(searchElement, fromIndex); } public indexOf(searchElement: S, fromIndex?: number): number { return this._value.indexOf(searchElement, fromIndex); } public every(predicate: (value: S, index: number, array: S[]) => boolean): boolean { return this._value.every(predicate); } public some(predicate: (value: S, index: number, array: S[]) => boolean): boolean { return this._value.some(predicate); } public concat(...items: (K)[][]): this { this._value = this._value.concat(...items); return this; } public shift(): S { return this._value.shift() as S } public pop(): S { return this._value.pop() as S } public length(): number { return this._value.length } public push(...items: S[]): this { this._value.push(...items); return this; } public unshift(...items: S[]): this { this._value.unshift(...items); return this; } public keyBy(key?: string | ((item: S, index: number) => string)): { [index: string]: S } { return Arrays.keyBy(this._value, key) } public groupBy(key: string | number | ((item: S) => string | number)): { [index: string]: S[] } { return Arrays.groupBy(this._value, key); } public random(): S { return Arrays.random(this._value); } public randomItems(count: number = 1): S[] { return Arrays.randomItems(this._value, count); } public value(): S[] { return this._value as S[] } } export function _(arr: K[]): Chain export function _(arr: K): ChainObject export function _(arr: K[]): Chain | ChainObject { return Array.isArray(arr) ? new Chain(arr) : new ChainObject(arr as any) }