https://github.com/vladkens/array-utils-ts
🔢🔧 A set of functions for working with arrays, often necessary but absent in lodash.
https://github.com/vladkens/array-utils-ts
array array-utils cjs esm lodash npm-package typescript utils
Last synced: 10 months ago
JSON representation
🔢🔧 A set of functions for working with arrays, often necessary but absent in lodash.
- Host: GitHub
- URL: https://github.com/vladkens/array-utils-ts
- Owner: vladkens
- License: mit
- Created: 2023-05-16T09:48:29.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-15T22:48:49.000Z (12 months ago)
- Last Synced: 2025-04-21T09:05:38.786Z (10 months ago)
- Topics: array, array-utils, cjs, esm, lodash, npm-package, typescript, utils
- Language: TypeScript
- Homepage: https://npm.im/array-utils-ts
- Size: 66.4 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
- Codeowners: .github/codeowners
Awesome Lists containing this project
README
# array-utils-ts
[
](https://npmjs.org/package/array-utils-ts)
[
](https://packagephobia.now.sh/result?p=array-utils-ts)
[
](https://npmjs.org/package/array-utils-ts)
[
](https://github.com/vladkens/array-utils-ts/blob/main/LICENSE)
[
](https://buymeacoffee.com/vladkens)
A set of functions for working with arrays, often necessary for working with state, but absent in lodash.
## Install
```sh
npm i array-utils-ts
```
```sh
yarn add array-utils-ts
```
## Usage
### filterNullable
```typescript
import { filterNullable } from "array-utils-ts"
filterNullable([1, null, 2, undefined])
// -> [1, 2]
```
### filterEmpty
```typescript
import { filterEmpty } from "array-utils-ts"
filterEmpty([1, null, 2, undefined, 3, "", "a"])
// -> [1, 2, 3, "a"]
```
### isUniq
```typescript
import { isUniq } from "array-utils-ts"
isUniq([1, 2, 3])
// -> true
isUniq([1, 2, 1])
// -> false
```
### hasEmpty
```typescript
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 `` component
```typescript
import { toggleItem } from "array-utils-ts"
toggleItem([1, 2, 3], 4)
// -> [1, 2, 3, 4]
toggleItem([1, 2, 3], 3)
// -> [1, 2]
```
### updateByKey
```typescript
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
```typescript
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
```typescript
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.
```typescript
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.
```typescript
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
```typescript
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"]]
```