- Opt-in mutability with
remmi - Reference preservation (
filter(array, () => true) === array) - Pipe-friendly (
pipe(filter(() => true))(array)) - Graceful failure handling (
at(),atOr(),atOrElse(),atOrThrow())
npm install @monstermann/arraypnpm add @monstermann/arrayyarn add @monstermann/arraybun add @monstermann/arraynpm install -D @monstermann/unplugin-arraypnpm -D add @monstermann/unplugin-arrayyarn -D add @monstermann/unplugin-arraybun -D add @monstermann/unplugin-array// vite.config.ts
import array from "@monstermann/unplugin-array/vite";
export default defineConfig({
plugins: [array()],
});// rollup.config.js
import array from "@monstermann/unplugin-array/rollup";
export default {
plugins: [array()],
};// rolldown.config.js
import array from "@monstermann/unplugin-array/rolldown";
export default {
plugins: [array()],
};// webpack.config.js
const array = require("@monstermann/unplugin-array/webpack");
module.exports = {
plugins: [array()],
};// rspack.config.js
const array = require("@monstermann/unplugin-array/rspack");
module.exports = {
plugins: [array()],
};// esbuild.config.js
import { build } from "esbuild";
import array from "@monstermann/unplugin-array/esbuild";
build({
plugins: [array()],
});function Array.append(array: T[], value: T): T[]Appends value to the end of array.
import { Array } from "@monstermann/array";
Array.append([1, 2, 3], 4); // [1, 2, 3, 4]import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.append(4)); // [1, 2, 3, 4]function Array.at(array: T[], offset: number): T | undefinedReturns the value at the specified offset.
import { Array } from "@monstermann/array";
Array.at([1, 2, 3], -1); // 3import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.at(-1)); // 3function Array.atOr(array: T[], offset: number, fallback: U): T | UReturns the value at the specified offset. Returns fallback if the offset was out of range, or the retrieved value was nullable.
import { Array } from "@monstermann/array";
Array.atOr([1, null], -1, 2); // 2import { Array } from "@monstermann/array";
pipe([1, null], Array.atOr(-1, 2)); // 2function Array.atOrElse(
array: T[],
offset: number,
fallback: (array: T[]) => U
): T | UReturns the value at the specified offset. Calls fallback if the offset was out of range, or the retrieved value was nullable.
import { Array } from "@monstermann/array";
Array.atOrElse([1, null], -1, (array) => array.length); // 2import { Array } from "@monstermann/array";
pipe(
[1, null],
Array.atOrElse(-1, (array) => array.length),
); // 2function Array.atOrThrow(array: T[], offset: number): TReturns the value at the specified offset, throws an exception if the offset was out of range, or the retrieved value was nullable.
import { Array } from "@monstermann/array";
Array.atOrThrow([1, null], -1); // Errorimport { Array } from "@monstermann/array";
pipe([1, null], Array.atOrThrow(-1)); // Errorfunction Array.clone(array: T[]): T[]Creates a shallow copy of array, unless marked as mutable with markAsMutable inside a mutation context (see @monstermann/remmi).
import { Array } from "@monstermann/array";
Array.clone([1, 2, 3, 4]); // [1, 2, 3, 4]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.clone()); // [1, 2, 3, 4]function Array.compact(array: T[]): T[]Removes all nullable values from array.
import { Array } from "@monstermann/array";
Array.compact([1, null, undefined]); // [1]import { Array } from "@monstermann/array";
pipe([1, null, undefined], Array.compact()); // [1]function Array.concat(array: T[], source: T[]): T[]Concatenates source array to the end of array, returning a new array with the combined elements.
import { Array } from "@monstermann/array";
Array.concat([1, 2], [3, 4]); // [1, 2, 3, 4]import { Array } from "@monstermann/array";
pipe([1, 2], Array.concat([3, 4])); // [1, 2, 3, 4]function Array.countBy(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): numberCounts the number of elements in the target array satisfy the provided predicate function.
import { Array } from "@monstermann/array";
const isEven = (n) => n % 2 === 0;
Array.countBy([1, 2, 3, 4, 5], isEven); // 2import { Array } from "@monstermann/array";
const isEven = (n) => n % 2 === 0;
pipe([1, 2, 3, 4, 5], Array.countBy(isEven)); // 2function Array.create(
target: Iterable<T> | ArrayLike<T>,
map?: (value: T, index: number) => U
): U[]An alias for Array.from(target, map?).
import { Array } from "@monstermann/array";
Array.create({ length: 3 }, (_, i) => i); // [0, 1, 2]function Array.drop(array: T[], amount: number): T[]Removes the first amount elements from array.
import { Array } from "@monstermann/array";
Array.drop([1, 2, 3, 4, 5], 2); // [3, 4, 5]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4, 5], Array.drop(2)); // [3, 4, 5]function Array.dropLast(array: T[], amount: number): T[]Removes amount of elements from the end of the target array.
import { Array } from "@monstermann/array";
Array.dropLast([1, 2, 3, 4, 5], 2); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4, 5], Array.dropLast(2)); // [1, 2, 3]const Array.empty: []A reference to an empty array - useful if you want to clear an array, but want any === checks to pass if it already was empty.
import { Array } from "@monstermann/array";
empty; // []function Array.every(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): booleanTests whether all elements in the array pass the test implemented by the predicate function. It returns true if all elements pass, otherwise false.
import { Array } from "@monstermann/array";
const isEven = (n: number) => n % 2 === 0;
Array.every([2, 4, 6], isEven); // true
Array.every([2, 4, 7], isEven); // falseimport { Array } from "@monstermann/array";
const isEven = (n: number) => n % 2 === 0;
pipe([2, 4, 6], Array.every(isEven)); // true
pipe([2, 4, 7], Array.every(isEven)); // falsefunction Array.filter(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): T[]Filters elements from target array based on the predicate function by.
import { Array } from "@monstermann/array";
Array.filter([1, 2, 3, 4], (x) => x > 2); // [3, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.filter((x) => x > 2),
); // [3, 4]function Array.find(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): T | undefinedReturns the first element in array that satisfies the provided predicate function, or undefined if no element is found.
import { Array } from "@monstermann/array";
Array.find([1, 2, 3, 4], (x) => x > 2); // 3import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.find((x) => x > 2),
); // 3function Array.findIndex(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): numberReturns the index of the first element in array that satisfies the provided predicate function, or -1 if no element is found.
import { Array } from "@monstermann/array";
Array.findIndex([1, 2, 3, 4], (x) => x > 2); // 2import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findIndex((x) => x > 2),
); // 2function Array.findIndexOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: U
): number | UReturns the index of the first element in target that satisfies the provided predicate function. If no element satisfies the predicate, returns or.
import { Array } from "@monstermann/array";
Array.findIndexOr([1, 2, 3, 4], (x) => x > 2, -1); // 2
Array.findIndexOr([1, 2, 3, 4], (x) => x > 5, -1); // -1import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findIndexOr((x) => x > 2, -1),
); // 2
pipe(
[1, 2, 3, 4],
Array.findIndexOr((x) => x > 5, -1),
); // -1function Array.findIndexOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: (array: T[]) => U
): number | UReturns the index of the first element in target that satisfies the provided predicate function. If no element satisfies the predicate, calls orElse with the original array.
import { Array } from "@monstermann/array";
Array.findIndexOrElse(
[1, 2, 3, 4],
(x) => x > 2,
() => -1,
); // 2
Array.findIndexOrElse(
[1, 2, 3, 4],
(x) => x > 5,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findIndexOrElse(
(x) => x > 2,
() => -1,
),
); // 2
pipe(
[1, 2, 3, 4],
Array.findIndexOrElse(
(x) => x > 5,
(arr) => arr.length,
),
); // 4function Array.findIndexOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): numberReturns the index of the first element in target that satisfies the provided predicate function. If no element satisfies the predicate, throws an error.
import { Array } from "@monstermann/array";
Array.findIndexOrThrow([1, 2, 3, 4], (x) => x > 2); // 2
Array.findIndexOrThrow([1, 2, 3, 4], (x) => x > 5); // throws FnErrorimport { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findIndexOrThrow((x) => x > 2),
); // 2
pipe(
[1, 2, 3, 4],
Array.findIndexOrThrow((x) => x > 5),
); // throws FnErrorfunction Array.findLast(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): T | undefinedReturns the last element in array that satisfies the provided predicate function, or undefined if no element is found.
import { Array } from "@monstermann/array";
Array.findLast([1, 2, 3, 4], (x) => x > 2); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findLast((x) => x > 2),
); // 4function Array.findLastIndex(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): numberReturns the index of the last element in array that satisfies the provided predicate function, or -1 if no element is found.
import { Array } from "@monstermann/array";
Array.findLastIndex([1, 2, 3, 4], (x) => x > 2); // 3import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findLastIndex((x) => x > 2),
); // 3function Array.findLastIndexOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: U
): number | UReturns the index of the last element in target that satisfies the provided predicate function. If no element satisfies the predicate, returns or.
import { Array } from "@monstermann/array";
Array.findLastIndexOr([1, 3, 2, 4], (x) => x > 2, -1); // 3
Array.findLastIndexOr([1, 2, 3, 4], (x) => x > 5, -1); // -1import { Array } from "@monstermann/array";
pipe(
[1, 3, 2, 4],
Array.findLastIndexOr((x) => x > 2, -1),
); // 3
pipe(
[1, 2, 3, 4],
Array.findLastIndexOr((x) => x > 5, -1),
); // -1function Array.findLastIndexOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: (array: T[]) => U
): number | UReturns the index of the last element in target that satisfies the provided predicate function. If no element satisfies the predicate, calls orElse with the original array.
import { Array } from "@monstermann/array";
Array.findLastIndexOrElse(
[1, 3, 2, 4],
(x) => x > 2,
() => -1,
); // 3
Array.findLastIndexOrElse(
[1, 2, 3, 4],
(x) => x > 5,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 3, 2, 4],
Array.findLastIndexOrElse(
(x) => x > 2,
() => -1,
),
); // 3
pipe(
[1, 2, 3, 4],
Array.findLastIndexOrElse(
(x) => x > 5,
(arr) => arr.length,
),
); // 4function Array.findLastIndexOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): numberReturns the index of the last element in target that satisfies the provided predicate function. If no element satisfies the predicate, throws an error.
import { Array } from "@monstermann/array";
Array.findLastIndexOrThrow([1, 3, 2, 4], (x) => x > 2); // 3
Array.findLastIndexOrThrow([1, 2, 3, 4], (x) => x > 5); // throws FnErrorimport { Array } from "@monstermann/array";
pipe(
[1, 3, 2, 4],
Array.findLastIndexOrThrow((x) => x > 2),
); // 3
pipe(
[1, 2, 3, 4],
Array.findLastIndexOrThrow((x) => x > 5),
); // throws FnErrorfunction Array.findLastOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: U
): T | UReturns the last element in array that satisfies the provided predicate function, or fallback if no element is found.
import { Array } from "@monstermann/array";
Array.findLastOr([1, 2, 3, 4], (x) => x > 10, 0); // 0import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findLastOr((x) => x > 10, 0),
); // 0function Array.findLastOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: (array: T[]) => U
): T | UReturns the last element in array that satisfies the provided predicate function, or the result of calling callback with the array if no element is found.
import { Array } from "@monstermann/array";
Array.findLastOrElse(
[1, 2, 3, 4],
(x) => x > 10,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findLastOrElse(
(x) => x > 10,
(arr) => arr.length,
),
); // 4function Array.findLastOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): TReturns the last element in array that satisfies the provided predicate function, or throws an error if no element is found.
import { Array } from "@monstermann/array";
Array.findLastOrThrow([1, 2, 3, 4], (x) => x > 2); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findLastOrThrow((x) => x > 2),
); // 4function Array.findMap(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U
): T[]Finds the first element in array that satisfies the provided predicate function and applies the mapper function to it, returning a new array with the mapped element.
import { Array } from "@monstermann/array";
Array.findMap(
[1, 2, 3, 4],
(x) => x > 2,
(x) => x * 10,
); // [1, 2, 30, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMap(
(x) => x > 2,
(x) => x * 10,
),
); // [1, 2, 30, 4]function Array.findMapAll(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U
): T[]Finds all elements in array that satisfy the provided predicate function and applies the mapper function to each of them, returning a new array with the mapped elements.
import { Array } from "@monstermann/array";
Array.findMapAll(
[1, 2, 3, 4],
(x) => x > 2,
(x) => x * 10,
); // [1, 2, 30, 40]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMapAll(
(x) => x > 2,
(x) => x * 10,
),
); // [1, 2, 30, 40]function Array.findMapLast(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U
): T[]Finds the last element in array that satisfies the provided predicate function and applies the mapper function to it, returning a new array with the mapped element.
import { Array } from "@monstermann/array";
Array.findMapLast(
[1, 2, 3, 4],
(x) => x > 2,
(x) => x * 10,
); // [1, 2, 3, 40]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMapLast(
(x) => x > 2,
(x) => x * 10,
),
); // [1, 2, 3, 40]function Array.findMapLastOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U,
fallback: V
): T[] | VFinds the last element in array that satisfies the provided predicate function and applies the mapper function to it, returning a new array with the mapped element, or fallback if no element is found.
import { Array } from "@monstermann/array";
Array.findMapLastOr(
[1, 2, 3, 4],
(x) => x > 10,
(x) => x * 10,
[],
); // []import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMapLastOr(
(x) => x > 10,
(x) => x * 10,
[],
),
); // []function Array.findMapLastOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U,
fallback: (array: T[]) => V
): T[] | VFinds the last element in array that satisfies the provided predicate function and applies the mapper function to it, returning a new array with the mapped element, or the result of calling callback with the array if no element is found.
import { Array } from "@monstermann/array";
Array.findMapLastOrElse(
[1, 2, 3, 4],
(x) => x > 10,
(x) => x * 10,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMapLastOrElse(
(x) => x > 10,
(x) => x * 10,
(arr) => arr.length,
),
); // 4function Array.findMapLastOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U
): T[]Finds the last element in array that satisfies the provided predicate function and applies the mapper function to it, returning a new array with the mapped element, or throws an error if no element is found.
import { Array } from "@monstermann/array";
Array.findMapLastOrThrow(
[1, 2, 3, 4],
(x) => x > 2,
(x) => x * 10,
); // [1, 2, 3, 40]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMapLastOrThrow(
(x) => x > 2,
(x) => x * 10,
),
); // [1, 2, 3, 40]function Array.findMapOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U,
fallback: V
): T[] | VFinds the first element in array that satisfies the provided predicate function and applies the mapper function to it, returning a new array with the mapped element, or fallback if no element is found.
import { Array } from "@monstermann/array";
Array.findMapOr(
[1, 2, 3, 4],
(x) => x > 10,
(x) => x * 10,
[],
); // []import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMapOr(
(x) => x > 10,
(x) => x * 10,
[],
),
); // []function Array.findMapOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U,
fallback: (array: T[]) => V
): T[] | VFinds the first element in array that satisfies the provided predicate function and applies the mapper function to it, returning a new array with the mapped element, or the result of calling callback with the array if no element is found.
import { Array } from "@monstermann/array";
Array.findMapOrElse(
[1, 2, 3, 4],
(x) => x > 10,
(x) => x * 10,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMapOrElse(
(x) => x > 10,
(x) => x * 10,
(arr) => arr.length,
),
); // 4function Array.findMapOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
mapper: (value: T, index: number, array: T[]) => U
): T[]Finds the first element in array that satisfies the provided predicate function and applies the mapper function to it, returning a new array with the mapped element, or throws an error if no element is found.
import { Array } from "@monstermann/array";
Array.findMapOrThrow(
[1, 2, 3, 4],
(x) => x > 2,
(x) => x * 10,
); // [1, 2, 30, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findMapOrThrow(
(x) => x > 2,
(x) => x * 10,
),
); // [1, 2, 30, 4]function Array.findOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: U
): T | UReturns the first element in array that satisfies the provided predicate function, or fallback if no element is found.
import { Array } from "@monstermann/array";
Array.findOr([1, 2, 3, 4], (x) => x > 10, 0); // 0import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findOr((x) => x > 10, 0),
); // 0function Array.findOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: (array: T[]) => U
): T | UReturns the first element in array that satisfies the provided predicate function, or the result of calling callback with the array if no element is found.
import { Array } from "@monstermann/array";
Array.findOrElse(
[1, 2, 3, 4],
(x) => x > 10,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findOrElse(
(x) => x > 10,
(arr) => arr.length,
),
); // 4function Array.findOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): TReturns the first element in array that satisfies the provided predicate function, or throws an error if no element is found.
import { Array } from "@monstermann/array";
Array.findOrThrow([1, 2, 3, 4], (x) => x > 2); // 3import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findOrThrow((x) => x > 2),
); // 3function Array.findRemove(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): T[]Finds the first element in array that satisfies the provided predicate function and removes it, returning a new array without the removed element.
import { Array } from "@monstermann/array";
Array.findRemove([1, 2, 3, 4], (x) => x > 2); // [1, 2, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findRemove((x) => x > 2),
); // [1, 2, 4]function Array.findRemoveLast(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): T[]Finds the last element in array that satisfies the provided predicate function and removes it, returning a new array without the removed element.
import { Array } from "@monstermann/array";
Array.findRemoveLast([1, 2, 3, 4], (x) => x > 2); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findRemoveLast((x) => x > 2),
); // [1, 2, 3]function Array.findRemoveLastOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: U
): T[] | UFinds the last element in array that satisfies the provided predicate function and removes it, returning a new array without the removed element, or fallback if no element is found.
import { Array } from "@monstermann/array";
Array.findRemoveLastOr([1, 2, 3, 4], (x) => x > 10, []); // []import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findRemoveLastOr((x) => x > 10, []),
); // []function Array.findRemoveLastOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: (array: T[]) => U
): T[] | UFinds the last element in array that satisfies the provided predicate function and removes it, returning a new array without the removed element, or the result of calling callback with the array if no element is found.
import { Array } from "@monstermann/array";
Array.findRemoveLastOrElse(
[1, 2, 3, 4],
(x) => x > 10,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findRemoveLastOrElse(
(x) => x > 10,
(arr) => arr.length,
),
); // 4function Array.findRemoveLastOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): T[]Finds the last element in array that satisfies the provided predicate function and removes it, returning a new array without the removed element, or throws an error if no element is found.
import { Array } from "@monstermann/array";
Array.findRemoveLastOrThrow([1, 2, 3, 4], (x) => x > 2); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findRemoveLastOrThrow((x) => x > 2),
); // [1, 2, 3]function Array.findRemoveOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: U
): T[] | UFinds the first element in array that satisfies the provided predicate function and removes it, returning a new array without the removed element, or fallback if no element is found.
import { Array } from "@monstermann/array";
Array.findRemoveOr([1, 2, 3, 4], (x) => x > 10, []); // []import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findRemoveOr((x) => x > 10, []),
); // []function Array.findRemoveOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
fallback: (array: T[]) => U
): T[] | UFinds the first element in array that satisfies the provided predicate function and removes it, returning a new array without the removed element, or the result of calling callback with the array if no element is found.
import { Array } from "@monstermann/array";
Array.findRemoveOrElse(
[1, 2, 3, 4],
(x) => x > 10,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findRemoveOrElse(
(x) => x > 10,
(arr) => arr.length,
),
); // 4function Array.findRemoveOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): T[]Finds the first element in array that satisfies the provided predicate function and removes it, returning a new array without the removed element, or throws an error if no element is found.
import { Array } from "@monstermann/array";
Array.findRemoveOrThrow([1, 2, 3, 4], (x) => x > 2); // [1, 2, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findRemoveOrThrow((x) => x > 2),
); // [1, 2, 4]function Array.findReplace(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
value: U
): T[]Finds the first element in array that satisfies the provided predicate function and replaces it with replacement, returning a new array with the replaced element.
import { Array } from "@monstermann/array";
Array.findReplace([1, 2, 3, 4], (x) => x > 2, 10); // [1, 2, 10, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findReplace((x) => x > 2, 10),
); // [1, 2, 10, 4]function Array.findReplaceLast(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
value: U
): T[]Finds the last element in array that satisfies the provided predicate function and replaces it with replacement, returning a new array with the replaced element.
import { Array } from "@monstermann/array";
Array.findReplaceLast([1, 2, 3, 4], (x) => x > 2, 10); // [1, 2, 3, 10]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findReplaceLast((x) => x > 2, 10),
); // [1, 2, 3, 10]function Array.findReplaceLastOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
value: U,
fallback: V
): T[] | VFinds the last element in array that satisfies the provided predicate function and replaces it with replacement, returning a new array with the replaced element, or fallback if no element is found.
import { Array } from "@monstermann/array";
Array.findReplaceLastOr([1, 2, 3, 4], (x) => x > 10, 99, []); // []import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findReplaceLastOr((x) => x > 10, 99, []),
); // []function Array.findReplaceLastOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
value: U,
fallback: (array: T[]) => V
): T[] | VFinds the last element in array that satisfies the provided predicate function and replaces it with replacement, returning a new array with the replaced element, or the result of calling callback with the array if no element is found.
import { Array } from "@monstermann/array";
Array.findReplaceLastOrElse(
[1, 2, 3, 4],
(x) => x > 10,
99,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findReplaceLastOrElse(
(x) => x > 10,
99,
(arr) => arr.length,
),
); // 4function Array.findReplaceLastOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
value: U
): T[]Finds the last element in array that satisfies the provided predicate function and replaces it with replacement, returning a new array with the replaced element, or throws an error if no element is found.
import { Array } from "@monstermann/array";
Array.findReplaceLastOrThrow([1, 2, 3, 4], (x) => x > 2, 99); // [1, 2, 3, 99]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findReplaceLastOrThrow((x) => x > 2, 99),
); // [1, 2, 3, 99]function Array.findReplaceOr(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
value: U,
fallback: V
): T[] | VFinds the first element in array that satisfies the provided predicate function and replaces it with replacement, returning a new array with the replaced element, or fallback if no element is found.
import { Array } from "@monstermann/array";
Array.findReplaceOr([1, 2, 3, 4], (x) => x > 10, 99, []); // []import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findReplaceOr((x) => x > 10, 99, []),
); // []function Array.findReplaceOrElse(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
value: U,
fallback: (array: T[]) => V
): T[] | VFinds the first element in array that satisfies the provided predicate function and replaces it with replacement, returning a new array with the replaced element, or the result of calling callback with the array if no element is found.
import { Array } from "@monstermann/array";
Array.findReplaceOrElse(
[1, 2, 3, 4],
(x) => x > 10,
99,
(arr) => arr.length,
); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findReplaceOrElse(
(x) => x > 10,
99,
(arr) => arr.length,
),
); // 4function Array.findReplaceOrThrow(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean,
value: U
): T[]Finds the first element in array that satisfies the provided predicate function and replaces it with replacement, returning a new array with the replaced element, or throws an error if no element is found.
import { Array } from "@monstermann/array";
Array.findReplaceOrThrow([1, 2, 3, 4], (x) => x > 2, 99); // [1, 2, 99, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.findReplaceOrThrow((x) => x > 2, 99),
); // [1, 2, 99, 4]function Array.first(array: T[]): T | undefinedReturns the first element of array, or undefined if the array is empty.
import { Array } from "@monstermann/array";
Array.first([1, 2, 3, 4]); // 1import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.first()); // 1function Array.firstOr(array: T[], fallback: U): T | UReturns the first element of array, or fallback if the array is empty.
import { Array } from "@monstermann/array";
Array.firstOr([1, 2, 3, 4], 0); // 1import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.firstOr(0)); // 1function Array.firstOrElse(array: T[], fallback: (array: T[]) => U): T | UReturns the first element of array, or the result of calling callback with the array if the array is empty.
import { Array } from "@monstermann/array";
Array.firstOrElse([1, 2, 3, 4], (arr) => arr.length); // 1import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.firstOrElse((arr) => arr.length),
); // 1function Array.firstOrThrow(array: T[]): TReturns the first element of array, or throws an error if the array is empty.
import { Array } from "@monstermann/array";
Array.firstOrThrow([1, 2, 3, 4]); // 1import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.firstOrThrow()); // 1function Array.flatMap(
array: T[],
mapper: (value: T, index: number, array: T[]) => U[]
): U[]Maps each element in array using the mapper function and flattens the result by one level.
import { Array } from "@monstermann/array";
Array.flatMap([1, 2, 3], (x) => [x, x * 2]); // [1, 2, 2, 4, 3, 6]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.flatMap((x) => [x, x * 2]),
); // [1, 2, 2, 4, 3, 6]function Array.forEach(
array: T[],
callback: (value: T, index: number, array: T[]) => void
): T[]Executes the provided callback function once for each element in array and returns the original array.
import { Array } from "@monstermann/array";
Array.forEach([1, 2, 3], (x) => console.log(x)); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.forEach((x) => console.log(x)),
); // [1, 2, 3]function Array.forEachRight(
array: T[],
callback: (value: T, index: number, array: T[]) => void
): T[]Executes the provided callback function once for each element in array in reverse order and returns the original array.
import { Array } from "@monstermann/array";
Array.forEachRight([1, 2, 3], (x) => console.log(x)); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.forEachRight((x) => console.log(x)),
); // [1, 2, 3]function Array.groupBy(
array: T[],
keySelector: (value: T, index: number, array: T[]) => string
): Record<string, T[]>Groups elements in array by the result of calling grouper function on each element, optionally transforming each element with transform, returning an object with keys as group values and values as arrays of elements.
import { Array } from "@monstermann/array";
Array.groupBy(
[1, 2, 3, 4],
(x) => (x % 2 === 0 ? "even" : "odd"),
(x) => x * 10,
); // { even: [20, 40], odd: [10, 30] }import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.groupBy(
(x) => (x % 2 === 0 ? "even" : "odd"),
(x) => x * 10,
),
); // { even: [20, 40], odd: [10, 30] }function Array.includes(array: T[], value: T): booleanReturns true if array contains value, otherwise returns false.
import { Array } from "@monstermann/array";
Array.includes([1, 2, 3, 4], 3); // trueimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.includes(3)); // truefunction Array.includesAll(array: T[], values: T[]): booleanReturns true if array contains all values, otherwise returns false. Supports iterables for the values parameter.
import { Array } from "@monstermann/array";
Array.includesAll([1, 2, 3, 4], [2, 3]); // trueimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.includesAll([2, 3])); // truefunction Array.includesAny(array: T[], values: T[]): booleanReturns true if array contains any of the values, otherwise returns false. Supports iterables for the values parameter.
import { Array } from "@monstermann/array";
Array.includesAny([1, 2, 3, 4], [5, 6, 2]); // trueimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.includesAny([5, 6, 2])); // truefunction Array.includesNone(array: T[], values: T[]): booleanReturns true if array contains none of the values, otherwise returns false. Supports iterables for the values parameter.
import { Array } from "@monstermann/array";
Array.includesNone([1, 2, 3, 4], [5, 6, 7]); // trueimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.includesNone([5, 6, 7])); // truefunction Array.indexBy(
array: T[],
keySelector: (value: T, index: number, array: T[]) => string
): Record<string, T>Creates a record by indexing the target array using the by function to generate keys. Optionally transforms values using the transform function.
import { Array } from "@monstermann/array";
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
];
Array.indexBy(users, (user) => user.id);
// { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } }
Array.indexBy(
users,
(user) => user.id,
(user) => user.name,
); // { 1: "Alice", 2: "Bob" }import { Array } from "@monstermann/array";
pipe(
users,
Array.indexBy((user) => user.id),
); // { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } }
pipe(
users,
Array.indexBy(
(user) => user.id,
(user) => user.name,
),
); // { 1: "Alice", 2: "Bob" }function Array.indexOf(array: T[], value: T): numberReturns the first index at which value can be found in array, or -1 if it is not present.
import { Array } from "@monstermann/array";
Array.indexOf([1, 2, 3, 2, 4], 2); // 1import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2, 4], Array.indexOf(2)); // 1function Array.indexOfOr(array: T[], value: T, fallback: U): number | UReturns the index of the first occurrence of value in target. If value is not found, returns or.
import { Array } from "@monstermann/array";
Array.indexOfOr([1, 2, 3, 2], 2, -1); // 1
Array.indexOfOr([1, 2, 3], 4, -1); // -1import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.indexOfOr(2, -1)); // 1
pipe([1, 2, 3], Array.indexOfOr(4, -1)); // -1function Array.indexOfOrElse(
array: T[],
value: T,
fallback: (array: T[]) => U
): number | UReturns the index of the first occurrence of value in target. If value is not found, calls orElse with the original array.
import { Array } from "@monstermann/array";
Array.indexOfOrElse([1, 2, 3, 2], 2, () => -1); // 1
Array.indexOfOrElse([1, 2, 3], 4, (arr) => arr.length); // 3import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 2],
Array.indexOfOrElse(2, () => -1),
); // 1
pipe(
[1, 2, 3],
Array.indexOfOrElse(4, (arr) => arr.length),
); // 3function Array.indexOfOrThrow(array: T[], value: T): numberReturns the index of the first occurrence of value in target. If value is not found, throws an error.
import { Array } from "@monstermann/array";
Array.indexOfOrThrow([1, 2, 3, 2], 2); // 1
Array.indexOfOrThrow([1, 2, 3], 4); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.indexOfOrThrow(2)); // 1
pipe([1, 2, 3], Array.indexOfOrThrow(4)); // throws FnErrorfunction Array.insertAllAt(array: T[], index: number, values: U[]): T[]Inserts all elements from values at the specified index in array, returning a new array with the inserted elements. Supports iterables for the values parameter.
import { Array } from "@monstermann/array";
Array.insertAllAt([1, 2, 3], 1, [10, 20]); // [1, 10, 20, 2, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.insertAllAt(1, [10, 20])); // [1, 10, 20, 2, 3]function Array.insertAllAtOr(
array: T[],
index: number,
values: U[],
fallback: V
): T[] | VInserts all values at the specified idx in target. If the index is out of bounds, returns or. Supports iterables.
import { Array } from "@monstermann/array";
Array.insertAllAtOr([1, 2, 3], 1, [8, 9], []); // [1, 8, 9, 2, 3]
Array.insertAllAtOr([1, 2, 3], 5, [8, 9], []); // []import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.insertAllAtOr(1, [8, 9], [])); // [1, 8, 9, 2, 3]
pipe([1, 2, 3], Array.insertAllAtOr(5, [8, 9], [])); // []function Array.insertAllAtOrElse(
array: T[],
index: number,
values: U[],
fallback: (array: T[]) => V
): T[] | VInserts all values at the specified idx in target. If the index is out of bounds, calls orElse with the original array. Supports iterables.
import { Array } from "@monstermann/array";
Array.insertAllAtOrElse([1, 2, 3], 1, [8, 9], () => []); // [1, 8, 9, 2, 3]
Array.insertAllAtOrElse([1, 2, 3], 5, [8, 9], (arr) => arr); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.insertAllAtOrElse(1, [8, 9], () => []),
); // [1, 8, 9, 2, 3]
pipe(
[1, 2, 3],
Array.insertAllAtOrElse(5, [8, 9], (arr) => arr),
); // [1, 2, 3]function Array.insertAllAtOrThrow(array: T[], index: number, values: U[]): T[]Inserts all values at the specified idx in target. If the index is out of bounds, throws an error. Supports iterables.
import { Array } from "@monstermann/array";
Array.insertAllAtOrThrow([1, 2, 3], 1, [8, 9]); // [1, 8, 9, 2, 3]
Array.insertAllAtOrThrow([1, 2, 3], 5, [8, 9]); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.insertAllAtOrThrow(1, [8, 9])); // [1, 8, 9, 2, 3]
pipe([1, 2, 3], Array.insertAllAtOrThrow(5, [8, 9])); // throws FnErrorfunction Array.insertAt(array: T[], index: number, value: U): T[]Inserts value at the specified index in array, returning a new array with the inserted element.
import { Array } from "@monstermann/array";
Array.insertAt([1, 2, 3], 1, 10); // [1, 10, 2, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.insertAt(1, 10)); // [1, 10, 2, 3]function Array.insertAtOr(array: T[], index: number, value: U, fallback: V): T[] | VInserts value at the specified index in array, returning a new array with the inserted element, or fallback if the index is out of bounds.
import { Array } from "@monstermann/array";
Array.insertAtOr([1, 2, 3], 10, 99, []); // []import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.insertAtOr(10, 99, [])); // []function Array.insertAtOrElse(
array: T[],
index: number,
value: U,
fallback: (array: T[]) => V
): T[] | VInserts value at the specified index in array, returning a new array with the inserted element, or the result of calling callback with the array if the index is out of bounds.
import { Array } from "@monstermann/array";
Array.insertAtOrElse([1, 2, 3], 10, 99, (arr) => arr.length); // 3import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.insertAtOrElse(10, 99, (arr) => arr.length),
); // 3function Array.insertAtOrThrow(array: T[], index: number, value: U): T[]Inserts value at the specified index in array, returning a new array with the inserted element, or throws an error if the index is out of bounds.
import { Array } from "@monstermann/array";
Array.insertAtOrThrow([1, 2, 3], 1, 10); // [1, 10, 2, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.insertAtOrThrow(1, 10)); // [1, 10, 2, 3]function Array.is(value: unknown): booleanReturns true if value is an array, otherwise returns false.
import { Array } from "@monstermann/array";
Array.is([1, 2, 3]); // trueimport { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.is()); // truefunction Array.isEmpty(array: T[]): booleanReturns true if array has no elements, otherwise returns false.
import { Array } from "@monstermann/array";
Array.isEmpty([]); // trueimport { Array } from "@monstermann/array";
pipe([], Array.isEmpty()); // truefunction Array.isShallowEqual(target: T[], source: T[]): booleanReturns true if target and source have the same length and their elements are equal using shallow comparison, otherwise returns false.
import { Array } from "@monstermann/array";
Array.isShallowEqual([1, 2, 3], [1, 2, 3]); // trueimport { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.isShallowEqual([1, 2, 3])); // truefunction Array.join(array: T[], separator?: string): stringJoins all elements of array into a string, separated by the specified separator.
import { Array } from "@monstermann/array";
Array.join([1, 2, 3], ", "); // "1, 2, 3"import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.join(", ")); // "1, 2, 3"function Array.last(array: T[]): T | undefinedReturns the last element of array, or undefined if the array is empty.
import { Array } from "@monstermann/array";
Array.last([1, 2, 3, 4]); // 4import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.last()); // 4function Array.lastIndexOf(array: T[], value: T): numberReturns the last index at which value can be found in array, or -1 if it is not present.
import { Array } from "@monstermann/array";
Array.lastIndexOf([1, 2, 3, 2, 4], 2); // 3import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2, 4], Array.lastIndexOf(2)); // 3function Array.lastIndexOfOr(array: T[], value: T, fallback: U): number | UReturns the index of the last occurrence of value in target. If value is not found, returns or.
import { Array } from "@monstermann/array";
Array.lastIndexOfOr([1, 2, 3, 2], 2, -1); // 3
Array.lastIndexOfOr([1, 2, 3], 4, -1); // -1import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.lastIndexOfOr(2, -1)); // 3
pipe([1, 2, 3], Array.lastIndexOfOr(4, -1)); // -1function Array.lastIndexOfOrElse(
array: T[],
value: T,
fallback: (array: T[]) => U
): number | UReturns the index of the last occurrence of value in target. If value is not found, calls orElse with the original array.
import { Array } from "@monstermann/array";
Array.lastIndexOfOrElse([1, 2, 3, 2], 2, () => -1); // 3
Array.lastIndexOfOrElse([1, 2, 3], 4, (arr) => arr.length); // 3import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 2],
Array.lastIndexOfOrElse(2, () => -1),
); // 3
pipe(
[1, 2, 3],
Array.lastIndexOfOrElse(4, (arr) => arr.length),
); // 3function Array.lastIndexOfOrThrow(array: T[], value: T): numberReturns the index of the last occurrence of value in target. If value is not found, throws an error.
import { Array } from "@monstermann/array";
Array.lastIndexOfOrThrow([1, 2, 3, 2], 2); // 3
Array.lastIndexOfOrThrow([1, 2, 3], 4); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.lastIndexOfOrThrow(2)); // 3
pipe([1, 2, 3], Array.lastIndexOfOrThrow(4)); // throws FnErrorfunction Array.lastOr(array: T[], fallback: U): T | UReturns the last element of array, or fallback if the array is empty.
import { Array } from "@monstermann/array";
Array.lastOr([1, 2, 3, 4], 0); // 4import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.lastOr(0)); // 4function Array.lastOrElse(array: T[], fallback: (array: T[]) => U): T | UReturns the last element of array, or the result of calling callback with the array if the array is empty.
import { Array } from "@monstermann/array";
Array.lastOrElse([1, 2, 3, 4], (arr) => arr.length); // 4import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.lastOrElse((arr) => arr.length),
); // 4function Array.lastOrThrow(array: T[]): TReturns the last element of array, or throws an error if the array is empty.
import { Array } from "@monstermann/array";
Array.lastOrThrow([1, 2, 3, 4]); // 4import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.lastOrThrow()); // 4function Array.length(array: T[]): numberReturns the number of elements in array.
import { Array } from "@monstermann/array";
Array.length([1, 2, 3, 4]); // 4import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.length()); // 4function Array.mapAt(
array: T[],
index: number,
mapper: (value: T, index: number, array: T[]) => U
): T[]Applies the mapper function to the element at the specified index in array, returning a new array with the mapped element.
import { Array } from "@monstermann/array";
Array.mapAt([1, 2, 3, 4], 1, (x) => x * 10); // [1, 20, 3, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.mapAt(1, (x) => x * 10),
); // [1, 20, 3, 4]function Array.mapAtOr(
array: T[],
index: number,
mapper: (value: T, index: number, array: T[]) => U,
fallback: V
): T[] | VApplies the mapper function to the element at the specified index in array, returning a new array with the mapped element, or fallback if the index is out of bounds.
import { Array } from "@monstermann/array";
Array.mapAtOr([1, 2, 3], 10, (x) => x * 10, []); // []import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.mapAtOr(10, (x) => x * 10, []),
); // []function Array.mapAtOrElse(
array: T[],
index: number,
mapper: (value: T, index: number, array: T[]) => U,
fallback: (array: T[]) => V
): T[] | VApplies the mapper function to the element at the specified index in array, returning a new array with the mapped element, or the result of calling callback with the array if the index is out of bounds.
import { Array } from "@monstermann/array";
Array.mapAtOrElse(
[1, 2, 3],
10,
(x) => x * 10,
(arr) => arr.length,
); // 3import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.mapAtOrElse(
10,
(x) => x * 10,
(arr) => arr.length,
),
); // 3function Array.mapAtOrThrow(
array: T[],
index: number,
mapper: (value: T, index: number, array: T[]) => U
): T[]Applies the mapper function to the element at the specified index in array, returning a new array with the mapped element, or throws an error if the index is out of bounds.
import { Array } from "@monstermann/array";
Array.mapAtOrThrow([1, 2, 3, 4], 1, (x) => x * 10); // [1, 20, 3, 4]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.mapAtOrThrow(1, (x) => x * 10),
); // [1, 20, 3, 4]function Array.mapEach(
array: T[],
mapper: (value: T, index: number, array: T[]) => U
): U[]Applies the mapper function to each element in array, returning a new array with the mapped elements.
import { Array } from "@monstermann/array";
Array.mapEach([1, 2, 3, 4], (x) => x * 2); // [2, 4, 6, 8]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.mapEach((x) => x * 2),
); // [2, 4, 6, 8]function Array.maxOr(array: number[], fallback: U): number | UReturns the maximum value in the number array, or fallback if the array is empty.
import { Array } from "@monstermann/array";
Array.maxOr([1, 3, 2, 5], 0); // 5import { Array } from "@monstermann/array";
pipe([1, 3, 2, 5], Array.maxOr(0)); // 5function Array.maxOrElse(
array: number[],
fallback: (array: number[]) => U
): number | UReturns the maximum value from array, or calls orElse if the array is empty.
import { Array } from "@monstermann/array";
Array.maxOrElse([1, 5, 3], () => 0); // 5
Array.maxOrElse([], () => 0); // 0import { Array } from "@monstermann/array";
pipe(
[1, 5, 3],
Array.maxOrElse(() => 0),
); // 5
pipe(
[],
Array.maxOrElse(() => 0),
); // 0function Array.maxOrThrow(array: number[]): numberReturns the maximum value from array, or throws an error if the array is empty.
import { Array } from "@monstermann/array";
Array.maxOrThrow([1, 5, 3]); // 5
Array.maxOrThrow([]); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 5, 3], Array.maxOrThrow()); // 5
pipe([], Array.maxOrThrow()); // throws FnErrorfunction Array.meanOr(array: number[], fallback: U): number | UReturns the mean (average) value of the number array, or fallback if the array is empty.
import { Array } from "@monstermann/array";
Array.meanOr([1, 2, 3, 4], 0); // 2.5import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.meanOr(0)); // 2.5function Array.meanOrElse(
array: number[],
fallback: (array: number[]) => U
): number | UReturns the mean (average) value from array, or calls orElse if the array is empty.
import { Array } from "@monstermann/array";
Array.meanOrElse([1, 2, 3], () => 0); // 2
Array.meanOrElse([], () => 0); // 0import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.meanOrElse(() => 0),
); // 2
pipe(
[],
Array.meanOrElse(() => 0),
); // 0function Array.meanOrThrow(array: number[]): numberReturns the mean (average) value from array, or throws an error if the array is empty.
import { Array } from "@monstermann/array";
Array.meanOrThrow([1, 2, 3]); // 2
Array.meanOrThrow([]); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.meanOrThrow()); // 2
pipe([], Array.meanOrThrow()); // throws FnErrorfunction Array.medianOr(array: number[], fallback: U): number | UReturns the median value of the number array, or fallback if the array is empty.
import { Array } from "@monstermann/array";
Array.medianOr([1, 2, 3, 4, 5], 0); // 3import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4, 5], Array.medianOr(0)); // 3function Array.medianOrElse(
array: number[],
fallback: (array: number[]) => U
): number | UReturns the median value from array, or calls orElse if the array is empty.
import { Array } from "@monstermann/array";
Array.medianOrElse([1, 3, 5], () => 0); // 3
Array.medianOrElse([1, 2, 3, 4], () => 0); // 2.5
Array.medianOrElse([], () => 0); // 0import { Array } from "@monstermann/array";
pipe(
[1, 3, 5],
Array.medianOrElse(() => 0),
); // 3
pipe(
[1, 2, 3, 4],
Array.medianOrElse(() => 0),
); // 2.5
pipe(
[],
Array.medianOrElse(() => 0),
); // 0function Array.medianOrThrow(array: number[]): numberReturns the median value from array, or throws an error if the array is empty.
import { Array } from "@monstermann/array";
Array.medianOrThrow([1, 3, 5]); // 3
Array.medianOrThrow([1, 2, 3, 4]); // 2.5
Array.medianOrThrow([]); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 3, 5], Array.medianOrThrow()); // 3
pipe([1, 2, 3, 4], Array.medianOrThrow()); // 2.5
pipe([], Array.medianOrThrow()); // throws FnErrorfunction Array.minOr(array: number[], fallback: U): number | UReturns the minimum value in the number array, or fallback if the array is empty.
import { Array } from "@monstermann/array";
Array.minOr([5, 1, 3, 2], 0); // 1import { Array } from "@monstermann/array";
pipe([5, 1, 3, 2], Array.minOr(0)); // 1function Array.minOrElse(
array: number[],
fallback: (array: number[]) => U
): number | UReturns the minimum value from target array, or calls orElse if the array is empty.
import { Array } from "@monstermann/array";
Array.minOrElse([5, 2, 8, 1], () => 0); // 1
Array.minOrElse([], () => 0); // 0import { Array } from "@monstermann/array";
pipe(
[5, 2, 8, 1],
Array.minOrElse(() => 0),
); // 1
pipe(
[],
Array.minOrElse(() => 0),
); // 0function Array.minOrThrow(array: number[]): numberReturns the minimum value from target array, or throws an error if the array is empty.
import { Array } from "@monstermann/array";
Array.minOrThrow([5, 2, 8, 1]); // 1
Array.minOrThrow([]); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([5, 2, 8, 1], Array.minOrThrow()); // 1
pipe([], Array.minOrThrow()); // throws FnErrorfunction Array.none(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): booleanReturns true if no elements in array satisfy the provided predicate function, otherwise returns false.
import { Array } from "@monstermann/array";
Array.none([1, 2, 3, 4], (x) => x > 10); // trueimport { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.none((x) => x > 10),
); // truefunction Array.partition(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): [T[], T[]]Splits array into two arrays based on the predicate function, returning a tuple where the first array contains elements that satisfy the predicate and the second contains those that don't.
import { Array } from "@monstermann/array";
Array.partition([1, 2, 3, 4, 5], (x) => x % 2 === 0); // [[2, 4], [1, 3, 5]]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4, 5],
Array.partition((x) => x % 2 === 0),
); // [[2, 4], [1, 3, 5]]function Array.prepend(array: T[], value: U): T[]Adds value to the beginning of array.
import { Array } from "@monstermann/array";
Array.prepend([2, 3, 4], 1); // [1, 2, 3, 4]import { Array } from "@monstermann/array";
pipe([2, 3, 4], Array.prepend(1)); // [1, 2, 3, 4]function Array.random(array: T[]): T | undefinedReturns a random element from array, or undefined if the array is empty.
import { Array } from "@monstermann/array";
Array.random([1, 2, 3, 4]); // 2 (random)import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.random()); // 2 (random)function Array.randomOr(array: T[], fallback: U): T | UReturns a random element from array, or fallback if the array is empty.
import { Array } from "@monstermann/array";
Array.randomOr([1, 2, 3, 4], 0); // 2 (random)import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.randomOr(0)); // 2 (random)function Array.randomOrElse(array: T[], fallback: (array: T[]) => U): T | UReturns a random element from array, or the result of calling callback with the array if the array is empty.
import { Array } from "@monstermann/array";
Array.randomOrElse([1, 2, 3, 4], (arr) => arr.length); // 2 (random)import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.randomOrElse((arr) => arr.length),
); // 2 (random)function Array.reduce(
array: T[],
reducer: (acc: U, value: T, index: number, array: T[]) => U,
initial: U
): UReduces array to a single value by executing the reducer function on each element, starting with the initial accumulator value.
import { Array } from "@monstermann/array";
Array.reduce([1, 2, 3, 4], 0, (acc, x) => acc + x); // 10import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.reduce(0, (acc, x) => acc + x),
); // 10function Array.reject(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): T[]Returns a new array with elements from array that do not satisfy the provided predicate function.
import { Array } from "@monstermann/array";
Array.reject([1, 2, 3, 4, 5], (x) => x % 2 === 0); // [1, 3, 5]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4, 5],
Array.reject((x) => x % 2 === 0),
); // [1, 3, 5]function Array.remove(array: T[], value: U): T[]Removes the first occurrence of value from target array. If the value is not found, returns the original array unchanged.
import { Array } from "@monstermann/array";
Array.remove([1, 2, 3, 2], 2); // [1, 3, 2]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.remove(2)); // [1, 3, 2]function Array.removeAll(array: T[], values: Iterable<T>): T[]Removes all occurrences of each value in values from target array. If no values are found, returns the original array unchanged.
import { Array } from "@monstermann/array";
Array.removeAll([1, 2, 3, 2, 4], [2, 4]); // [1, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2, 4], Array.removeAll([2, 4])); // [1, 3]function Array.removeAt(array: T[], index: number): T[]Removes the element at index idx from target array. Supports negative indices to count from the end. If the index is out of bounds, returns the original array unchanged.
import { Array } from "@monstermann/array";
Array.removeAt([1, 2, 3, 4], 1); // [1, 3, 4]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.removeAt(1)); // [1, 3, 4]function Array.removeAtOr(array: T[], index: number, fallback: U): T[] | URemoves the element at index idx from target array. Supports negative indices to count from the end. If the index is out of bounds, returns the fallback value or.
import { Array } from "@monstermann/array";
Array.removeAtOr([1, 2, 3], 1, []); // [1, 3]
Array.removeAtOr([1, 2, 3], 5, []); // []import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.removeAtOr(1, [])); // [1, 3]
pipe([1, 2, 3], Array.removeAtOr(5, [])); // []function Array.removeAtOrElse(
array: T[],
index: number,
fallback: (array: T[]) => U
): T[] | URemoves the element at index idx from target array. Supports negative indices to count from the end. If the index is out of bounds, calls the orElse function with the original array and returns its result.
import { Array } from "@monstermann/array";
Array.removeAtOrElse([1, 2, 3], 1, () => []); // [1, 3]
Array.removeAtOrElse([1, 2, 3], 5, (arr) => arr); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.removeAtOrElse(1, () => []),
); // [1, 3]
pipe(
[1, 2, 3],
Array.removeAtOrElse(5, (arr) => arr),
); // [1, 2, 3]function Array.removeAtOrThrow(array: T[], index: number): T[]Removes the element at index idx from target array. Supports negative indices to count from the end. If the index is out of bounds, throws an error.
import { Array } from "@monstermann/array";
Array.removeAtOrThrow([1, 2, 3], 1); // [1, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.removeAtOrThrow(1)); // [1, 3]function Array.removeLast(array: T[], value: U): T[]Removes the last occurrence of value from target array. If the value is not found, returns the original array unchanged.
import { Array } from "@monstermann/array";
Array.removeLast([1, 2, 3, 2], 2); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.removeLast(2)); // [1, 2, 3]function Array.removeLastOr(array: T[], value: U, fallback: V): T[] | VRemoves the last occurrence of value from target array. If the value is not found, returns the fallback value or.
import { Array } from "@monstermann/array";
Array.removeLastOr([1, 2, 3, 2], 2, []); // [1, 2, 3]
Array.removeLastOr([1, 2, 3], 4, []); // []import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.removeLastOr(2, [])); // [1, 2, 3]
pipe([1, 2, 3], Array.removeLastOr(4, [])); // []function Array.removeLastOrElse(
array: T[],
value: U,
fallback: (array: T[]) => V
): T[] | VRemoves the last occurrence of value from target array. If the value is not found, calls the orElse function with the original array and returns its result.
import { Array } from "@monstermann/array";
Array.removeLastOrElse([1, 2, 3, 2], 2, () => []); // [1, 2, 3]
Array.removeLastOrElse([1, 2, 3], 4, (arr) => arr); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 2],
Array.removeLastOrElse(2, () => []),
); // [1, 2, 3]
pipe(
[1, 2, 3],
Array.removeLastOrElse(4, (arr) => arr),
); // [1, 2, 3]function Array.removeLastOrThrow(array: T[], value: U): T[]Removes the last occurrence of value from target array. If the value is not found, throws an error.
import { Array } from "@monstermann/array";
Array.removeLastOrThrow([1, 2, 3, 2], 2); // [1, 2, 3]
Array.removeLastOrThrow([1, 2, 3], 4); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.removeLastOrThrow(2)); // [1, 2, 3]
pipe([1, 2, 3], Array.removeLastOrThrow(4)); // throws FnErrorfunction Array.removeOrElse(
array: T[],
value: U,
fallback: (array: T[]) => V
): T[] | VRemoves the first occurrence of value from target array. If the value is not found, calls the orElse function with the original array and returns its result.
import { Array } from "@monstermann/array";
Array.removeOrElse([1, 2, 3, 2], 2, () => []); // [1, 3, 2]
Array.removeOrElse([1, 2, 3], 4, (arr) => arr); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 2],
Array.removeOrElse(2, () => []),
); // [1, 3, 2]
pipe(
[1, 2, 3],
Array.removeOrElse(4, (arr) => arr),
); // [1, 2, 3]function Array.removeOrThrow(array: T[], value: U): T[]Removes the first occurrence of value from target array. If the value is not found, throws an error.
import { Array } from "@monstermann/array";
Array.removeOrThrow([1, 2, 3, 2], 2); // [1, 3, 2]
Array.removeOrThrow([1, 2, 3], 4); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.removeOrThrow(2)); // [1, 3, 2]
pipe([1, 2, 3], Array.removeOrThrow(4)); // throws FnErrorfunction Array.replace(array: T[], oldValue: U, newValue: V): T[]Replaces the first occurrence of value with replacement in target array. If the value is not found or if value and replacement are the same, returns the original array unchanged.
import { Array } from "@monstermann/array";
Array.replace([1, 2, 3, 2], 2, 5); // [1, 5, 3, 2]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.replace(2, 5)); // [1, 5, 3, 2]function Array.replaceLast(array: T[], oldValue: U, newValue: V): T[]Replaces the last occurrence of value with replacement in target array. If the value is not found or if value and replacement are the same, returns the original array unchanged.
import { Array } from "@monstermann/array";
Array.replaceLast([1, 2, 3, 2], 2, 5); // [1, 2, 3, 5]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.replaceLast(2, 5)); // [1, 2, 3, 5]function Array.replaceLastOr(
array: T[],
oldValue: U,
newValue: V,
fallback: W
): T[] | WReplaces the last occurrence of value with replacement in target array. If the value is not found, returns the fallback value or. If value and replacement are the same, returns the original array unchanged.
import { Array } from "@monstermann/array";
Array.replaceLastOr([1, 2, 3, 2], 2, 5, []); // [1, 2, 3, 5]
Array.replaceLastOr([1, 2, 3], 4, 5, []); // []import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.replaceLastOr(2, 5, [])); // [1, 2, 3, 5]
pipe([1, 2, 3], Array.replaceLastOr(4, 5, [])); // []function Array.replaceLastOrElse(
array: T[],
oldValue: U,
newValue: V,
fallback: (array: T[]) => W
): T[] | WReplaces the last occurrence of value in target with replacement. If value is not found, calls orElse with the original array.
import { Array } from "@monstermann/array";
Array.replaceLastOrElse([1, 2, 3, 2], 2, 9, () => []); // [1, 2, 3, 9]
Array.replaceLastOrElse([1, 2, 3], 4, 9, (arr) => arr); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 2],
Array.replaceLastOrElse(2, 9, () => []),
); // [1, 2, 3, 9]
pipe(
[1, 2, 3],
Array.replaceLastOrElse(4, 9, (arr) => arr),
); // [1, 2, 3]function Array.replaceLastOrThrow(array: T[], oldValue: U, newValue: V): T[]Replaces the last occurrence of value in target with replacement. If value is not found, throws an error.
import { Array } from "@monstermann/array";
Array.replaceLastOrThrow([1, 2, 3, 2], 2, 9); // [1, 2, 3, 9]
Array.replaceLastOrThrow([1, 2, 3], 4, 9); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.replaceLastOrThrow(2, 9)); // [1, 2, 3, 9]
pipe([1, 2, 3], Array.replaceLastOrThrow(4, 9)); // throws FnErrorfunction Array.replaceOr(
array: T[],
oldValue: U,
newValue: V,
fallback: W
): T[] | WReplaces the first occurrence of value in target with replacement. If value is not found, returns or.
import { Array } from "@monstermann/array";
Array.replaceOr([1, 2, 3, 2], 2, 9, []); // [1, 9, 3, 2]
Array.replaceOr([1, 2, 3], 4, 9, []); // []import { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.replaceOr(2, 9, [])); // [1, 9, 3, 2]
pipe([1, 2, 3], Array.replaceOr(4, 9, [])); // []function Array.replaceOrElse(
array: T[],
oldValue: U,
newValue: V,
fallback: (array: T[]) => W
): T[] | WReplaces the first occurrence of value in target with replacement. If value is not found, calls orElse with the original array.
import { Array } from "@monstermann/array";
Array.replaceOrElse([1, 2, 3, 2], 2, 9, () => []); // [1, 9, 3, 2]
Array.replaceOrElse([1, 2, 3], 4, 9, (arr) => arr); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 2],
Array.replaceOrElse(2, 9, () => []),
); // [1, 9, 3, 2]
pipe(
[1, 2, 3],
Array.replaceOrElse(4, 9, (arr) => arr),
); // [1, 2, 3]function Array.replaceOrThrow(array: T[], oldValue: U, newValue: V): T[]Replaces the first occurrence of value in target with replacement. If value is not found, throws an error.
import { Array } from "@monstermann/array";
Array.replaceOrThrow([1, 2, 3, 2], 2, 9); // [1, 9, 3, 2]
Array.replaceOrThrow([1, 2, 3], 4, 9); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3, 2], Array.replaceOrThrow(2, 9)); // [1, 9, 3, 2]
pipe([1, 2, 3], Array.replaceOrThrow(4, 9)); // throws FnErrorfunction Array.setAt(array: T[], index: number, value: U): T[]Sets the value at the specified idx in target to value. Returns the original array if the index is out of bounds or the value is already the same.
import { Array } from "@monstermann/array";
Array.setAt([1, 2, 3], 1, 9); // [1, 9, 3]
Array.setAt([1, 2, 3], -1, 9); // [1, 2, 9]
Array.setAt([1, 2, 3], 5, 9); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.setAt(1, 9)); // [1, 9, 3]
pipe([1, 2, 3], Array.setAt(-1, 9)); // [1, 2, 9]
pipe([1, 2, 3], Array.setAt(5, 9)); // [1, 2, 3]function Array.setAtOr(array: T[], index: number, value: U, fallback: V): T[] | VSets the value at the specified idx in target to value. If the index is out of bounds, returns or.
import { Array } from "@monstermann/array";
Array.setAtOr([1, 2, 3], 1, 9, []); // [1, 9, 3]
Array.setAtOr([1, 2, 3], -1, 9, []); // [1, 2, 9]
Array.setAtOr([1, 2, 3], 5, 9, []); // []import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.setAtOr(1, 9, [])); // [1, 9, 3]
pipe([1, 2, 3], Array.setAtOr(-1, 9, [])); // [1, 2, 9]
pipe([1, 2, 3], Array.setAtOr(5, 9, [])); // []function Array.setAtOrElse(
array: T[],
index: number,
value: U,
fallback: (array: T[]) => V
): T[] | VSets the value at the specified idx in target to value. If the index is out of bounds, calls orElse with the original array.
import { Array } from "@monstermann/array";
Array.setAtOrElse([1, 2, 3], 1, 9, () => []); // [1, 9, 3]
Array.setAtOrElse([1, 2, 3], -1, 9, () => []); // [1, 2, 9]
Array.setAtOrElse([1, 2, 3], 5, 9, (arr) => arr); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe(
[1, 2, 3],
Array.setAtOrElse(1, 9, () => []),
); // [1, 9, 3]
pipe(
[1, 2, 3],
Array.setAtOrElse(-1, 9, () => []),
); // [1, 2, 9]
pipe(
[1, 2, 3],
Array.setAtOrElse(5, 9, (arr) => arr),
); // [1, 2, 3]function Array.setAtOrThrow(
array: T[],
index: number,
value: U
): T[]Sets the value at the specified idx in target to value. If the index is out of bounds, throws an error.
import { Array } from "@monstermann/array";
Array.setAtOrThrow([1, 2, 3], 1, 9); // [1, 9, 3]
Array.setAtOrThrow([1, 2, 3], -1, 9); // [1, 2, 9]
Array.setAtOrThrow([1, 2, 3], 5, 9); // throws FnErrorimport { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.setAtOrThrow(1, 9)); // [1, 9, 3]
pipe([1, 2, 3], Array.setAtOrThrow(-1, 9)); // [1, 2, 9]
pipe([1, 2, 3], Array.setAtOrThrow(5, 9)); // throws FnErrorfunction Array.shuffle(array: T[]): T[]Returns a new array with the elements of array randomly shuffled.
import { Array } from "@monstermann/array";
Array.shuffle([1, 2, 3, 4]); // [3, 1, 4, 2] (random)import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.shuffle()); // [3, 1, 4, 2] (random)function Array.slice(array: T[], start?: number, end?: number): T[]Extracts a section of target array from start index to end index (exclusive). If end is not provided, extracts to the end of the array.
import { Array } from "@monstermann/array";
Array.slice([1, 2, 3, 4, 5], 1, 4); // [2, 3, 4]
Array.slice([1, 2, 3, 4, 5], 2); // [3, 4, 5]
Array.slice([1, 2, 3, 4, 5], -2); // [4, 5]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4, 5], Array.slice(1, 4)); // [2, 3, 4]
pipe([1, 2, 3, 4, 5], Array.slice(2)); // [3, 4, 5]
pipe([1, 2, 3, 4, 5], Array.slice(-2)); // [4, 5]function Array.some(
array: T[],
predicate: (value: T, index: number, array: T[]) => boolean
): booleanReturns true if at least one element in array satisfies the provided predicate function, otherwise returns false.
import { Array } from "@monstermann/array";
Array.some([1, 2, 3, 4], (x) => x > 3); // trueimport { Array } from "@monstermann/array";
pipe(
[1, 2, 3, 4],
Array.some((x) => x > 3),
); // truefunction Array.sort(array: T[], compareFn?: (a: T, b: T) => number): T[]Returns a new array with the elements of target sorted using the provided comparator function.
import { Array } from "@monstermann/array";
Array.sort([3, 1, 4, 2], (a, b) => a - b); // [1, 2, 3, 4]
Array.sort(["c", "a", "b"], (a, b) => a.localeCompare(b)); // ['a', 'b', 'c']import { Array } from "@monstermann/array";
pipe(
[3, 1, 4, 2],
Array.sort((a, b) => a - b),
); // [1, 2, 3, 4]
pipe(
["c", "a", "b"],
Array.sort((a, b) => a.localeCompare(b)),
); // ['a', 'b', 'c']function Array.sum(array: number[]): numberReturns the sum of all numbers in array.
import { Array } from "@monstermann/array";
Array.sum([1, 2, 3, 4]); // 10import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4], Array.sum()); // 10function Array.take(array: T[], count: number): T[]Returns a new array containing the first amount elements from array.
import { Array } from "@monstermann/array";
Array.take([1, 2, 3, 4, 5], 3); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4, 5], Array.take(3)); // [1, 2, 3]function Array.takeLast(array: T[], count: number): T[]Returns a new array containing the last amount elements from array.
import { Array } from "@monstermann/array";
Array.takeLast([1, 2, 3, 4, 5], 3); // [3, 4, 5]import { Array } from "@monstermann/array";
pipe([1, 2, 3, 4, 5], Array.takeLast(3)); // [3, 4, 5]function Array.union(target: T[], source: T[]): T[]Returns a new array containing all unique elements from both target and source. Elements from source that are not already in target are added to the result.
import { Array } from "@monstermann/array";
Array.union([1, 2, 3], [3, 4, 5]); // [1, 2, 3, 4, 5]import { Array } from "@monstermann/array";
pipe([1, 2, 3], Array.union([3, 4, 5])); // [1, 2, 3, 4, 5]function Array.unique(array: T[]): T[]Returns a new array with only the unique elements from target, preserving the order of first occurrence.
import { Array } from "@monstermann/array";
Array.unique([1, 2, 2, 3, 1]); // [1, 2, 3]import { Array } from "@monstermann/array";
pipe([1, 2, 2, 3, 1], Array.unique()); // [1, 2, 3]