array
validation
isArray
function isArray<T>(
value: CheckT<T>,
): value is IsUnknown<T> | IsAny<T> extends false ? Extract<T, ROList> : Extract<List, T>;Checks whether a value is an array.
function foo(value: string | string[]) {
if (isArray(value)) {
value; // string[]
}
}isNotArray
function isNotArray<T>(value: CheckT<T>): value is Exclude<T, ROList>;Checks whether a value is not an array.
function foo(value: string | string[]) {
if (isNotArray(value)) {
value; // string
}
}isEmptyArray
function isEmptyArray<T>(value: CheckT<T>): value is Extract<[], T>;Checks whether a value is an empty array.
function foo(value: string[]) {
if (isEmptyArray(value)) {
value; // []
}
}isNonEmptyArray
function isNonEmptyArray<T>(
value: CheckT<T>,
): value is IsUnknown<T> | IsAny<T> extends false
? T extends T
? IsTuple<T> extends true
? Exclude<T, []>
: T extends ROList
? Extract<MatchRO<NonEmptyTuple<T[number]>, T>, T>
: never
: never
: Extract<NonEmptyTuple<T>, T>;Checks whether a value is a non-empty array.
function foo(value: string[]) {
if (isNonEmptyArray(value)) {
value; // [string, ...string[]]
}
}isSingletonArray
function isSingletonArray<T>(
value: CheckT<T>,
): value is IsUnknown<T> | IsAny<T> extends false
? T extends ROList
? number extends T["length"]
? Extract<MatchRO<[T[number]], T>, T>
: Extract<T, readonly [unknown]>
: never
: Extract<[T], T>;Checks whether a value is an array with a single element.
function foo(value: string[]) {
if (isSingletonArray(value)) {
value; // [string]
}
}isNonSingletonArray
function isNonSingletonArray<T>(
value: CheckT<T>,
): value is IsUnknown<T> | IsAny<T> extends false
? Exclude<Extract<T, ROList>, [unknown]>
: Extract<List, T>;Checks whether a value is an array with no or more than one element.
isNonSingletonArray([]); // true
isNonSingletonArray([1]); // false
isNonSingletonArray([1, 2]); // trueisFirstIndex
function isFirstIndex(index: number): boolean;A readable way to check if index === 0.
isNotFirstIndex
function isNotFirstIndex(index: number): boolean;A readable way to check if index !== 0.
isLastIndex
function isLastIndex(index: number, length: number): boolean;Checks whether a given index is the last index of an array.
const arr = [0, 1, 2, 3];
isLastIndex(0, arr.length); // false;
isLastIndex(3, arr.length); // true;isNotLastIndex
function isNotLastIndex(index: number, length: number): boolean;Checks whether a given index is not the last index of an array.
const arr = [0, 1, 2, 3];
isNotLastIndex(0, arr.length); // true;
isNotLastIndex(3, arr.length); // false;hasIndex
function hasIndex(index: number, length: number): boolean;Checks whether a given index is within the range of an array.
const arr = [0, 1, 2, 3];
hasIndex(-1, arr.length); // false;
hasIndex(0, arr.length); // true;
hasIndex(3, arr.length); // true;
hasIndex(4, arr.length); // false;lacksIndex
function lacksIndex(index: number, length: number): boolean;Checks whether a given index is not within the range of an array.
const arr = [0, 1, 2, 3];
lacksIndex(-1, arr.length); // true;
lacksIndex(0, arr.length); // false;
lacksIndex(3, arr.length); // false;
lacksIndex(4, arr.length); // true;utils
indexToNumber
function indexToNumber(index: number): number;Converts a 0-based index to a 1-based index.
indexToNumber(0); // 1numberToIndex
function numberToIndex(number: number): number;Converts a 1-based index to a 0-based index.
numberToIndex(1); // 0getFirstElement
function getFirstElement<const T>(arr: [T, ...unknown[]]): T;Returns the first element of an array.
getLastIndex
function getLastIndex(length: number): number;Returns the last index of an array using its length.
const arr = [1, 2, 3, 4, 5];
getLastIndex(arr.length); // 4getLastElement
function getLastElement<const T>(arr: [...unknown[], T]): T;Returns the last element of an array.
getMiddleIndex
function getMiddleIndex(length: number): number;Returns the middle index of an array using its length.
const oddArr = [0, 1, 2];
const evenArr = [0, 1, 2, 3];
getMiddleIndex(oddArr.length); // 1
getMiddleIndex(evenArr.length); // 1getMiddleElement
function getMiddleElement<const T>(arr: ROList<T>): T;Returns the middle element of an array.
getIndexRange
function getIndexRange(length: number): Range;Returns the index range of an array using its length.
const arr = [0, 1, 2, 3, 4];
getIndexRange(arr.length); // [0, 4]getSignedIndexRange
function getSignedIndexRange(length: number): Range;Returns the signed index range of an array using its length.
const arr = [0, 1, 2, 3, 4];
getSignedIndexRange(arr.length); // [-5, 4]createArray
function createArray<L extends number, const R>(
length: L,
getValue: Func<[index: number], R>,
): Tuple<L, R>;Creates an array of the given length and uses the given function to generate its values.
createArray(5, (index) => index); // [0, 1, 2, 3, 4]getZeroBasedIndices
function getZeroBasedIndices(length: number): number[];Returns an array of the given length with zero-based indices.
cons arr = [img1, img2, img3];
const indexes = getZeroBasedIndices(arr.length); // [0, 1, 2]getOneBasedIndices
function getOneBasedIndices(length: number): number[];Returns an array of the given length with one-based indices.
cons arr = [img1, img2, img3];
const indexes = getOneBasedIndices(arr.length); // [1, 2, 3]getRandomIndex
function getRandomIndex(length: number): number;Returns a random index from an array using its length.
getRandomElement
function getRandomElement<T extends ROList>(
arr: T,
): T extends T ? T[number] | (IsTuple<T> extends true ? never : undefined) : never;Returns a random element from an array.
mapOnDefined
function mapOnDefined<T extends ROList, const R>(
arr: T,
func: Func<[value: Exclude<T[number], undefined | null>], R>,
): (R | Extract<T[number], undefined | null>)[];Maps a function over an array, ignoring undefined and null values.
const arr = [1, null, 3];
mapOnDefined(arr, (value) => value * 2); // [2, null, 6]toUniqueArray
function toUniqueArray<T extends ROList>(arr: T): T[number][];Returns an array with unique elements.
const arr = ["foo", "foo", "bar"];
const uniqueArr = toUniqueArray(arr); // ["foo", "bar"]