Skip to main content

functions/array

Helper functions that have to do with arrays.

Functions

arrayCopyTwoDimensional()

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

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

T[][]

Defined in

functions/array.ts:17


arrayEquals()

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

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

Defined in

functions/array.ts:33


arrayRemove()

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

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.

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

T[]

Defined in

functions/array.ts:55


arrayRemoveInPlace()

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

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 matching element. 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

T[]

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

Defined in

functions/array.ts:84


emptyArray()

function emptyArray<T>(array): void

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

Type Parameters

Type Parameter
T

Parameters

ParameterType
arrayT[]

Returns

void

Defined in

functions/array.ts:104


filterMap()

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

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) => undefined | NewT

Returns

NewT[]

Defined in

functions/array.ts:120


getRandomArrayElement()

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

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

Defined in

functions/array.ts:145


getRandomArrayIndex()

function getRandomArrayIndex<T>(array, exceptions): number

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

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDefault valueDescription
arrayreadonly T[]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

Defined in

functions/array.ts:174


includes()

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

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

Defined in

functions/array.ts:193


isArray()

function isArray(arg): arg is unknown[]

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

Parameters

ParameterType
argunknown

Returns

arg is unknown[]

Defined in

functions/array.ts:202


newArray()

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

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

Type Parameters

Type Parameter
T

Parameters

ParameterType
lengthnumber
valueT

Returns

T[]

Defined in

functions/array.ts:208


sumArray()

function sumArray(array): number

Helper function to sum every value in an array together.

Parameters

ParameterType
arrayreadonly number[]

Returns

number

Defined in

functions/array.ts:213