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.
typescriptrange(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.
typescriptreplicate('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.
typescriptprepend([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.
typescriptprependAll([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.
typescriptappend([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.
typescriptappendAll([1, 2], [3, 4]) // returns [1, 2, 3, 4]
Array Properties
isEmptyArray<T>(array: Array<T>): boolean
Checks if an array is empty.
typescriptisEmptyArray([]) // returns true isEmptyArray([1]) // returns false
isNonEmptyArray<T>(array: Array<T>): boolean
Checks if an array is non-empty.
typescriptisNonEmptyArray([1]) // returns true isNonEmptyArray([]) // returns false
length<T>(array: Array<T>): number
Gets the length of an array.
typescriptlength([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.
typescriptget([1, 2, 3], 1) // returns 2
head<T>(array: Array<T>): T
Gets the first element of an array.
typescripthead([1, 2, 3]) // returns 1
tail<T>(array: Array<T>): Array<T>
Gets the array without the first element.
typescripttail([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.
typescripttake([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.
typescripttakeRight([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.
typescripttakeWhile([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.
typescriptdrop([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.
typescriptdropRight([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.
typescriptdropWhile([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.
typescriptfindFirstIndex([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.
typescriptfindLastIndex([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.
typescriptfindFirst([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.
typescriptfindLast([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.
typescriptinsertAt([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.
typescriptreplace([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.
typescriptremove([1, 2, 3], 1) // returns [1, 3]
reverse<T>(array: Array<T>): Array<T>
Reverses the elements of an array.
typescriptreverse([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.
typescriptmap([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.
typescriptflatMap([1, 2], x => [x, x]) // returns [1, 1, 2, 2]
flatten<T>(array: Array<Array<T>>): Array<T>
Flattens an array of arrays.
typescriptflatten([[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.
typescriptzip([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.
typescriptunzip([[1, 'a'], [2, 'b']]) // returns [[1, 2], ['a', 'b']]
dedupe<T>(array: Array<T>): Array<T>
Removes duplicates from an array.
typescriptdedupe([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.
typescriptjoin(['a', 'b', 'c'], ',') // returns "a,b,c"