Skip to main content

Array

Helper functions that have to do with arrays.

Functions

arrayCopyTwoDimensional()

function arrayCopyTwoDimensional<T>(array): readonly readonly T[][];

Defined in: functions/array.ts:16

Helper function to copy a two-dimensional array. Note that the sub-arrays will only be shallow copied (using the spread operator).

Type Parameters

Type Parameter
T

Parameters

ParameterType
arrayreadonly readonly T[][]

Returns

readonly readonly T[][]


arrayEquals()

function arrayEquals<T>(array1, array2): boolean;

Defined in: functions/array.ts:32

Helper function for determining if two arrays contain the exact same elements. Note that this only performs a shallow comparison.

Type Parameters

Type Parameter
T

Parameters

ParameterType
array1readonly T[]
array2readonly T[]

Returns

boolean


arrayRemove()

function arrayRemove<T>(originalArray, ...elementsToRemove): readonly T[];

Defined in: functions/array.ts:55

Builds a new array based on the original array without the specified element(s). Returns the new array. If the specified element(s) are not found in the array, it will simply return a shallow copy of the array.

If there is more than one matching element in the array, this function will remove all of them.

This function is variadic, meaning that you can specify N arguments to remove N elements.

Type Parameters

Type Parameter
T

Parameters

ParameterType
originalArrayreadonly T[]
...elementsToRemovereadonly T[]

Returns

readonly T[]


arrayRemoveAllInPlace()

function arrayRemoveAllInPlace<T>(array, ...elementsToRemove): boolean;

Defined in: functions/array.ts:83

Removes all of the specified element(s) from the array. If the specified element(s) are not found in the array, this function will do nothing.

This function is variadic, meaning that you can specify N arguments to remove N elements.

If there is more than one matching element in the array, this function will remove every matching element. If you want to only remove the first matching element, use the arrayRemoveInPlace function instead.

Type Parameters

Type Parameter
T

Parameters

ParameterType
arrayT[]
...elementsToRemovereadonly T[]

Returns

boolean

True if one or more elements were removed, false otherwise.


arrayRemoveInPlace()

function arrayRemoveInPlace<T>(array, ...elementsToRemove): readonly T[];

Defined in: functions/array.ts:115

Removes the specified element(s) from the array. If the specified element(s) are not found in the array, this function will do nothing.

This function is variadic, meaning that you can specify N arguments to remove N elements.

If there is more than one matching element in the array, this function will only remove the first one. If you want to remove all of the elements, use the arrayRemoveAllInPlace function instead.

Type Parameters

Type Parameter
T

Parameters

ParameterType
arrayT[]
...elementsToRemovereadonly T[]

Returns

readonly T[]

The removed elements. This will be an empty array if no elements were removed.


emptyArray()

function emptyArray(array): void;

Defined in: functions/array.ts:135

Helper function to remove all of the elements in an array in-place.

Parameters

ParameterType
arrayunknown[]

Returns

void


filterAsync()

function filterAsync<T>(array, predicate): Promise<readonly T[]>;

Defined in: functions/array.ts:152

Helper function to perform an asynchronous filter. The vanilla Array.filter method does not wait for promises (and treats them as truthy), so this function runs the predicate on all elements concurrently, awaits the results, and then filters the original array.

Usage:

const results = await filterAsync(things, async (thing) => await filterFunc(thing));

(This is an abstraction around Promise.all.)

Type Parameters

Type Parameter
T

Parameters

ParameterType
arrayreadonly T[]
predicate(element, index, array) => Promise<boolean>

Returns

Promise<readonly T[]>


filterMap()

function filterMap<OldT, NewT>(array, func): readonly NewT[];

Defined in: functions/array.ts:178

Helper function to perform a filter and a map at the same time. Similar to Array.map, provide a function that transforms a value, but return undefined if the value should be skipped. (Thus, this function cannot be used in situations where undefined can be a valid array element.)

This function is useful because the Array.map method will always produce an array with the same amount of elements as the original array.

This is named filterMap after the Rust function: https://doc.rust-lang.org/std/iter/struct.FilterMap.html

Type Parameters

Type Parameter
OldT
NewT

Parameters

ParameterType
arrayreadonly OldT[]
func(element) => NewT | undefined

Returns

readonly NewT[]


filterMapAsync()

function filterMapAsync<OldT, NewT>(array, func): Promise<readonly NewT[]>;

Defined in: functions/array.ts:213

