import type Arrayable from '../Contracts/Arrayable'; import type Jsonable from '../Contracts/Jsonable'; import type { MaybeArray } from './type'; export type Order> = { property: keyof T | ((item: T) => unknown); direction: 'asc' | 'desc'; }; export default class Collection implements Jsonable, Arrayable, Iterable, ArrayLike { /** * Collection item. */ [index: number]: T; /** * The constructor. * * @param {any=} items */ constructor(items?: MaybeArray); /** * The length of the collection. * * @type {number} */ length: number; /** * The iterator used for looping the collection. * * @type {Symbol.iterator} */ [Symbol.iterator](): Iterator; /** * Workaround to return a new current class. * * @param {any[]|Collection} items * * @protected * * @return {this} */ protected _newInstance(items?: MaybeArray): this; /** * Set the values in context. * * @param {Array} array * * @private * * @return {this} */ protected _setArray(array: T[]): this; /** * Return the first element in the collection, * if callback given the first element that passes the truth test. * Otherwise, undefined. * * @param {function} callback * * @return {undefined|any} */ first(callback?: (item: T, index: number) => boolean): T | undefined; /** * Return the last element in the collection, * if callback given the last element that passes the truth test. * Otherwise, undefined. * * @param {function} callback * * @return {undefined|any} */ last(callback?: (item: T, index: number) => boolean): T | undefined; /** * Return a random element(s) from the collection. * * @param {number} count * * @return {undefined|any|this} */ random(count?: number): T | this | undefined; /** * Randomise the order of elements in the collection using the * {@link https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm|Durstenfeld algorithm}. */ shuffle(): this; /** * Check that the collection the same as the given one. * * @param value * * @return {this} */ is(value: unknown): value is this; /** * Assert whether the collection * is empty or not. * * @return {boolean} */ isEmpty(): boolean; /** * Assert whether the collection * is not empty or is empty. *. * @return {boolean} */ isNotEmpty(): boolean; /** * Assert whether there are duplicates * in the collection based deep equality. * * @param {string|undefined} key * * @return {boolean} */ hasDuplicates(key?: string | ((obj: T) => T)): boolean; /** * De-duplicate the collection. * Optionally find duplicates by key or * the return value of a method called with the element. * * @param {string|function|undefined} key * * @return {this} */ unique(key?: string | ((obj: T) => T)): this; /** * Only keep the duplicated values in the collection. * Optionally if all items are object compare * by the given key or function. * * @param {string|function|undefined} key * * @return {this} */ duplicates(key?: string | ((obj: T) => T)): this; /** * Remove all items that are deep equal to the argument. * * @param {any} item * * @return {this} */ delete(item: T): this; /** * Only keep every nth element in the collection. * * @param {number} every * * @return {this} */ nth(every: number): this; /** * Filter out null and undefined values. * * @return {this} */ withoutEmpty(): this; /** * Pad collection to the specified length with a value. * Negative length will pad the beginning of the collection. * * @param {number|function} length * @param {any|function|undefined} value * * @return {this} */ pad(length: number, value?: T | (() => T)): this; /** * Join the collection and one or more iterables without overlapping values. * * @param {array|Collection} iterables * * @return {this} */ union(...iterables: (Collection | MaybeArray)[]): this; /** * Diff the collection with the given items. * * @param {any|any[]} values * * @return {this} */ diff(values: MaybeArray): this; /** * Intersect the collection with the given values. * * @param {any|any[]} values * * @return {this} */ intersect(values: MaybeArray): this; /** * Chunk the collection into chunks of the given size. * * @param {number} size * * @return {Collection} */ chunk(size: number): Collection>; /** * Chunk the collection by the specified key. */ chunkBy(key: K | ((item: T) => PropertyKey)): Record>; /** * Sort the items into a collection of their own based on * weather they pass the given truthfulness test. * * @param callback */ partition(callback: (item: T) => boolean): [this, this]; /** * Call a callback on the collection * when the first argument is Boolean(true) or * a closure called with the collection * resolving to a value converted to boolean. * * @param {function|boolean} boolean * @param {function} callback * * @return {this} */ when(boolean: boolean | ((collection: Collection) => boolean), callback: (collection: this) => this): this; /** * Call a callback on the collection * unless the first argument is Boolean(true) or * a closure called with the collection * resolving to a value converted to boolean. * * @param {function|boolean} boolean * @param {function} callback * * @return {this} */ unless(boolean: boolean | ((collection: Collection) => boolean), callback: (collection: this) => this): this; /** * Call the given callback with the collection * if the collection is empty. * * @param {function} callback * @return {this} */ whenEmpty(callback: (collection: this) => void): this; /** * Call the given callback with the collection * if the collection is not empty. * * @param {function} callback * @return {this} */ whenNotEmpty(callback: (collection: this) => void): this; /** * Return the specified number of elements from the collection's * start or end on negative argument. * * @param {number} count * * @return {this} */ take(count: number): this; /** * Take items in the collection until the given closure * with the current item resolves to false. * * @param {function} closure * * @return {this} */ takeUntil(closure: (item: any) => boolean): this; /** * Take items in the collection while the given closure * with the current item resolves to true. * * @param {function} closure * * @return {this} */ takeWhile(closure: (item: any) => boolean): this; /** * Skip items in the collection until the specified count * from the start or end based on the argument. * * @param {number} count * * @return {this} */ skip(count: number): this; /** * Skip items in the collection until the given closure * with the current item resolves to false. * * @param {function} closure * * @return {this} */ skipUntil(closure: (item: T) => boolean): this; /** * Skip items in the collection while the given closure * with the current item resolves to true. * * @param {function} closure * * @return {this} */ skipWhile(closure: (item: T) => boolean): this; /** * Get a collection with the values of a given key. * * @param {string|string[]} properties * * @return {this} * * @throws {Error} */ pluck)[]>(properties: Keys): Collection>; pluck>(properties: Keys): Collection>; pluck(properties: MaybeArray): Collection; /** * Pass a clone of the collection to a given function. * * @param {function} closure * * @return {this} */ tap(closure: (collection: Collection) => void): this; /** * Pass the collection to a given function. * * @param {function} closure * * @return {this} */ pipe(closure: (collection: this) => this): this; /** * Print the collection values to the console. * * @param {string=} message * @return {this} */ dump(message?: string): this; /** * Get the indexes for the collection items */ keys(): string[]; /** * Order the collection by given configurations(s) * * @param {Order | Order[]} order * @param {Order[]=} additional * * @return {this} */ orderBy(order: T extends Record ? MaybeArray> : never, ...additional: T extends Record ? Order[] : never): this; /** * The getter that returns the numeric values based * on the given key or the getter function. * * @param {string|function} key * * @private */ private getNumericValues; /** * Get summative of the collection values. * * @param {string|function} key */ sum(key?: string | ((item: T) => any)): number; /** * Get the highest number in the collection. * * @param {string|function} key */ max(key?: string | ((item: T) => any)): number; /** * Get the lowest number in the collection. * * @param {string|function} key */ min(key?: string | ((item: T) => any)): number; /** * Get the average of values in the collection. * * @param {string|function} key */ average(key?: string | ((item: T) => any)): number; /** * @inheritDoc */ toArray(): T[]; /** * @inheritDoc */ toJSON(): { elements: ReturnType[]; }; /** * Asserts whether the given value * is an instance of Collection. * * @param {any} value * * @return {boolean} */ static isCollection(value: unknown): value is Collection; /** * Create a new collection from the evaluated * callback or value the given number of times. * * @param {number} number * @param {any|function} value * * @return {this} */ static times(number: number, value: ST | ((index: number) => ST)): Collection; /** * Assert whether the given value is in the collection using deep equality. * * @param {any} value * * @return {boolean} */ includes(value: unknown): boolean; /** * @override * * @param {function} callback * @param {object} thisArg * * @return {this} */ forEach(callback: (value: T, index: number, array: T[]) => void, thisArg?: any[]): this; /** * @see Array.prototype.map * * @return {this} */ map(callback: (value: T, index: number, array: T[]) => U, thisArg?: any): Collection; /** * @see Array.prototype.reverse * * @return {this} */ reverse(): this; /** * @see Array.prototype.concat * * @return {this} */ concat(...items: ConcatArray[]): this; /** * @see Array.prototype.sort * * @return {this} */ sort(compareFn?: (a: T, b: T) => number): this; /** * @see Array.prototype.splice * * @return {Collection} */ splice(start: number, deleteCount: number, ...items: T[]): this; /** * @see Array.prototype.slice * * @return {this} */ slice(start?: number, end?: number): this; /** * @see Array.prototype.filter * * @return {this} */ filter(predicate: (value: T, index: number, array: T[]) => boolean, thisArg?: any): this; /** * @see Array.prototype.flat * * @return {Collection} */ flat(depth?: number): Collection; /** * @see Array.prototype.flatMap * * @return {Collection} */ flatMap(callback: (this: This, value: T, index: number, array: T[]) => U | readonly U[], thisArg?: This): Collection; /** * @see Array.prototype.shift * * @return {any} */ shift(): T | undefined; /** * @see Array.prototype.unshift * * @return {number} */ unshift(...items: T[]): number; /** * @see Array.prototype.pop * * @return {any} */ pop(): T | undefined; /** * @see Array.prototype.push * * @return {number} */ push(...items: T[]): number; /** * @see Array.prototype.fill * * @return {this} */ fill(value: T, start?: number, end?: number): this; /** * @see Array.prototype.copyWithin * * @return {this} */ copyWithin(target: number, start: number, end?: number): this; /** * @see Array.prototype.every * * @return {boolean} */ every(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[]; every(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; /** * @see Array.prototype.some * * @return {boolean} */ some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; /** * @see Array.prototype.find * * @return {any} */ find(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined; /** * @see Array.prototype.findLast * * @return {any} */ findLast(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined; /** * @see Array.prototype.findIndex * * @return {number} */ findIndex(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): number; /** * @see Array.prototype.findLastIndex * * @return {number} */ findLastIndex(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): number; /** * @see Array.prototype.indexOf * * @return {boolean} */ indexOf(searchElement: T, fromIndex?: number): number; /** * @see Array.prototype.join * * @return {string} */ join(key?: string | ((item: T) => any), separator?: string | ((item: T) => any) | undefined): string; /** * @see Array.prototype.toString * * @return {string} */ toString(): string; /** * @see Array.prototype.lastIndexOf * * @return {number} */ lastIndexOf(searchElement: T, fromIndex?: number): number; /** * @see Array.prototype.reduce * * @return {any} */ reduce(callback: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; /** * @see Array.prototype.reduceRight * * @return {any} */ reduceRight(callback: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; /** * @see Array.prototype.with * * @return {any} */ with(index: number, value: T): this; /** * @see Array.prototype.at */ at(index: number): T | undefined; /** * @see Array.prototype.entries */ entries(): IterableIterator<[number, T]>; /** * @see Array.prototype.values */ values(): IterableIterator; /** * Determine whether all the values in this are objects. * * @return {boolean} * * @protected */ protected _allAreObjects(): this is Collection>; } //# sourceMappingURL=Collection.d.ts.map