Skip to content

Array

Functions to manipulate arrays immutably and type-safely. All functions preserve the original array and return a new value.

How to import?

The library exposes the DArray and A namespaces from the main entry or via direct import (tree-shaking friendly), which lets you only load what you need.

typescript
import { DArray, A } from "@duplojs/utils";
import * as DArray from "@duplojs/utils/array";
import * as A from "@duplojs/utils/array";

Creation and conversion

from

Creates an array from an iterable or an array-like object.

toTuple

Converts an array to a tuple with strict typing.

Element access

at

Returns the element at an index (supports negative indexes).

first

Returns the first element of the array.

last

Returns the last element of the array.

length

Returns the length of the array.

lengthEqual

Checks that an array has an exact length.

Search and test

includes

Checks whether an array contains an element.

notIncludes

Checks that an array does not contain a given value (type guard).

indexOf

Returns the index of the first occurrence of an element.

lastIndexOf

Returns the index of the last occurrence of an element.

isLastIndex

Indicates whether an index corresponds to the last element of the array.

find

Finds the first element that satisfies a condition.

findLast

Finds the last element that satisfies a condition.

findIndex

Finds the index of the first element that satisfies a condition.

findLastIndex

Finds the index of the last element that satisfies a condition.

every

Checks whether all elements satisfy a condition.

some

Checks whether at least one element satisfies a condition.

is

Checks whether a value is an array (type guard).

Transformation

map

Applies a function to each element and returns a new array.

filter

Filters elements according to a condition.

select

Filters and maps in a single pass (with output inference).

slice

Extracts a portion of the array without modifying it.

flat

Flattens a nested array by a specified depth.

flatMap

Applies a function then flattens the result by one level.

chunk

Splits an array into fixed-size chunks.

reverse

Reverses the order of the elements in the array.

sort

Sorts the elements of the array.

Aggregation

reduce

Reduces the array to a single value (left to right).

reduceRight

Reduces the array to a single value (right to left).

join

Joins all elements into a string with a separator.

group

Groups elements according to a key function.

sum

Computes the sum of the array elements.

Min/Max

minOf

Returns the minimum value according to a projection function.

maxOf

Returns the maximum value according to a projection function.

minElements

Returns the element(s) having the minimum value.

maxElements

Returns the element(s) having the maximum value.

Modification

set

Updates an element at a specific index.

fill

Fills a section of the array with a value.

fillAll

Fills the entire array with a value.

copyWithin

Copies a section of the array to another location.

insert

Adds a value at the end of the array (arguments reversed for currying).

Add and remove

push

Adds one or more elements to the end of the array.

pop

Removes and returns the last element.

unshift

Adds one or more elements to the beginning of the array.

shift

Removes and returns the first element.

concat

Concatenates multiple arrays together.

spliceDelete

Deletes elements starting from an index.

spliceInsert

Inserts elements at a specific index.

spliceReplace

Replaces elements starting from an index.

Find and modify

findAndReplace

Finds an element and replaces it with a new value.

findAndSpliceDelete

Finds and deletes an element.

findAndSpliceInsert

Finds an element and inserts new elements.

findAndSpliceReplace

Finds and replaces an element via splice.

Utilities

coalescing

Returns the first non-null and non-undefined element.

Released under the MIT license.