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
Parameter | Type |
---|---|
array | readonly readonly T [][] |
Returns
T
[][]
Defined in
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
Parameter | Type |
---|---|
array1 | readonly T [] |
array2 | readonly T [] |
Returns
boolean
Defined in
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
Parameter | Type |
---|---|
originalArray | readonly T [] |
...elementsToRemove | readonly T [] |
Returns
T
[]
Defined in
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
Parameter | Type |
---|---|
array | T [] |
...elementsToRemove | readonly T [] |
Returns
T
[]
The removed elements. This will be an empty array if no elements were removed.
Defined in
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
Parameter | Type |
---|---|
array | T [] |
Returns
void
Defined in
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
Parameter | Type |
---|---|
array | readonly OldT [] |
func | (element ) => undefined | NewT |
Returns
NewT
[]
Defined in
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
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
Defined in
getRandomArrayIndex()
function getRandomArrayIndex<T>(array, exceptions): number
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
Defined in
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
Parameter | Type |
---|---|
array | readonly TupleElement [] |
searchElement | WidenLiteral <T > |
Returns
searchElement is TupleElement
Defined in
isArray()
function isArray(arg): arg is unknown[]
A wrapper around Array.isArray
that narrows to unknown[]
instead of any[]
.
Parameters
Parameter | Type |
---|---|
arg | unknown |
Returns
arg is unknown[]
Defined in
newArray()
function newArray<T>(length, value): T[]
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
[]
Defined in
sumArray()
function sumArray(array): number
Helper function to sum every value in an array together.
Parameters
Parameter | Type |
---|---|
array | readonly number [] |
Returns
number