array-utils-ts
A set of functions for working with arrays, often necessary for working with state, but absent in lodash.
Install
npm i array-utils-ts
yarn add array-utils-ts
Usage
filterNullable
import { filterNullable } from "array-utils-ts"
filterNullable([1, null, 2, undefined])
filterEmpty
import { filterEmpty } from "array-utils-ts"
filterEmpty([1, null, 2, undefined, 3, "", "a"])
isUniq
import { isUniq } from "array-utils-ts"
isUniq([1, 2, 3])
isUniq([1, 2, 1])
hasEmpty
import { hasEmpty } from "array-utils-ts"
hasEmpty(["a", "b", "c"])
hasEmpty(["a", "", "c"])
hasEmpty(["a", undefined, "c"])
toggleItem
For example useful in <select>
component
import { toggleItem } from "array-utils-ts"
toggleItem([1, 2, 3], 4)
toggleItem([1, 2, 3], 3)
updateByKey
import { updateByKey } from "array-utils-ts"
const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }]
const arr2 = updateByKey(arr1, "id", { id: 1, v: 2 })
const arr3 = updateByKey(arr2, "id", { id: 3, v: 1 })
upsertByKey
import { upsertByKey } from "array-utils-ts"
const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }]
const arr2 = upsertByKey(arr1, "id", { id: 1, v: 2 })
const arr3 = upsertByKey(arr2, "id", { id: 3, v: 1 })
toggleByKey
import { toggleByKey } from "array-utils-ts"
const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }]
const arr2 = toggleByKey(arr1, "id", { id: 1, v: 2 })
const arr3 = toggleByKey(arr2, "id", { id: 3, v: 1 })
isFirstByKey
Check if given object is first in collection by some key.
import { isFirstByKey } from "array-utils-ts"
const arr = [{ id: 1 }, { id: 2 }, { id: 3 }]
isFirstByKey(arr, "id", { id: 1 })
isFirstByKey(arr, "id", { id: 2 })
isFirstByKey(arr, "id", { id: 3 })
isLastByKey
Check if given object is last in collection by some key.
import { isLastByKey } from "array-utils-ts"
const arr = [{ id: 1 }, { id: 2 }, { id: 3 }]
isLastByKey(arr, "id", { id: 1 })
isLastByKey(arr, "id", { id: 2 })
isLastByKey(arr, "id", { id: 3 })
enumerate
import { enumerate } from "array-utils-ts"
const arr = ["a", "b", "c"]
enumerate(arr)
enumerate(arr, 1)