Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

immutability-helper

Package Overview
Dependencies
Maintainers
2
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

immutability-helper

mutate a copy of data without changing the original source

  • 2.9.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
520K
decreased by-8.22%
Maintainers
2
Weekly downloads
 
Created

What is immutability-helper?

The immutability-helper npm package provides a set of utilities to perform immutable operations on JavaScript objects and arrays. It allows you to update nested data structures without mutating the original data, which is particularly useful in state management for applications like React.

What are immutability-helper's main functionalities?

Updating a value

This feature allows you to update a specific value in an object. In the example, the value of 'a' is updated from 1 to 3.

const update = require('immutability-helper');
const state = {a: 1, b: 2};
const newState = update(state, {a: {$set: 3}});
console.log(newState); // {a: 3, b: 2}

Pushing an element to an array

This feature allows you to push a new element to an array. In the example, the number 4 is pushed to the end of the array.

const update = require('immutability-helper');
const state = [1, 2, 3];
const newState = update(state, {$push: [4]});
console.log(newState); // [1, 2, 3, 4]

Splicing an array

This feature allows you to splice an array. In the example, the element at index 1 is replaced with the number 4.

const update = require('immutability-helper');
const state = [1, 2, 3];
const newState = update(state, {$splice: [[1, 1, 4]]});
console.log(newState); // [1, 4, 3]

Merging objects

This feature allows you to merge objects. In the example, a new key 'c' with value 3 is added to the object under the key 'b'.

const update = require('immutability-helper');
const state = {a: 1, b: 2};
const newState = update(state, {b: {$merge: {c: 3}}});
console.log(newState); // {a: 1, b: {c: 3}}

Applying a function

This feature allows you to apply a function to a value. In the example, the value of 'a' is doubled.

const update = require('immutability-helper');
const state = {a: 1, b: 2};
const newState = update(state, {a: {$apply: (x) => x * 2}});
console.log(newState); // {a: 2, b: 2}

Other packages similar to immutability-helper

Keywords

FAQs

Package last updated on 24 Jan 2019

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