New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

array-utils-ts

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-utils-ts

Lightweight TypeScript utility library providing essential array manipulation functions for state management.

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

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"]]

Keywords

FAQs

Package last updated on 09 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc