super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
89 lines (88 loc) • 2.24 kB
TypeScript
/**
* Creates a slice of array with n elements taken from the beginning.
*
* @param array - The array to query
* @param n - The number of elements to take
* @returns The slice of array
*
* @example
* ```ts
* take([1, 2, 3]);
* // => [1]
*
* take([1, 2, 3], 2);
* // => [1, 2]
*
* take([1, 2, 3], 5);
* // => [1, 2, 3]
*
* take([1, 2, 3], 0);
* // => []
* ```
*/
export declare function take<T>(array: T[], n?: number): T[];
/**
* Creates a slice of array with n elements taken from the end.
*
* @param array - The array to query
* @param n - The number of elements to take
* @returns The slice of array
*
* @example
* ```ts
* takeRight([1, 2, 3]);
* // => [3]
*
* takeRight([1, 2, 3], 2);
* // => [2, 3]
*
* takeRight([1, 2, 3], 5);
* // => [1, 2, 3]
*
* takeRight([1, 2, 3], 0);
* // => []
* ```
*/
export declare function takeRight<T>(array: T[], n?: number): T[];
/**
* Creates a slice of array with elements taken from the beginning.
* Elements are taken until predicate returns falsey.
*
* @param array - The array to query
* @param predicate - The function invoked per iteration
* @returns The slice of array
*
* @example
* ```ts
* const users = [
* { 'user': 'barney', 'active': false },
* { 'user': 'fred', 'active': false },
* { 'user': 'pebbles', 'active': true }
* ];
*
* takeWhile(users, function(o) { return !o.active; });
* // => objects for ['barney', 'fred']
* ```
*/
export declare function takeWhile<T>(array: T[], predicate: (value: T, index: number, array: T[]) => boolean): T[];
/**
* Creates a slice of array with elements taken from the end.
* Elements are taken until predicate returns falsey.
*
* @param array - The array to query
* @param predicate - The function invoked per iteration
* @returns The slice of array
*
* @example
* ```ts
* const users = [
* { 'user': 'barney', 'active': true },
* { 'user': 'fred', 'active': false },
* { 'user': 'pebbles', 'active': false }
* ];
*
* takeRightWhile(users, function(o) { return !o.active; });
* // => objects for ['fred', 'pebbles']
* ```
*/
export declare function takeRightWhile<T>(array: T[], predicate: (value: T, index: number, array: T[]) => boolean): T[];