Skip to main content

Class: ArrayUtils

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:31

Utility functions for working with arrays.

Constructors

Constructor

new ArrayUtils(): ArrayUtils

Returns

ArrayUtils

Methods

at()

static at<T>(array, index): T

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:86

Gets the element at a specific index in an array.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
arrayreadonly T[]An array.
indexnumberThe index to access. Negative indexes are supported and access elements starting from the end of the array (-1 accesses the last element, -2 the second to last element, etc).

Returns

T

The element at the specified index in the array.

Throws

RangeError if the index is out of bounds.


binarySearch()

static binarySearch<T>(array, element, comparator, first): number

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:314

Performs a binary search on a sorted array to find the index of the first or last element in the array whose sorting order is equal to a query element. If no such element in the array exists, -(index + 1) is returned, where index is the index at which the query element would be found if it were contained in the sorted array.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDefault valueDescription
arrayreadonly T[]undefinedAn array.
elementTundefinedThe element to search for.
comparator(a, b) => numberundefinedA function which determines the sorting order of elements in the array. The function should return a negative number if the first element is to be sorted before the second, a positive number if the first element is to be sorted after the second, or zero if both elements are to be sorted equivalently.
firstbooleantrueIf true, this method will find the first (lowest) matching index if there are multiple matching indexes, otherwise this method will find the last (highest) matching index. Defaults to true.

Returns

number

The index of the first (if first is true) or last (if first is false) element in the specified array whose sorting order is equal to the query element, or -(index + 1), where index is the index at which the query element would be found if it were contained in the sorted array, if no element in the array has a sorting order equal to the query.


create()

static create<T>(length, init): T[]

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:40

Creates a new array with initialized values.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
lengthnumberThe length of the new array.
init(index) => TA function which generates initial values for the new array at each index.

Returns

T[]

A new array of the specified length with initialized values.


equals()

static equals<T1, T2>(a, b, equalsFunc): boolean

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:179

Checks if two arrays are equal to each other. This method considers two arrays a and b if their lengths are equal and a[i] equals b[i] for every valid index i. All empty arrays are considered equal to one another.

Type Parameters

Type Parameter
T1
T2

Parameters

ParameterTypeDefault valueDescription
areadonly T1[]undefinedThe first array.
breadonly T2[]undefinedThe second array.
equalsFunc(a, b) => booleanArrayUtils.STRICT_EQUALSThe function to use to determine whether two array elements are equal to each other. Defaults to a function which uses the strict equality operator (===).

Returns

boolean

Whether the two specified arrays are equal.


fillRange()

static fillRange(array, length, startIndex, start, increment): number[]

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:71

Fills an existing array with a sequence of evenly-spaced numbers. The sequence is written to the array in a single contiguous block of consecutive indexes.

Parameters

ParameterTypeDefault valueDescription
arraynumber[]undefinedThe array to fill.
lengthnumberundefinedThe length of the number sequence.
startIndexnumber0The index at which to start filling the array. Defaults to 0.
startnumberstartIndexThe first number in the sequence. Defaults to startIndex.
incrementnumber1The increment between each successive number in the new array. Defaults to 1.

Returns

number[]

The array, after it has been filled with the specified number sequence.


first()

static first<T>(array): T

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:119

Gets the first element of an array.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
arrayreadonly T[]An array.

Returns

T

The first element of the specified array.

Throws

RangeError if the array is empty.


flat()

Creates a new array by flattening an existing array to a maximum depth, leaving the original array intact. The process of flattening replaces each element in the array that is itself an array with the sequence of elements found in the sub-array, recursively up to the maximum depth.

Param

An array.

Param

The maximum depth to which to flatten. Values less than or equal to zero will result in no flattening (in other words, a shallow copy of the original array will be returned). Defaults to 1.

Call Signature

static flat<A>(array): FlattenArray<A>

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:225

Creates a new array by flattening an existing array to a maixmum depth of one, leaving the original array intact. The process of flattening replaces each element in the array that is itself an array with the sequence of elements found in the sub-array, recursively up to the maximum depth.

Type Parameters
Type Parameter
A extends readonly any[]
Parameters
ParameterTypeDescription
arrayAAn array.
Returns

FlattenArray<A>

A new array which was created by flattening the specified array to a maximum depth of one.

Call Signature

static flat<A, Depth>(array, depth): FlattenArrayToDepth<A, Depth extends undefined ? 1 : Depth>

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:235

Creates a new array by flattening an existing array to a maximum depth, leaving the original array intact. The process of flattening replaces each element in the array that is itself an array with the sequence of elements found in the sub-array, recursively up to the maximum depth.

