Skip to content

πŸ”’πŸ”§ A set of functions for working with arrays, often necessary but absent in lodash.

License

Notifications You must be signed in to change notification settings

vladkens/array-utils-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

array-utils-ts

version size downloads license donate

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]) // -> [1, 2]

filterEmpty

import { filterEmpty } from "array-utils-ts" filterEmpty([1, null, 2, undefined, 3, "", "a"]) // -> [1, 2, 3, "a"]

isUniq

import { isUniq } from "array-utils-ts" isUniq([1, 2, 3]) // -> true isUniq([1, 2, 1]) // -> false

hasEmpty

import { hasEmpty } from "array-utils-ts" hasEmpty(["a", "b", "c"]) // -> false hasEmpty(["a", "", "c"]) // -> true hasEmpty(["a", undefined, "c"]) // -> true

toggleItem

For example useful in <select> component

import { toggleItem } from "array-utils-ts" toggleItem([1, 2, 3], 4) // -> [1, 2, 3, 4] toggleItem([1, 2, 3], 3) // -> [1, 2]

updateByKey

import { updateByKey } from "array-utils-ts" // prettier-ignore const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }] const arr2 = updateByKey(arr1, "id", { id: 1, v: 2 }) // -> [{ id: 1, v: 2 }, { id: 2, v: 1 }] // arr1 !== arr2 const arr3 = updateByKey(arr2, "id", { id: 3, v: 1 }) // -> [{ id: 1, v: 2 }, { id: 2, v: 1 }] // arr2 === arr3 // note: item not found, nothing changed

upsertByKey

import { upsertByKey } from "array-utils-ts" // prettier-ignore const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }] const arr2 = upsertByKey(arr1, "id", { id: 1, v: 2 }) // -> [{ id: 1, v: 2 }, { id: 2, v: 1 }] // arr1 !== arr2 const arr3 = upsertByKey(arr2, "id", { id: 3, v: 1 }) // -> [{ id: 1, v: 2 }, { id: 2, v: 1 }, { id: 3, v: 1 }] // arr2 !== arr3

toggleByKey

import { toggleByKey } from "array-utils-ts" // prettier-ignore const arr1 = [{ id: 1, v: 1 }, { id: 2, v: 1 }] const arr2 = toggleByKey(arr1, "id", { id: 1, v: 2 }) // -> [{ id: 2, v: 1 }]; arr1 !== arr2 const arr3 = toggleByKey(arr2, "id", { id: 3, v: 1 }) // -> [{ id: 2, v: 1 }, { id: 3, v: 1 }]; arr2 !== arr3

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 }) // -> true isFirstByKey(arr, "id", { id: 2 }) // -> false isFirstByKey(arr, "id", { id: 3 }) // -> false

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 }) // -> false isLastByKey(arr, "id", { id: 2 }) // -> false isLastByKey(arr, "id", { id: 3 }) // -> true

enumerate

import { enumerate } from "array-utils-ts" const arr = ["a", "b", "c"] enumerate(arr) // -> [[0, "a"], [1, "b"], [2, "c"]] enumerate(arr, 1) // -> [[1, "a"], [2, "b"], [3, "c"]]

About

πŸ”’πŸ”§ A set of functions for working with arrays, often necessary but absent in lodash.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published