Helper function to perform a filter and a map at the same time. Similar to Array.map, provide a function that transforms a value, but return undefined if the value should be skipped. (Thus, this function cannot be used in situations where undefined can be a valid array element.)

This function is useful because the Array.map method will always produce an array with the same amount of elements as the original array.

This is named filterMap after the Rust function: https://doc.rust-lang.org/std/iter/struct.FilterMap.html

This is the asynchronous version, which can be used like this:

const results = await asyncFilterMap(things, someFunc);

(This is an abstraction around Promise.all.)

Type Parameters

Type Parameter
OldT
NewT

Parameters

ParameterType
arrayreadonly OldT[]
func(element, index, array) => Promise<NewT | undefined>

Returns

Promise<readonly NewT[]>


getRandomArrayElement()

function getRandomArrayElement<T>(array, exceptions): T;

Defined in: functions/array.ts:237

Helper function to get a random element from the provided array.

Note that this will only work with arrays that do not contain values of undefined, since the function uses undefined as an indication that the corresponding element does not exist.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDefault valueDescription
arrayreadonly T[]undefinedThe array to get an element from.
exceptionsreadonly T[][]Optional. An array of elements to skip over if selected.

Returns

T


getRandomArrayIndex()

function getRandomArrayIndex(array, exceptions): number;

Defined in: functions/array.ts:266

Helper function to get a random index from the provided array.

Parameters

ParameterTypeDefault valueDescription
arrayreadonly unknown[]undefinedThe array to get the index from.
exceptionsreadonly number[][]Optional. An array of indexes that will be skipped over when getting the random index. Default is an empty array.

Returns

number


includes()

function includes<T, TupleElement>(array, searchElement): searchElement is TupleElement;

Defined in: functions/array.ts:285

Similar to the Array.includes method, but works on a widened version of the array.

This is useful when the normal Array.includes produces a type error from an array that uses an as const assertion.

Type Parameters

Type Parameter
T
TupleElement

Parameters

ParameterType
arrayreadonly TupleElement[]
searchElementWidenLiteral<T>

Returns

searchElement is TupleElement


includesAny()

function includesAny<T>(array, ...searchElements): boolean;

Defined in: functions/array.ts:298

Similar to the Array.includes method, but accepts a variadic amount of search elements.

Type Parameters

Type Parameter
T

Parameters

ParameterType
arrayreadonly T[]
...searchElementsreadonly T[]

Returns

boolean

True if any of the elements are found, false otherwise.


isArray()

function isArray(variable): variable is unknown[];

Defined in: functions/array.ts:306

A wrapper around Array.isArray that narrows to unknown[] instead of any[].

Parameters

ParameterType
variableunknown

Returns

variable is unknown[]


isArrayBoolean()

function isArrayBoolean(variable): variable is boolean[];

Defined in: functions/array.ts:311

Helper function to check every value of an array to see if it is a boolean.

Parameters

ParameterType
variableunknown

Returns

variable is boolean[]


isArrayNumber()

function isArrayNumber(variable): variable is number[];

Defined in: functions/array.ts:320

Helper function to check every value of an array to see if it is a number.

Parameters

ParameterType
variableunknown

Returns

variable is number[]


isArrayString()

function isArrayString(variable): variable is string[];

Defined in: functions/array.ts:329

Helper function to check every value of an array to see if it is a string.

Parameters

ParameterType
variableunknown

Returns

variable is string[]


mapAsync()

function mapAsync<T, U>(array, callback): Promise<readonly U[]>;

Defined in: functions/array.ts:353

Helper function to perform an asynchronous map. The vanilla Array.map method does not wait for promises, resulting in an array of promises rather than the resolved values. This function runs the callback on all elements concurrently, awaits the results, and returns the mapped array.

You can also use this function to simply run an asynchronous function on each element of an array concurrently.

Usage:

const results = await mapAsync(things, async (thing) => await mapFunc(thing));

(This is an abstraction around Promise.all.)

Type Parameters

Type Parameter
T
U

Parameters

ParameterType
arrayreadonly T[]
callback(element, index, array) => Promise<U>

Returns

Promise<readonly U[]>


newArray()

function newArray<T>(length, value): readonly T[];

Defined in: functions/array.ts:364

Initializes an array with all elements containing the specified default value.

Type Parameters

Type Parameter
T

Parameters

ParameterType
lengthnumber
valueT

Returns

readonly T[]


sumArray()

function sumArray(array): number;

Defined in: functions/array.ts:369

Helper function to sum every value in an array together.

Parameters

ParameterType
arrayreadonly number[]

Returns

number