Type Parameters
Type Parameter
A extends readonly any[]
Depth extends undefined | 0 | 2 | 4 | 1 | 3 | 5 | 6 | 7 | 8 | 9 | 10
Parameters
ParameterTypeDescription
arrayAAn array.
depthDepthThe maximum depth to which to flatten. Values less than or equal to zero will result in no flattening (in other words, a shallow copy of the original array will be returned). Defaults to 1.
Returns

FlattenArrayToDepth<A, Depth extends undefined ? 1 : Depth>

A new array which was created by flattening the specified array to the specified maximum depth.

Call Signature

static flat<T>(array, depth?): T[]

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:248

Creates a new array by flattening an existing array to a maximum depth, leaving the original array intact. The process of flattening replaces each element in the array that is itself an array with the sequence of elements found in the sub-array, recursively up to the maximum depth.

Type Parameters
Type ParameterDefault type
Tunknown
Parameters
ParameterTypeDescription
arrayreadonly unknown[]An array.
depth?numberThe maximum depth to which to flatten. Values less than or equal to zero will result in no flattening (in other words, a shallow copy of the original array will be returned). Defaults to 1.
Returns

T[]

A new array which was created by flattening the specified array to the specified maximum depth.


flatMap()

static flatMap<O, A>(array, map): O extends readonly T1[] ? T1 : O[]

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:201

Creates a new array by mapping each element of an existing array using a mapping function, then flattening the mapped elements to a maximum depth of one, leaving the original array intact.

Type Parameters

Type Parameter
O
A extends readonly any[]

Parameters

ParameterTypeDescription
arrayAAn array.
map(value, index, array) => OA function which is called once on each element of the original array to map it to an arbitrary value.

Returns

O extends readonly T1[] ? T1 : O[]

A new array which was created by mapping each element of the specified array, then flattening the mapped elements to a maximum depth of one.


getMaxStringLength()

static getMaxStringLength(array): number

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:343

Gets the length of the longest string in the array.

Parameters

ParameterTypeDescription
arraystring[]The array to search in.

Returns

number

length of the longest string


includes()

static includes<T>(array, searchElement, fromIndex?): searchElement is T

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:166

Checks if a certain element is included in an array.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
arrayreadonly T[]An array.
searchElementanyThe element to search for.
fromIndex?numberThe position in this array at which to begin searching for searchElement.

Returns

searchElement is T

Whether the search element is included in the specified array.


last()

static last<T>(array): T

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:142

Gets the last element of an array.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
arrayreadonly T[]An array.

Returns

T

The last element of the specified array.

Throws

RangeError if the array is empty.


peekAt()

static peekAt<T>(array, index): undefined | T

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:105

Gets the element at a specific index in an array, or undefined if the index is out of bounds.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
arrayreadonly T[]An array.
indexnumberThe index to access. Negative indexes are supported and access elements starting from the end of the array (-1 accesses the last element, -2 the second to last element, etc).

Returns

undefined | T

The element at the specified index in the array, or undefined if the index is out of bounds.


peekFirst()

static peekFirst<T>(array): undefined | T

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:132

Gets the first element of an array if it is not empty, or undefined otherwise.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
arrayreadonly T[]An array.

Returns

undefined | T

The first element of an array if it is not empty, or undefined otherwise.


peekLast()

static peekLast<T>(array): undefined | T

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:155

Gets the last element of an array if it is not empty, or undefined otherwise.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
arrayreadonly T[]An array.

Returns

undefined | T

The last element of an array if it is not empty, or undefined otherwise.


range()

static range(length, start, increment): number[]

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:57

Creates a new array containing a sequence of evenly-spaced numbers.

Parameters

ParameterTypeDefault valueDescription
lengthnumberundefinedThe length of the new array.
startnumber0The number contained at index 0 of the new array. Defaults to 0.
incrementnumber1The increment between each successive number in the new array. Defaults to 1.

Returns

number[]

A new array containing the specified sequence of evenly-spaced numbers.


shallowCopy()

static shallowCopy<T>(source, target): T[]

Defined in: src/sdk/utils/datastructures/ArrayUtils.ts:289

Performs a shallow copy of an array. After the operation is complete, the target array will have the same length and the same elements in the same order as the source array.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDefault valueDescription
sourcereadonly T[]undefinedThe array to copy.
targetT[][]The array to copy into. If not defined, a new array will be created.

Returns

T[]

The target array, after the source array has been copied into it.