@sv443-network/coreutils
Version:
Cross-platform, general-purpose, JavaScript core library for Node, Deno and the browser. Intended to be used in conjunction with `@sv443-network/userutils` and `@sv443-network/djsutils`, but can be used independently as well.
20 lines (19 loc) • 1.27 kB
TypeScript
/**
* @module array
* This module contains various functions for working with arrays - [see the documentation for more info](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#arrays)
*/
/** Describes an array with at least one item */
export type NonEmptyArray<TArray = unknown> = [TArray, ...TArray[]];
/** Returns a random item from the passed array */
export declare function randomItem<TItem = unknown>(array: TItem[]): TItem | undefined;
/**
* Returns a tuple of a random item and its index from the passed array
* Returns `[undefined, undefined]` if the passed array is empty
*/
export declare function randomItemIndex<TItem = unknown>(array: TItem[]): [item?: TItem, index?: number];
/** Returns a copy of the array with its items in a random order */
export declare function randomizeArray<TItem = unknown>(array: TItem[]): TItem[];
/** Returns a random item from the passed array and mutates the array to remove the item */
export declare function takeRandomItem<TItem = unknown>(arr: TItem[]): TItem | undefined;
/** Returns a random item and its index as a tuple from the passed array and mutates the array to remove the item */
export declare function takeRandomItemIndex<TItem = unknown>(arr: TItem[]): [item?: TItem, index?: number];