@xylabs/array
v5.0.87
Published
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Readme
@xylabs/array
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Reference
@xylabs/array
Functions
| Function | Description |
| ------ | ------ |
| containsAll | Checks whether the source array contains every element in the target array. |
| distinct | Array filter callback that removes duplicate values, with correct NaN handling. Use with array.filter(distinct). |
| filterAs | Maps each element using the predicate and filters out nullish results. |
| filterAsync | Returns the elements of an array that meet the condition specified in a callback function. |
| findAs | Maps each element using the predicate and returns the first non-nullish result. |
| findLastAs | Maps each element using the predicate and returns the last non-nullish result. |
| flatten | Concatenates two values or arrays into a single flat array, filtering out nullish entries. |
| uniq | Returns a new array with duplicate values removed. |
| uniqBy | Returns a new array with duplicates removed, using a key function for comparison. |
functions
containsAll
function containsAll<T>(source: T[], target: T[]): boolean;Checks whether the source array contains every element in the target array.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| source | T[] | The array to search within |
| target | T[] | The elements that must all be present |
Returns
boolean
True if every target element exists in source
distinct
function distinct<T>(
value: T,
index: number,
array: T[]): boolean;Array filter callback that removes duplicate values, with correct NaN handling.
Use with array.filter(distinct).
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| index | number |
| array | T[] |
Returns
boolean
filterAs
function filterAs<In, Out>(x: In[], predicate: (a: In) => Out): NonNullable<Out>[];Maps each element using the predicate and filters out nullish results.
Type Parameters
| Type Parameter |
| ------ |
| In |
| Out |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| x | In[] | The input array |
| predicate | (a: In) => Out | Transform function applied to each element |
Returns
NonNullable<Out>[]
Array of non-nullish transformed values
filterAsync
function filterAsync<T>(array: T[], predicate: (value: T, index: number, array: T[]) => Promise<boolean>): Promise<T[]>;Returns the elements of an array that meet the condition specified in a callback function.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| array | T[] | The array to filter. |
| predicate | (value: T, index: number, array: T[]) => Promise<boolean> | A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. |
Returns
Promise<T[]>
The elements of an array that meet the condition specified in a callback function.
findAs
function findAs<In, Out>(x: In[], predicate: (a: In) => Out): NonNullable<Out> | undefined;Maps each element using the predicate and returns the first non-nullish result.
Type Parameters
| Type Parameter |
| ------ |
| In |
| Out |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| x | In[] | The input array |
| predicate | (a: In) => Out | Transform function applied to each element |
Returns
NonNullable<Out> | undefined
The first non-nullish transformed value, or undefined
findLastAs
function findLastAs<In, Out>(x: In[], predicate: (a: In) => Out): NonNullable<Out> | undefined;Maps each element using the predicate and returns the last non-nullish result.
Type Parameters
| Type Parameter |
| ------ |
| In |
| Out |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| x | In[] | The input array |
| predicate | (a: In) => Out | Transform function applied to each element |
Returns
NonNullable<Out> | undefined
The last non-nullish transformed value, or undefined
flatten
function flatten<T>(a?: T | ConcatArray<T>, b?: T | ConcatArray<T>): T[];Concatenates two values or arrays into a single flat array, filtering out nullish entries.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| a? | T | ConcatArray<T> | First value or array |
| b? | T | ConcatArray<T> | Second value or array |
Returns
T[]
A flat array of non-nullish elements
uniq
function uniq<T>(arr: T[]): T[];Returns a new array with duplicate values removed.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| arr | T[] | The input array |
Returns
T[]
A deduplicated array
uniqBy
function uniqBy<T, I>(arr: T[], iteratee: (item: T) => I): T[];Returns a new array with duplicates removed, using a key function for comparison.
Type Parameters
| Type Parameter |
| ------ |
| T |
| I |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| arr | T[] | The input array |
| iteratee | (item: T) => I | Function that returns the key to compare by |
Returns
T[]
A deduplicated array keeping the first occurrence of each key
Part of sdk-js
Maintainers
License
See the LICENSE file for license details
