UNPKG

2.88 kBTypeScriptView Raw
1/**
2 * Helper to find the index of an item within an array, using a callback to
3 * determine the match.
4 *
5 * @public
6 * @param array - Array to search.
7 * @param cb - Callback which returns true on matches.
8 * @param fromIndex - Optional index to start from (defaults to 0)
9 */
10export declare function findIndex<T>(array: T[], cb: (item: T, index: number) => boolean, fromIndex?: number): number;
11/**
12 * Helper to find the first item within an array that satisfies the callback.
13 * @param array - Array to search
14 * @param cb - Callback which returns true on matches
15 */
16export declare function find<T>(array: T[], cb: (item: T, index: number) => boolean): T | undefined;
17/**
18 * Creates an array of a given size and helper method to populate.
19 *
20 * @public
21 * @param size - Size of array.
22 * @param getItem - Callback to populate given cell index.
23 */
24export declare function createArray<T>(size: number, getItem: (index: number) => T): T[];
25/**
26 * Convert the given array to a matrix with columnCount number
27 * of columns.
28 *
29 * @public
30 * @param items - The array to convert
31 * @param columnCount - The number of columns for the resulting matrix
32 * @returns A matrix of items
33 */
34export declare function toMatrix<T>(items: T[], columnCount: number): T[][];
35/**
36 * Given an array, it returns a new array that does not contain the item at the given index.
37 * @param array - The array to operate on
38 * @param index - The index of the element to remove
39 */
40export declare function removeIndex<T>(array: T[], index: number): T[];
41/**
42 * Given an array, this function returns a new array where the element at a given index has been replaced.
43 * @param array - The array to operate on
44 * @param newElement - The element that will be placed in the new array
45 * @param index - The index of the element that should be replaced
46 */
47export declare function replaceElement<T>(array: T[], newElement: T, index: number): T[];
48/**
49 * Given an array, this function returns a new array where an element has been inserted at the given index.
50 * @param array - The array to operate on
51 * @param index - The index where an element should be inserted
52 * @param itemToAdd - The element to insert
53 */
54export declare function addElementAtIndex<T>(array: T[], index: number, itemToAdd: T): T[];
55/**
56 * Given an array where each element is of type T or T[], flatten it into an array of T
57 * @param array - The array where each element can optionally also be an array
58 */
59export declare function flatten<T>(array: (T | T[])[]): T[];
60/**
61 * Returns a boolean indicating if the two given arrays are equal in length and values.
62 *
63 * @param array1 - First array to compare
64 * @param array2 - Second array to compare
65 * @returns True if the arrays are the same length and have the same values in the same positions, false otherwise.
66 */
67export declare function arraysEqual<T>(array1: T[], array2: T[]): boolean;