Array
Helper functions that have to do with arrays.
Functions
arrayCopyTwoDimensional()
function arrayCopyTwoDimensional<T>(array): T[][]
Defined in: functions/array.ts:17
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
Parameter | Type |
---|---|
array | readonly readonly T [][] |
Returns
T
[][]
arrayEquals()
function arrayEquals<T>(array1, array2): boolean
Defined in: functions/array.ts:33
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
Parameter | Type |
---|---|
array1 | readonly T [] |
array2 | readonly T [] |
Returns
boolean
arrayRemove()
function arrayRemove<T>(originalArray, ...elementsToRemove): 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.
This function is variadic, meaning that you can specify N arguments to remove N elements.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type |
---|---|
originalArray | readonly T [] |
...elementsToRemove | readonly T [] |
Returns
T
[]
arrayRemoveInPlace()
function arrayRemoveInPlace<T>(array, ...elementsToRemove): T[]
Defined in: functions/array.ts:84
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
Parameter | Type |
---|---|
array | T [] |
...elementsToRemove | readonly T [] |
Returns
T
[]
The removed elements. This will be an empty array if no elements were removed.
emptyArray()
function emptyArray<T>(array): void
Defined in: functions/array.ts:104
Helper function to remove all of the elements in an array in-place.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type |
---|---|
array | T [] |
Returns
void
filterMap()
function filterMap<OldT, NewT>(array, func): NewT[]
Defined in: functions/array.ts:120
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
Parameter | Type |
---|---|
array | readonly OldT [] |
func | (element ) => undefined | NewT |
Returns
NewT
[]
getRandomArrayElement()
function getRandomArrayElement<T>(array, exceptions): T
Defined in: functions/array.ts:145
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
Parameter | Type | Default value | Description |
---|---|---|---|
array | readonly T [] | undefined | The array to get an element from. |
exceptions | readonly T [] | [] | Optional. An array of elements to skip over if selected. |
Returns
T
getRandomArrayIndex()
function getRandomArrayIndex<T>(array, exceptions): number
Defined in: functions/array.ts:174
Helper function to get a random index from the provided array.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
array | readonly T [] | undefined | The array to get the index from. |
exceptions | readonly 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:193
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
Parameter | Type |
---|---|
array | readonly TupleElement [] |
searchElement | WidenLiteral <T > |
Returns
searchElement is TupleElement
isArray()
function isArray(arg): arg is unknown[]
Defined in: functions/array.ts:202
A wrapper around Array.isArray
that narrows to unknown[]
instead of any[]
.
Parameters
Parameter | Type |
---|---|
arg | unknown |
Returns
arg is unknown[]
newArray()
function newArray<T>(length, value): T[]
Defined in: functions/array.ts:208
Initializes an array with all elements containing the specified default value.
Type Parameters
Type Parameter |
---|
T |
Parameters
Parameter | Type |
---|---|
length | number |
value | T |
Returns
T
[]
sumArray()
function sumArray(array): number
Defined in: functions/array.ts:213
Helper function to sum every value in an array together.
Parameters
Parameter | Type |
---|---|
array | readonly number [] |
Returns
number