UNPKG

ndarray-methods

Version:

Convenient methods for JavaScript built-in multi-dimensional arrays.

624 lines 68.4 kB
import { Infinity, MDArray, BuildIndices, Indices, Shape, Map, ElementType, Parent, Cast } from "."; declare global { interface Array<T> { /** * Builds a nested array with a specific shape and fills the array with the result of a defined map function. * ```js * [2, 3].buildShape((x, y) => x * 3 + y); // [[0, 1, 2], [3, 4, 5]] * ``` * @param this The shape of the array. * @param mapfn A function that accepts the coordinates of the element. * The `buildShape` method calls the `mapfn` function one time for each position in the array. * @param thisArg An object to which the `this` keyword can refer in the `mapfn` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The array built with the specific shape. */ buildShape<const A extends number[], T, const This = undefined>(this: A, mapfn: (this: This, ...indices: BuildIndices<A>) => T, thisArg?: This): MDArray<T, A>; /** * Builds a nested array with a specific shape and fills the array with a static value. * ```js * [2, 3].buildShape(10); // [[10, 10, 10], [10, 10, 10]] * ``` * @param this The shape of the array. * @param value The value to fill the array with. * @returns The array built with the specific shape. */ buildShape<const A extends number[], T>(this: A, value: T): MDArray<T, A>; /** * Gets the length of each axis of a nested array. The `shape` method returns the shape at the deepest element. * ```js * [[0, 1, 2], [3, 4, 5]].shape(); // [2, 3] * [[0, 1], [2, [3, 4], 5]].shape(); // [2, 3, 2] * ``` * For a non-variable length array, use the `shapeAtOrigin` method instead for a better performance. * @param this The original array. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A number array containing the lengths of each axis of the array. */ shape<A extends unknown[], const M extends number = Infinity>(this: A, maxDepth?: M): Shape<A, M>; /** * Gets the length of each axis of a nested array. The `shapeAtOrigin` method only checks the first element recursively. * ```js * [[0, 1, 2], [3, 4, 5]].shapeAtOrigin(); // [2, 3] * [[0, 1], [2, [3, 4], 5]].shapeAtOrigin(); // [2, 2] * ``` * For a variable length array, use the `shape` method instead to get the shape at the deepest element. * @param this The original array. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A number array containing the lengths of each axis of the array. */ shapeAtOrigin<A extends unknown[], const M extends number = Infinity>(this: A, maxDepth?: M): Shape<A, M>; /** * Calls a defined callback function on each element in a nested array, and returns a deeply-cloned array that contains the results. * ```js * [[0, 1, 2], [3, 4, 5]].nestedMap(n => n + 10); // [[10, 11, 12], [13, 14, 15]] * ``` * @param this The original array. * @param callbackfn A function that accepts up to 4 arguments. * The `nestedMap` method calls the `callbackfn` function one time for each element in the array. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `callbackfn` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The mapped array. */ nestedMap<A extends unknown[], U, const M extends number = Infinity, const This = undefined>(this: A, callbackfn: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => U, maxDepth?: M, thisArg?: This): Map<A, U, M>; /** * Performs the specified action for each element in a nested array. * ```js * [[0, 1, 2], [3, 4, 5]].nestedForEach(n => console.log(n)); // Prints 0 to 5 * ``` * @param this The original array. * @param callbackfn A function that accepts up to 4 arguments. * The `nestedForEach` method calls the `callbackfn` function one time for each element in the array. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `callbackfn` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. */ nestedForEach<A extends unknown[], U, const M extends number = Infinity, const This = undefined>(this: A, callbackfn: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => U, maxDepth?: M, thisArg?: This): void; /** * Splits a string into substrings using the specified separators for each axis and return them as a nested array. * ```js * [/,|;/, ""].nestedSplit("AB,CD;EF"); // [["A", "B"], ["C", "D"], ["E", "F"]] * ``` * @param this An array of separators to use in separating the string. * @param content The string to split. * @returns The splitted string as a nested array. */ nestedSplit<const A extends (string | RegExp)[]>(this: A, content: string): MDArray<string, A>; /** * Concatenates all the elements in a nested array into a string, separated by the specified separator strings for each axis. * ```js * [",", ""].nestedJoin([[0, 1, 2], [3, 4, 5]]); // "012,345" * ``` * @param this A string used to separate one element of the array from the next in the resulting string. * If a certain separator is `undefined`, a comma (`,`) is used instead for that axis. * @param content The array to join. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A string with all the elements concatenated. */ nestedJoin<const A extends string[]>(this: A, content: MDArray<unknown, A>, maxDepth?: number): string; /** * Changes all elements in a nested array from `startIndices` to `endIndices` to a static value in place and returns the array. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFill(10); // [[10, 10, 10], [10, 10, 10]] * [[0, 1, 2], [3, 4, 5]].nestedFill(10, [0, 0], [2, 2]); // [[10, 10, 2], [10, 10, 5]] * ``` * If both `startIndices` and `endIndices` are omitted, all the elements will be replaced to the specified value. * @param this The original array. * @param value The value to fill the section of the array with. * @param startIndices The coordinates to start filling the array at (inclusive). * If a certain start index is negative, the length of that axis of the array will be added to it. * @param endIndices The coordinates to stop filling the array at (exclusive). * If a certain end index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns The modified array, which the instance is the same as the original array. */ nestedFill<A extends unknown[], const M extends number = Infinity>(this: A, value: ElementType<A, M>, startIndices?: Indices<A, M>, endIndices?: Indices<A, M>, maxDepth?: M): A; /** * Calls a defined callback function on all elements in a nested array from `startIndices` to `endIndices` in place and returns the array. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFillMap(n => n + 10); // [[10, 11, 12], [13, 14, 15]] * [[0, 1, 2], [3, 4, 5]].nestedFillMap(n => n + 10, [0, 0], [2, 2]); // [[10, 11, 2], [13, 14, 5]] * ``` * If both `startIndices` and `endIndices` are omitted, the result is the same as the `nestedMap` method performed in place. * @param this The original array. * @param callbackfn A function that accepts up to 4 arguments. * The `nestedFillMap` method calls the `callbackfn` function one time for each element in the array. * @param startIndices The coordinates to start filling the array at (inclusive). * If a certain start index is negative, the length of that axis of the array will be added to it. * @param endIndices The coordinates to stop filling the array at (exclusive). * If a certain end index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `callbackfn` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The modified array, which the instance is the same as the original array. */ nestedFillMap<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, callbackfn: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => ElementType<A, M>, startIndices?: Indices<A, M>, endIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): A; /** * Determines whether a nested array includes a specified element, searching forwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedIncludes(3); // true * [[0, 1, 2], [3, 4, 5]].nestedIncludes(3, [0, 1]); // false * ``` * If the element is more likely to appear near the end of the array, use the `nestedIncludesFromLast` method instead for a better performance. * @param this The original array. * @param searchElement The element to search for. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns `true` if the element is found in the array, or `false` otherwise. */ nestedIncludes<A extends unknown[], const M extends number = Infinity>(this: A, searchElement: ElementType<A, M>, fromIndices?: Indices<A, M>, maxDepth?: M): boolean; /** * Determines whether a nested array includes a specified element, searching backwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedIncludesFromLast(2); // true * [[0, 1, 2], [3, 4, 5]].nestedIncludesFromLast(2, [1, 1]); // false * ``` * If the element is more likely to appear near the start of the array, use the `nestedIncludes` method instead for a better performance. * @param this The original array. * @param searchElement The element to search for. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns `true` if the element is found in the array, or `false` otherwise. */ nestedIncludesFromLast<A extends unknown[], const M extends number = Infinity>(this: A, searchElement: ElementType<A, M>, fromIndices?: Indices<A, M>, maxDepth?: M): boolean; /** * Returns the coordinates of the first occurrence of a specified value in an array, or `undefined` if it is not present. * ```js * [[0, 1, 2], [3, 4, 5]].nestedIndexOf(3); // [1, 0] * [[0, 1, 2], [3, 4, 5]].nestedIndexOf(3, [0, 1]); // undefined * ``` * If the element is more likely to appear near the end of the array, use the `nestedLastIndexOf` method instead for a better performance. * @param this The original array. * @param searchElement The element to search for. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A number array containing the coordinates of the element, or `undefined` if it is not present. */ nestedIndexOf<A extends unknown[], const M extends number = Infinity>(this: A, searchElement: ElementType<A, M>, fromIndices?: Indices<A, M>, maxDepth?: M): Indices<A, M> | undefined; /** * Returns the coordinates of the last occurrence of a specified value in an array, or `undefined` if it is not present. * ```js * [[0, 1, 2], [3, 4, 5]].nestedLastIndexOf(2); // [0, 2] * [[0, 1, 2], [3, 4, 5]].nestedLastIndexOf(2, [1, 1]); // undefined * ``` * If the element is more likely to appear near the start of the array, use the `nestedIndexOf` method instead for a better performance. * @param this The original array. * @param searchElement The element to search for. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A number array containing the coordinates of the element, or `undefined` if it is not present. */ nestedLastIndexOf<A extends unknown[], const M extends number = Infinity>(this: A, searchElement: ElementType<A, M>, fromIndices?: Indices<A, M>, maxDepth?: M): Indices<A, M> | undefined; /** * Returns the value of the first element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFind(n => n % 6 == 3); // 3 * [[0, 1, 2], [3, 4, 5]].nestedFind(n => n % 6 == 3, [0, 1]); // undefined * ``` * If the element is more likely to appear near the end of the array, use the `nestedFindLast` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFind` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The value of the first element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFind<A extends unknown[], S extends ElementType<A, M>, const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => value is S, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): S | undefined; /** * Returns the value of the first element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFind(n => n % 6 == 3); // 3 * [[0, 1, 2], [3, 4, 5]].nestedFind(n => n % 6 == 3, [0, 1]); // undefined * ``` * If the element is more likely to appear near the end of the array, use the `nestedFindLast` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFind` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The value of the first element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFind<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): ElementType<A, M> | undefined; /** * Returns the value of the last element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFindLast(n => n % 6 == 2); // 2 * [[0, 1, 2], [3, 4, 5]].nestedFindLast(n => n % 6 == 2, [1, 1]); // undefined * ``` * If the element is more likely to appear near the start of the array, use the `nestedFind` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFindLast` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The value of the last element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFindLast<A extends unknown[], S extends ElementType<A, M>, const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => value is S, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): S | undefined; /** * Returns the value of the last element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFindLast(n => n % 6 == 2); // 2 * [[0, 1, 2], [3, 4, 5]].nestedFindLast(n => n % 6 == 2, [1, 1]); // undefined * ``` * If the element is more likely to appear near the start of the array, use the `nestedFind` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFindLast` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The value of the last element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFindLast<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): ElementType<A, M> | undefined; /** * Returns the coordinates of the first element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFindIndex(n => n % 6 == 3); // [1, 0] * [[0, 1, 2], [3, 4, 5]].nestedFindIndex(n => n % 6 == 3, [0, 1]); // undefined * ``` * If the element is more likely to appear near the end of the array, use the `nestedFindLastIndex` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFindIndex` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The coordinates of the first element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFindIndex<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): Indices<A, M> | undefined; /** * Returns the coordinates of the last element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFindLastIndex(n => n % 6 == 2); // [0, 2] * [[0, 1, 2], [3, 4, 5]].nestedFindLastIndex(n => n % 6 == 2, [1, 1]); // undefined * ``` * If the element is more likely to appear near the start of the array, use the `nestedFindIndex` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFindLastIndex` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The coordinates of the last element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFindLastIndex<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): Indices<A, M> | undefined; /** * Determines whether at least one element in a nested array satisfies the `predicate` function, searching forwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedSome(n => n % 6 == 3); // true * [[0, 1, 2], [3, 4, 5]].nestedSome(n => n % 6 == 3, [0, 1]); // false * ``` * If the element is more likely to appear near the end of the array, use the `nestedSomeFromLast` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedSome` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns `true` if at least one element in the array satisfies the `predicate` function, or `false` otherwise. */ nestedSome<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): boolean; /** * Determines whether at least one element in a nested array satisfies the `predicate` function, searching backwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedSomeFromLast(n => n % 6 == 2); // true * [[0, 1, 2], [3, 4, 5]].nestedSomeFromLast(n => n % 6 == 2, [1, 1]); // false * ``` * If the element is more likely to appear near the start of the array, use the `nestedSome` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedSomeFromLast` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns `true` if at least one element in the array satisfies the `predicate` function, or `false` otherwise. */ nestedSomeFromLast<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): boolean; /** * Determines whether all elements in a nested array satisfies the `predicate` function, searching forwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedEvery(n => n % 6 != 3); // false * [[0, 1, 2], [3, 4, 5]].nestedEvery(n => n % 6 != 3, [0, 1]); // true * ``` * If the counter-element is more likely to appear near the end of the array, use the `nestedEveryFromLast` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedEvery` method calls the `predicate` function one time for each element in the array until it returns a falsy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns `true` if all elements in the array satisfies the `predicate` function, or `false` otherwise. */ nestedEvery<A extends unknown[], S extends ElementType<A, M>, const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => value is S, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): this is Cast<Map<A, S, M>, A>; /** * Determines whether all elements in a nested array satisfies the `predicate` function, searching forwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedEvery(n => n % 6 != 3); // false * [[0, 1, 2], [3, 4, 5]].nestedEvery(n => n % 6 != 3, [0, 1]); // true * ``` * If the counter-element is more likely to appear near the end of the array, use the `nestedEveryFromLast` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedEvery` method calls the `predicate` function one time for each element in the array until it returns a falsy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns `true` if all elements in the array satisfies the `predicate` function, or `false` otherwise. */ nestedEvery<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): boolean; /** * Determines whether all elements in a nested array satisfies the `predicate` function, searching backwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedEveryFromLast(n => n % 6 != 2); // false * [[0, 1, 2], [3, 4, 5]].nestedEveryFromLast(n => n % 6 != 2, [1, 1]); // true * ``` * If the counter-element is more likely to appear near the start of the array, use the `nestedEvery` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedEvery` method calls the `predicate` function one time for each element in the array until it returns a falsy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns `true` if all elements in the array satisfies the `predicate` function, or `false` otherwise. */ nestedEveryFromLast<A extends unknown[], S extends ElementType<A, M>, const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => value is S, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): this is Cast<Map<A, S, M>, A>; /** * Determines whether all elements in a nested array satisfies the `predicate` function, searching backwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedEveryFromLast(n => n % 6 != 2); // false * [[0, 1, 2], [3, 4, 5]].nestedEveryFromLast(n => n % 6 != 2, [1, 1]); // true * ``` * If the counter-element is more likely to appear near the start of the array, use the `nestedEvery` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedEvery` method calls the `predicate` function one time for each element in the array until it returns a falsy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns `true` if all elements in the array satisfies the `predicate` function, or `false` otherwise. */ nestedEveryFromLast<A extends unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): boolean; } interface ReadonlyArray<T> { /** * Builds a nested array with a specific shape and fills the array with the result of a defined map function. * ```js * [2, 3].buildShape((x, y) => x * 3 + y); // [[0, 1, 2], [3, 4, 5]] * ``` * @param this The shape of the array. * @param mapfn A function that accepts the coordinates of the element. * The `buildShape` method calls the `mapfn` function one time for each position in the array. * @param thisArg An object to which the `this` keyword can refer in the `mapfn` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The array built with the specific shape. */ buildShape<const A extends readonly number[], T, const This = undefined>(this: A, mapfn: (this: This, ...indices: BuildIndices<A>) => T, thisArg?: This): MDArray<T, A>; /** * Builds a nested array with a specific shape and fills the array with a static value. * ```js * [2, 3].buildShape(10); // [[10, 10, 10], [10, 10, 10]] * ``` * @param this The shape of the array. * @param value The value to fill the array with. * @returns The array built with the specific shape. */ buildShape<const A extends readonly number[], T>(this: A, value: T): MDArray<T, A>; /** * Gets the length of each axis of a nested array. The `shape` method returns the shape at the deepest element. * ```js * [[0, 1, 2], [3, 4, 5]].shape(); // [2, 3] * [[0, 1], [2, [3, 4], 5]].shape(); // [2, 3, 2] * ``` * For a non-variable length array, use the `shapeAtOrigin` method instead for a better performance. * @param this The original array. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A number array containing the lengths of each axis of the array. */ shape<A extends readonly unknown[], const M extends number = Infinity>(this: A, maxDepth?: M): Shape<A, M>; /** * Gets the length of each axis of a nested array. The `shapeAtOrigin` method only checks the first element recursively. * ```js * [[0, 1, 2], [3, 4, 5]].shapeAtOrigin(); // [2, 3] * [[0, 1], [2, [3, 4], 5]].shapeAtOrigin(); // [2, 2] * ``` * For a variable length array, use the `shape` method instead to get the shape at the deepest element. * @param this The original array. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A number array containing the lengths of each axis of the array. */ shapeAtOrigin<A extends readonly unknown[], const M extends number = Infinity>(this: A, maxDepth?: M): Shape<A, M>; /** * Calls a defined callback function on each element in a nested array, and returns a deeply-cloned array that contains the results. * ```js * [[0, 1, 2], [3, 4, 5]].nestedMap(n => n + 10); // [[10, 11, 12], [13, 14, 15]] * ``` * @param this The original array. * @param callbackfn A function that accepts up to 4 arguments. * The `nestedMap` method calls the `callbackfn` function one time for each element in the array. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `callbackfn` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The mapped array. */ nestedMap<A extends readonly unknown[], U, const M extends number = Infinity, const This = undefined>(this: A, callbackfn: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => U, maxDepth?: M, thisArg?: This): Map<A, U, M>; /** * Performs the specified action for each element in a nested array. * ```js * [[0, 1, 2], [3, 4, 5]].nestedForEach(n => console.log(n)); // Prints 0 to 5 * ``` * @param this The original array. * @param callbackfn A function that accepts up to 4 arguments. * The `nestedForEach` method calls the `callbackfn` function one time for each element in the array. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `callbackfn` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. */ nestedForEach<A extends readonly unknown[], U, const M extends number = Infinity, const This = undefined>(this: A, callbackfn: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => U, maxDepth?: M, thisArg?: This): void; /** * Splits a string into substrings using the specified separators for each axis and return them as a nested array. * ```js * [/,|;/, ""].nestedSplit("AB,CD;EF"); // [["A", "B"], ["C", "D"], ["E", "F"]] * ``` * @param this An array of separators to use in separating the string. * @param content The string to split. * @returns The splitted string as a nested array. */ nestedSplit<const A extends readonly (string | RegExp)[]>(this: A, content: string): MDArray<string, A>; /** * Concatenates all the elements in a nested array into a string, separated by the specified separator strings for each axis. * ```js * [",", ""].nestedJoin([[0, 1, 2], [3, 4, 5]]); // "012,345" * ``` * @param this A string used to separate one element of the array from the next in the resulting string. * If a certain separator is `undefined`, a comma (`,`) is used instead for that axis. * @param content The array to join. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A string with all the elements concatenated. */ nestedJoin<const A extends readonly string[]>(this: A, content: MDArray<unknown, A>, maxDepth?: number): string; /** * Determines whether a nested array includes a specified element, searching forwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedIncludes(3); // true * [[0, 1, 2], [3, 4, 5]].nestedIncludes(3, [0, 1]); // false * ``` * If the element is more likely to appear near the end of the array, use the `nestedIncludesFromLast` method instead for a better performance. * @param this The original array. * @param searchElement The element to search for. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns `true` if the element is found in the array, or `false` otherwise. */ nestedIncludes<A extends readonly unknown[], const M extends number = Infinity>(this: A, searchElement: ElementType<A, M>, fromIndices?: Indices<A, M>, maxDepth?: M): boolean; /** * Determines whether a nested array includes a specified element, searching backwards. * ```js * [[0, 1, 2], [3, 4, 5]].nestedIncludesFromLast(2); // true * [[0, 1, 2], [3, 4, 5]].nestedIncludesFromLast(2, [1, 1]); // false * ``` * If the element is more likely to appear near the start of the array, use the `nestedIncludes` method instead for a better performance. * @param this The original array. * @param searchElement The element to search for. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns `true` if the element is found in the array, or `false` otherwise. */ nestedIncludesFromLast<A extends readonly unknown[], const M extends number = Infinity>(this: A, searchElement: ElementType<A, M>, fromIndices?: Indices<A, M>, maxDepth?: M): boolean; /** * Returns the coordinates of the first occurrence of a specified value in an array, or `undefined` if it is not present. * ```js * [[0, 1, 2], [3, 4, 5]].nestedIndexOf(3); // [1, 0] * [[0, 1, 2], [3, 4, 5]].nestedIndexOf(3, [0, 1]); // undefined * ``` * If the element is more likely to appear near the end of the array, use the `nestedLastIndexOf` method instead for a better performance. * @param this The original array. * @param searchElement The element to search for. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A number array containing the coordinates of the element, or `undefined` if it is not present. */ nestedIndexOf<A extends readonly unknown[], const M extends number = Infinity>(this: A, searchElement: ElementType<A, M>, fromIndices?: Indices<A, M>, maxDepth?: M): Indices<A, M> | undefined; /** * Returns the coordinates of the last occurrence of a specified value in an array, or `undefined` if it is not present. * ```js * [[0, 1, 2], [3, 4, 5]].nestedLastIndexOf(2); // [0, 2] * [[0, 1, 2], [3, 4, 5]].nestedLastIndexOf(2, [1, 1]); // undefined * ``` * If the element is more likely to appear near the start of the array, use the `nestedIndexOf` method instead for a better performance. * @param this The original array. * @param searchElement The element to search for. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @returns A number array containing the coordinates of the element, or `undefined` if it is not present. */ nestedLastIndexOf<A extends readonly unknown[], const M extends number = Infinity>(this: A, searchElement: ElementType<A, M>, fromIndices?: Indices<A, M>, maxDepth?: M): Indices<A, M> | undefined; /** * Returns the value of the first element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFind(n => n % 6 == 3); // 3 * [[0, 1, 2], [3, 4, 5]].nestedFind(n => n % 6 == 3, [0, 1]); // undefined * ``` * If the element is more likely to appear near the end of the array, use the `nestedFindLast` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFind` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The value of the first element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFind<A extends readonly unknown[], S extends ElementType<A, M>, const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => value is S, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): S | undefined; /** * Returns the value of the first element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFind(n => n % 6 == 3); // 3 * [[0, 1, 2], [3, 4, 5]].nestedFind(n => n % 6 == 3, [0, 1]); // undefined * ``` * If the element is more likely to appear near the end of the array, use the `nestedFindLast` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFind` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The value of the first element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFind<A extends readonly unknown[], const M extends number = Infinity, const This = undefined>(this: A, predicate: (this: This, value: ElementType<A, M>, indices: Indices<A, M>, array: A, parent: Parent<A, M>) => unknown, fromIndices?: Indices<A, M>, maxDepth?: M, thisArg?: This): ElementType<A, M> | undefined; /** * Returns the value of the last element in a nested array that satisfies the `predicate` function, or `undefined` if there is no such element. * ```js * [[0, 1, 2], [3, 4, 5]].nestedFindLast(n => n % 6 == 2); // 2 * [[0, 1, 2], [3, 4, 5]].nestedFindLast(n => n % 6 == 2, [1, 1]); // undefined * ``` * If the element is more likely to appear near the start of the array, use the `nestedFind` method instead for a better performance. * @param this The original array. * @param predicate A function that accepts up to 4 arguments. * The `nestedFindLast` method calls the `predicate` function one time for each element in the array until it returns a truthy value. * @param fromIndices The coordinates at which to begin searching for (inclusive). * If a certain index is negative, the length of that axis of the array will be added to it. * @param maxDepth The deepest axis the method will traverse. Defaults to `Infinity`. * @param thisArg An object to which the `this` keyword can refer in the `predicate` function. * If `thisArg` is omitted, `undefined` is used as the `this` value. * @returns The value of the last element in the array that satisfies the `predicate` function, or `undefined` if there is no such element. */ nestedFindLast<A ex