@feds01/graphika / utils/arrays
utils/arrays
Functions
fillRange()
function fillRange(size): number[];Defined in: utils/arrays.ts:20
fills an empty array from 0 to size with integers and then returns the new array.
Parameters
| Parameter | Type | Description |
|---|---|---|
size | number | Fill the array up to the given size. |
Returns
number[]
array with numbers 0 up to size.
findClosestIndex()
function findClosestIndex(arr, value): [number, number];Defined in: utils/arrays.ts:153
Find the index or indices or the closest elements to a given values in an array. For example:
const arr = [-5, -4, -3, -1, 1, 2, 3, 4, 5];
findClosest(arr, 0); // returns [3, 4]
findClosest(arr, 2); // returns [5, 5]
findClosest(arr, -6); // returns [0, 0]
findClosest(arr, 6); // returns [8, 8]
findClosest([], 6); // returns [-1, -1]Parameters
| Parameter | Type | Description |
|---|---|---|
arr | NumberArray | The array to search for the closest element. |
value | number | The value to find the closest element to. |
Returns
[number, number]
The indices of the closest element to the given value.
getMax()
function getMax(arr): number;Defined in: utils/arrays.ts:92
Function to get maximum element within array, we don't want to use Math.max if it is a large array.
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | number[] | Float64Array<ArrayBufferLike> | Source array. |
Returns
number
largest number in the array.
getMin()
function getMin(arr): number;Defined in: utils/arrays.ts:108
Function to get maximum element within array.
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | number[] | Source array. |
Returns
number
Smallest number in the array.
getMinMax()
function getMinMax(arr): {
max: number;
min: number;
};Defined in: utils/arrays.ts:124
Function to get maximum and minimum element within array.
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | NumberArray | Source array. |
Returns
{
max: number;
min: number;
}smallest and largest numbers within the array.
| Name | Type | Defined in |
|---|---|---|
max | number | utils/arrays.ts:124 |
min | number | utils/arrays.ts:124 |
getNext()
function getNext<T>(index, arr): T;Defined in: utils/arrays.ts:59
Get the next element of an array, this method is also a safety wrapper function, if the given index is equal to the length of the array - 1, or larger, return the last element of the array, rather than undefined.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
index | number | Current position in the array. |
arr | T[] | The array to index for the next item. |
Returns
T
the previous item from the array, the same one if the index is length of the array.
getPrevious()
function getPrevious<T>(index, data): T;Defined in: utils/arrays.ts:46
Get the previous element of an array, this method is created for safety, if the given index is zero or less than zero, the function will return the element at zero rather than undefined.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
index | number | Current position in the array. |
data | T[] | The array to index for the previous item. |
Returns
T
the previous item from the array, the same one if the index is 0.
longest()
function longest(arr): string;Defined in: utils/arrays.ts:31
Returns the longest string within a given array. It does not return the actual length of the longest item, just the longest item.
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | string[] | Array of strings to find the longest item. |
Returns
string
Longest string from the array.
negativeValues()
function negativeValues(arr): NumberArray;Defined in: utils/arrays.ts:69
Get all only negative values from a given array.
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | NumberArray | The array to filter out positive and zero values from. |
Returns
NumberArray
Negative only items.
nonNegativeValues()
function nonNegativeValues(array): NumberArray;Defined in: utils/arrays.ts:196
Get all only positive and zero values from a given array.
Parameters
| Parameter | Type | Description |
|---|---|---|
array | NumberArray | The array that is to be filtered. |
Returns
NumberArray
the array with only natural numbers.
sum()
function sum(arr): number;Defined in: utils/arrays.ts:186
Array sum function.
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | number[] | Array to be summed. |
Returns
number
sum of the array.
uniqueValues()
function uniqueValues<T>(arr): Set<T>;Defined in: utils/arrays.ts:81
Get all unique values from a given array.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | T[] | The array to convert into a set. |
Returns
Set<T>
the set of the array.