This module provides a collection of utility functions for working with arrays. It extends functionality from the Effect-TS library.

Array Creation

range(start: number, end: number): Array<number>

Returns an array containing a range of integers, including both endpoints.
typescript
range(1, 3) // returns [1, 2, 3]

replicate<T>(value: T, number: number): Array<T>

Returns an array containing a value repeated the specified number of times.
typescript
replicate('a', 3) // returns ['a', 'a', 'a']

Array Modification

prepend<T>(array: Array<T>, value: T): Array<T>

Adds an element to the front of an array.
typescript
prepend([1, 2], 0) // returns [0, 1, 2]

prependAll<T>(array: Array<T>, values: Array<T>): Array<T>

Adds multiple elements to the front of an array.
typescript
prependAll([3, 4], [1, 2]) // returns [1, 2, 3, 4]

append<T>(array: Array<T>, value: T): Array<T>

Adds an element to the end of an array.
typescript
append([1, 2], 3) // returns [1, 2, 3]

appendAll<T>(array: Array<T>, values: Array<T>): Array<T>

Adds multiple elements to the end of an array.
typescript
appendAll([1, 2], [3, 4]) // returns [1, 2, 3, 4]

Array Properties

isEmptyArray<T>(array: Array<T>): boolean

Checks if an array is empty.
typescript
isEmptyArray([]) // returns true isEmptyArray([1]) // returns false

isNonEmptyArray<T>(array: Array<T>): boolean

Checks if an array is non-empty.
typescript
isNonEmptyArray([1]) // returns true isNonEmptyArray([]) // returns false

length<T>(array: Array<T>): number

Gets the length of an array.
typescript
length([1, 2, 3]) // returns 3

Array Access

get<T>(array: Array<T>, index: number): T

Gets the element at a specific index in an array.
typescript
get([1, 2, 3], 1) // returns 2

head<T>(array: Array<T>): T

Gets the first element of an array.
typescript
head([1, 2, 3]) // returns 1

tail<T>(array: Array<T>): Array<T>

Gets the array without the first element.
typescript
tail([1, 2, 3]) // returns [2, 3]

Array Slicing

take<T>(array: Array<T>, n: number): Array<T>

Takes the first n elements of an array.
typescript
take([1, 2, 3, 4], 2) // returns [1, 2]

takeRight<T>(array: Array<T>, n: number): Array<T>

Takes the last n elements of an array.
typescript
takeRight([1, 2, 3, 4], 2) // returns [3, 4]

takeWhile<T>(array: Array<T>, predicate: (a: T) => boolean): Array<T>

Takes elements from an array while a predicate is true.
typescript
takeWhile([1, 2, 3, 4], x => x < 3) // returns [1, 2]

drop<T>(array: Array<T>, n: number): Array<T>

Drops the first n elements of an array.
typescript
drop([1, 2, 3, 4], 2) // returns [3, 4]

dropRight<T>(array: Array<T>, n: number): Array<T>

Drops the last n elements of an array.
typescript
dropRight([1, 2, 3, 4], 2) // returns [1, 2]

dropWhile<T>(array: Array<T>, predicate: (a: T) => boolean): Array<T>

Drops elements from an array while a predicate is true.
typescript
dropWhile([1, 2, 3, 4], x => x < 3) // returns [3, 4]

Array Search

findFirstIndex<T>(array: Array<T>, predicate: (a: T) => boolean): number

Finds the index of the first element that satisfies a predicate.
typescript
findFirstIndex([1, 2, 3], x => x > 1) // returns 1

findLastIndex<T>(array: Array<T>, predicate: (a: T) => boolean): number

Finds the index of the last element that satisfies a predicate.
typescript
findLastIndex([1, 2, 3], x => x < 3) // returns 1

findFirst<T>(array: Array<T>, predicate: (a: T) => boolean): T

Finds the first element that satisfies a predicate.
typescript
findFirst([1, 2, 3], x => x > 1) // returns 2

findLast<T>(array: Array<T>, predicate: (a: T) => boolean): T

Finds the last element that satisfies a predicate.
typescript
findLast([1, 2, 3], x => x < 3) // returns 2

Array Manipulation

insertAt<T>(array: Array<T>, index: number, value: T): Array<T>

Inserts an element at a specific index in an array.
typescript
insertAt([1, 3], 1, 2) // returns [1, 2, 3]

replace<T>(array: Array<T>, index: number, value: T): Array<T>

Replaces an element at a specific index in an array.
typescript
replace([1, 2, 3], 1, 4) // returns [1, 4, 3]

remove<T>(array: Array<T>, index: number): Array<T>

Removes an element at a specific index in an array.
typescript
remove([1, 2, 3], 1) // returns [1, 3]

reverse<T>(array: Array<T>): Array<T>

Reverses the elements of an array.
typescript
reverse([1, 2, 3]) // returns [3, 2, 1]

Array Transformation

map<T, U>(array: Array<T>, f: (a: T) => U): Array<U>

Maps a function over the elements of an array.
typescript
map([1, 2, 3], x => x * 2) // returns [2, 4, 6]

flatMap<T, U>(array: Array<T>, f: (a: T) => Array<U>): Array<U>

Flat maps a function over the elements of an array.
typescript
flatMap([1, 2], x => [x, x]) // returns [1, 1, 2, 2]

flatten<T>(array: Array<Array<T>>): Array<T>

Flattens an array of arrays.
typescript
flatten([[1], [2, 3]]) // returns [1, 2, 3]

Array Operations

zip<T, U>(array: Array<T>, that: Array<U>): Array<[T, U]>

Zips two arrays together.
typescript
zip([1, 2], ['a', 'b']) // returns [[1, 'a'], [2, 'b']]

unzip<T, U>(array: Array<[T, U]>): [Array<T>, Array<U>]

Unzips an array of pairs into two arrays.
typescript
unzip([[1, 'a'], [2, 'b']]) // returns [[1, 2], ['a', 'b']]

dedupe<T>(array: Array<T>): Array<T>

Removes duplicates from an array.
typescript
dedupe([1, 1, 2, 2, 3]) // returns [1, 2, 3]

join(array: Array<any>, separator: string): string

Joins array elements into a string with a separator.
typescript
join(['a', 'b', 'c'], ',') // returns "a,b,c"

Powered by Notaku
Share
Content