import { type IterableOrValue } from '../iterable/iterable'; import { type Maybe } from '../value/maybe.type'; /** * An array type that cannot contain any elements. */ export type EmptyArray = never[]; /** * A value that is either a single item of type T or an array of T items. */ export type ArrayOrValue = T | T[]; /** * A value that is either a single item of type T or a readonly array of T items. * * Every {@link ArrayOrValue} is assignable to it, so helpers that only read their input accept this wider type. * {@link ArrayOrValue} itself stays `T | T[]`: `Array.isArray` does not narrow `readonly T[]` out of the false branch, * so `Array.isArray(x) ? x : [x]` call sites would break, and {@link convertToArray} returns its input array by reference. */ export type ReadonlyArrayOrValue = T | ReadonlyArray; /** * Converts the input value to an array containing itself, or returns itself if it is a non-empty array. Returns undefined if the input is nullish or results in an empty array. * * @param arrayOrValue - Single value or array to convert. * @returns An array with at least one element, or undefined if the result would be empty. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, non-empty, convert, ensure, normalize * @dbxUtilRelated convert-maybe-to-array, convert-to-array */ export declare function convertMaybeToNonEmptyArray(arrayOrValue: Maybe>): Maybe; /** * Converts the input value to an array containing itself, or returns itself if it is an array. Returns an empty array if the input is nullish. * * @param arrayOrValue - Single value, array, or nullish value to convert. * @returns The input wrapped in an array, the input array itself, or an empty array if nullish. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, convert, ensure, normalize, maybe, nullish * @dbxUtilRelated convert-maybe-to-non-empty-array, convert-to-array, as-array */ export declare function convertMaybeToArray(arrayOrValue: Maybe>): T[]; /** * Alias for {@link convertMaybeToArray}. Converts a maybe value or array into an array, returning an empty array for nullish input. */ export declare const asArray: typeof convertMaybeToArray; /** * Alias for {@link convertMaybeToNonEmptyArray}. Converts a maybe value or array into a non-empty array, returning undefined for nullish or empty input. */ export declare const asNonEmptyArray: typeof convertMaybeToNonEmptyArray; /** * Converts the input value to an array containing itself, or returns itself if it is already an array. * * @param arrayOrValue - Single value or array to convert. * @returns The input array unchanged, or a new single-element array wrapping the input value. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, convert, ensure, wrap, normalize * @dbxUtilRelated convert-maybe-to-array, convert-maybe-to-non-empty-array */ export declare function convertToArray(arrayOrValue: ArrayOrValue): T[]; /** * Returns the first value from the array, or the value itself if not an array. * * @param input - Single value or array to retrieve from. * @returns The first element of the array, or the input value itself. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, first, head, get, value * @dbxUtilRelated last-value, value-at-index, first-and-last-value */ export declare function firstValue(input: ArrayOrValue): T; /** * Returns the last value from the array, or the value itself if not an array. * * @param input - Single value or array to retrieve from. * @returns The last element of the array, or the input value itself. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, last, tail, get, value * @dbxUtilRelated first-value, value-at-index, first-and-last-value */ export declare function lastValue(input: ArrayOrValue): T; /** * Returns a tuple with the first and last value of the input. * * If the input is not an array, returns that value as both the first and last value. * * @param input - Single value or array to retrieve from. * @returns A two-element tuple of the first and last values. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, first, last, head, tail, tuple, endpoints * @dbxUtilRelated first-value, last-value, value-at-index */ export declare function firstAndLastValue(input: ArrayOrValue): [T, T]; /** * Returns the value at the given index from an array, or the value itself if not an array. * * @param input - Single value or array to retrieve from. * @param index - Zero-based index of the element to retrieve. * @returns The element at the specified index, or the input value itself if not an array. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, index, get, value, at * @dbxUtilRelated first-value, last-value, first-and-last-value */ export declare function valueAtIndex(input: ArrayOrValue, index: number): T; /** * Concatenates the input arrays into a single array, filtering out nullish entries. * * @param arrays - Arrays to concatenate; nullish entries are ignored. * @returns A single flattened array containing all elements from the non-nullish input arrays. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, concat, concatenate, combine, flatten, merge * @dbxUtilRelated flatten-array, merge-arrays */ export declare function concatArrays(...arrays: Maybe>[]): T[]; /** * Flattens a two-dimensional array into a single-dimensional array. Any null/undefined entries in the outer dimension are filtered out. * * @param array - Two-dimensional array to flatten, may contain nullish entries. * @returns A single-dimensional array with all elements from the non-nullish inner arrays. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, flatten, flat, nested, two-dimensional, concat * @dbxUtilRelated concat-arrays, flatten-array-or-value-array, merge-arrays */ export declare function flattenArray(array: Maybe[]): T[]; /** * Flattens an array of {@link ArrayOrValue} entries into a single array. Nullish entries are filtered out. * * @param array - Array of single values or arrays to flatten. * @returns A single flat array containing all non-nullish elements. */ export declare function flattenArrayOrValueArray(array: ArrayOrValue>[]): T[]; /** * Creates a shallow copy of the input array. Returns an empty array if the input is nullish. * * @param input - Array to copy, or nullish. * @returns A new array with the same elements, or an empty array if input is nullish. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, copy, clone, shallow, duplicate * @dbxUtilRelated convert-maybe-to-array */ export declare function copyArray(input: Maybe): T[]; /** * Pushes the same element onto the target array a specified number of times. * * @param target - Array to push elements into. * @param element - Element to push. * @param times - Number of times to push the element. * @returns The mutated target array. */ export declare function pushElementOntoArray(target: T[], element: T, times: number): T[]; /** * Merges all input arrays into a single new array. Nullish entries are ignored. * * @param arrays - Arrays to merge; nullish entries are skipped. * @returns A new array containing all elements from the provided arrays. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, merge, concat, flatten, combine * @dbxUtilRelated merge-arrays-into-array */ export declare function mergeArrays(arrays: Maybe[]): T[]; /** * Merges the input arrays into the target array by pushing each item from each array. Creates an empty array if the target is nullish. * * @param target - Array to merge into; a new array is created if nullish. * @param arrays - Arrays whose elements are pushed into the target; nullish entries are skipped. * @returns The mutated target array, or a new array if the target was nullish. */ export declare function mergeArraysIntoArray(target: Maybe, ...arrays: Maybe[]): T[]; /** * Pushes the input value into the target array if it is not an array. If it is an array, pushes all of its elements into the target array. * * @param target - Array to push into. * @param value - Single value or array of values to add. * @returns The mutated target array. */ export declare function pushItemOrArrayItemsIntoArray(target: T[], value: ArrayOrValue): T[]; /** * Merges all elements from the source array into the target array using push. * * This is preferable in cases where immutability is not required. * * @param target - Array to push elements into. * @param array - Source array whose elements are pushed into the target. * @returns The mutated target array. */ export declare function pushArrayItemsIntoArray(target: T[], array: T[]): T[]; /** * Copies/takes the elements from the front of the array up to the specified maximum. * * @param values - Source array to take from. * @param maxToTake - Maximum number of elements to take from the front. * @returns A new array containing at most maxToTake elements from the front. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, take, front, head, slice, limit * @dbxUtilRelated take-last, split-front */ export declare function takeFront(values: T[], maxToTake: number): T[]; /** * splitFront() result */ export interface SplitFrontResult { /** * The input max to take value. */ readonly maxToTake: number; /** * The front of the array up to the maxToTake. */ readonly front: T[]; /** * The remaining values after the front. */ readonly remaining: T[]; } /** * Splits the array into two arrays, the first being the front of the array up to the maxToTake, and the second being the remaining values. * * @param values - Source items to partition into front + remaining halves. * @param maxToTake - Upper bound on how many entries to take from the start. * @returns Pair containing the leading slice plus everything left over. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, split, partition, front, slice * @dbxUtilRelated take-front, take-last */ export declare function splitFront(values: T[], maxToTake: number): SplitFrontResult; /** * Copies/takes as many elements as possible from the end. * * @param values - Values to take from. * @param maxToTake - Max number of values to take from the end of the input array. * @param keepFromFront - Number of values to retain in the front of the array. These are not taken. * @returns New array with the subset of taken values. * @throws {Error} When `maxToTake` is smaller than `keepFromFront`. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, take, last, tail, end, slice, limit * @dbxUtilRelated take-front, split-front */ export declare function takeLast(values: T[], maxToTake: number, keepFromFront?: number): T[]; /** * Performs a forEach iteration over the input and returns the resulting array. If the input is nullish, returns an empty array. * * @param array - Single value, array, or nullish value to iterate over. * @param forEach - Callback invoked for each element. * @returns Items iterated by the callback; empty when the input was nullish. */ export declare function forEachWithArray(array: Maybe>, forEach: (value: T) => void): T[]; /** * Counts the total number of elements across all inner arrays of a nested array. * * @param array - Two-dimensional array whose elements are counted. * @returns The total number of elements across all inner arrays. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, count, nested, total, length, two-dimensional * @dbxUtilRelated flatten-array */ export declare function countAllInNestedArray(array: T[][]): number; /** * Creates a copy of the array with the items at the specified indexes removed. * * @param array - Source array to copy from. * @param removeIndexes - Indexes of elements to exclude from the copy. * @returns A new array without the elements at the specified indexes. * * @dbxUtil * @dbxUtilCategory array * @dbxUtilTags array, remove, exclude, indexes, filter, copy */ export declare function removeValuesAtIndexesFromArrayCopy(array: T[], removeIndexes: IterableOrValue): T[];