flooent
Version:
Fluent interface to provide an expressive syntax for common manipulations.
143 lines (142 loc) • 5.7 kB
TypeScript
/**
* Returns the items until either the given value is found, or the given callback returns `true`.
*/
export declare function until<T>(value: T[], comparison: ((item: T, index: number) => boolean) | T): any[];
/**
* Return all items that don't pass the given truth test. Inverse of `Array.filter`.
*/
export declare function reject<T>(value: T[], callback: (item: T, index?: number) => boolean): T[];
/**
* Moves an item in the array using the given source index to either "before" or "after" the given target.
*/
export declare function move<T>(value: T[], source: number | 'first' | 'last', position: 'before' | 'after', target: number | 'first' | 'last'): any[];
/**
* Breaks the array into multiple, smaller arrays of a given size.
*/
export declare function chunk<T>(value: T[], n: any): any[];
/**
* Returns the items for the given page and size.
*/
export declare function forPage<T>(value: T[], page: number, size: number): T[];
/**
* Fills up the array with the given value.
*/
export declare function pad<T>(value: T[], size: number, paddedValue: T): T[];
/**
* Points to a specific index inside the array to do further actions on it.
*/
export declare function point<T>(value: T[], indexOrFn: number | ((item: T) => boolean)): {
/**
* Sets the value at the current index and returns a new array.
*/
set(callback: (item: T) => T): T[];
/**
* Appends given value to array in between the currently pointed item and its next item and returns a new array.
*/
append(...items: T[]): T[];
/**
* Prepends given value to array in between the currently pointed item and its previous item and returns a new array.
*/
prepend(...items: T[]): T[];
/**
* Removes the current index and returns a new array.
*/
remove(): T[];
/**
* Returns value for current pointer position.
*/
value(): T;
index(): number;
/**
* Splits the array at the current index
*/
split(): T[][];
/**
* Steps forward or backward given the number of steps.
*/
step(steps: number): any;
};
/**
* Filters array by given value or key/value pair.
*/
export declare function where<T>(array: T[], keyOrValue: any, value: any): T[];
/**
* Removes items from array by the given key or key/value pair.
*/
export declare function whereNot<T>(array: T[], keyOrValue: any, value: any): T[];
/**
* Filters array by given values or key/values pair.
*/
export declare function whereIn<T>(array: T[], keyOrValue: any, value: any): T[];
/**
* Removes items from array by the given value or key/values pair.
*/
export declare function whereNotIn<T>(array: T[], keyOrValue: any, value: any): T[];
/**
* Only returns items which are not empty.
*/
export declare function filled<T>(value: T[], key?: string): T[];
/**
* Mutates the original array with the return value of the given callback.
*/
export declare function mutate<T>(value: T[], callback: ((array: T[]) => T[])): T[];
/**
* Keys the collection by the given key. If multiple items have the same key, only the last one will appear in the new collection.
*/
export declare function keyBy<T, K extends keyof T>(value: T[], key: K | ((item: T, index: number) => T[K])): Map<T[K], T>;
/**
* Returns the sum of the array.
* For arrays of objects: Pass field or callback as argument.
*/
export declare function sum<T, K extends keyof T>(value: T[], key?: K | ((item: T, index: number) => number)): number;
/**
* Creates an array of the specified length and populates it using the callback function.
*/
export declare function sized<T = void>(length: number, callback: (index: number) => T): T[];
/**
* Omits given keys from all objects in the array.
*/
export declare function omit<T>(value: T[], keys: string[]): T[];
/**
* Pluck the given field out of each object in the array.
*/
export declare function pluck<T>(value: T[], key: keyof T): T[keyof T][];
/**
* Returns array of unique values.
* For array ob objects: Pass key or callback to use it for the comparison.
*/
export declare function unique<T, K extends keyof T>(value: T[], comparison?: K | ((item: T, index: number) => any)): any[];
/**
* Shuffles and returns a new array.
*/
export declare function shuffle<T>(value: T[]): T[];
/**
* Returns a tuple separating the items that pass the given truth test.
*/
export declare function partition<T>(value: T[], callback: (item: T, index: number) => boolean): [T[], T[]];
/**
* Prepends the given items to the array. Unlike `unshift`, it is immutable and returns a new array.
*/
export declare function prepend<T>(value: T[], ...items: T[]): T[];
/**
* Appends the given items to the array. Unlike `push`, it is immutable and returns a new array.
*/
export declare function append<T>(value: T[], ...items: T[]): T[];
/**
* Sorts an array in descending order and **returns a new array**.
* For array of objects: Pass index, field or callback to use it for sorting.
*/
export declare function sortDesc<T, K extends keyof T>(value: T[], key?: K | number | ((item: T) => any)): T[];
/**
* Sorts an array in ascending order and **returns a new array**.
* For array of objects: Pass index, field or callback to use it for sorting.
*/
export declare function sortAsc<T, K extends keyof T>(value: T[], key?: K | number | ((item: T, index: number) => any)): T[];
/**
* Turns an array in the structure of `[ ['key', 'value'] ]` into a map.
*/
export declare function toMap<T>(value: T[]): Map<unknown, unknown>;
/**
* Turns the given array into a map with each element becoming a key in the map.
*/
export declare function toKeyedMap<T, DV>(array: T[], defaultValueOrCallback: DV | ((item: T) => DV)): Map<T, DV>;