Skip to main content

Class: ArrayUtils

Utility functions for working with arrays.

Constructors

constructor

new ArrayUtils(): ArrayUtils

Returns

ArrayUtils

Methods

at

at<T>(array, index): T

Gets the element at a specific index in an array.

Type parameters

Name
T

Parameters

NameTypeDescription
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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:87


binarySearch

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

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

Name
T

Parameters

NameTypeDefault valueDescription
arrayreadonly T[]undefinedAn array.
elementTundefinedThe element to search for.
comparator(a: T, b: T) => 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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:315


create

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

Creates a new array with initialized values.

Type parameters

Name
T

Parameters

NameTypeDescription
lengthnumberThe length of the new array.
init(index: number) => 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.

Defined in

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


equals

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

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

Name
T1
T2

Parameters

NameTypeDefault valueDescription
areadonly T1[]undefinedThe first array.
breadonly T2[]undefinedThe second array.
equalsFunc(a: T1, b: T2) => 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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:180


fillRange

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

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

NameTypeDefault 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.

Defined in

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


first

first<T>(array): T

Gets the first element of an array.

Type parameters

Name
T

Parameters

NameTypeDescription
arrayreadonly T[]An array.

Returns

T

The first element of the specified array.

Throws

RangeError if the array is empty.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:120


flat

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

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

NameType
Aextends readonly any[]

Parameters

NameTypeDescription
arrayAAn array.

Returns

FlattenArray<A>

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

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:226

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

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

NameType
Aextends readonly any[]
Depthextends undefined | 0 | 2 | 4 | 1 | 3 | 5 | 6 | 7 | 8 | 9 | 10

Parameters

NameTypeDescription
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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:236

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

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

NameType
Tunknown

Parameters

NameTypeDescription
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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:249


flatMap

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

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

NameType
OO
Aextends readonly any[]

Parameters

NameTypeDescription
arrayAAn array.
map(value: ArrayType<A>, index: number, array: A) => 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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:202


getMaxStringLength

getMaxStringLength(array): number

Gets the length of the longest string in the array.

Parameters

NameTypeDescription
arraystring[]The array to search in.

Returns

number

length of the longest string

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:344


includes

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

Checks if a certain element is included in an array.

Type parameters

Name
T

Parameters

NameTypeDescription
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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:167


last

last<T>(array): T

Gets the last element of an array.

Type parameters

Name
T

Parameters

NameTypeDescription
arrayreadonly T[]An array.

Returns

T

The last element of the specified array.

Throws

RangeError if the array is empty.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:143


peekAt

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

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

Type parameters

Name
T

Parameters

NameTypeDescription
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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:106


peekFirst

peekFirst<T>(array): undefined | T

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

Type parameters

Name
T

Parameters

NameTypeDescription
arrayreadonly T[]An array.

Returns

undefined | T

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

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:133


peekLast

peekLast<T>(array): undefined | T

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

Type parameters

Name
T

Parameters

NameTypeDescription
arrayreadonly T[]An array.

Returns

undefined | T

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

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:156


range

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

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

Parameters

NameTypeDefault 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.

Defined in

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


shallowCopy

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

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

Name
T

Parameters

NameTypeDefault 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.

Defined in

src/sdk/utils/datastructures/ArrayUtils.ts:290