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

@blazingedge/update

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blazingedge/update

Utility for immutable deep updates of objects.

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by175%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status

update

Yet another utility for immutable object updates.

Installation

npm install @blazingedge/update --save

Usage

import update from '@blazingedge/update'

const newState = update(state, 'path.to.users[7].data', {
  email: 'some.email@example.com',
  balance: {
    amount: n => n + 100
  }
})

More examples in the article.

API

update(data, [path], change)

Arguments
  • data (any): The data to update.
  • [path] (Array | string): The path of the property to update.
  • change (any): The change to apply.
Returns

Updated data. When no effective changes are made, returns the same data.

REMOVE

Special value to use in a change to remove part(s) of data.

import update, { REMOVE } from '@blazingedge/update'

// Remove entire player
update(state, 'path.to.playersById[7]', REMOVE)

// Removing and setting
update(state, 'path.to.playersById', {
  [killedPlayerId]: REMOVE,
  [killedBy]: {
    kills: n => n + 1,
  }
})

Path with "*"

To apply a change to all values of an array/object, we can use "*".

update(state, 'path.to.users[*].data.balance', n => n + 100)

update(state, 'path.to.users[*]', (user, index) => {
  // Removing every second user
  if (index % 2 === 0) {
    return REMOVE
  }
  // Mark others as lucky and double the balance amount
  return update(user, {
    lucky: true,
    balance: {
      amount: n => n * 2
    }
  })
})

Path as Array

When path is passed as an array, it can also contain "filters" to selectively change values of an array/object.

update(state, ['path', 'to', 'users', { lucky: true }, 'balance'], {
  limit: REMOVE,
  amount: n => n + 1000
})

A filter can be a:

  • function: user => user.lucky,
  • plain object with required values: { lucky: true } (use {} for "all"),
  • array of indexes/keys: [2, 4, 6, 8]

Keywords

FAQs

Package last updated on 26 Oct 2017

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