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

setimmutable

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

setimmutable

An alternative to lodash.set when your object necessary working with immutable objects.

  • 0.1.9
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
322
Maintainers
1
Weekly downloads
 
Created
Source

SetImmutable Build Status

An alternative to lodash.set when your object necessary working with immutable objects.

Installation

Using npm:

npm install --save setimmutable

In Node.js:

const set = require('setimmutable');

Mutable Vs. Immutable

In a simple object when do you use _.set the data is updated if it is frozen nothing happens. The SetImmutable update the object tree until the final element to be replaced.

// const setLodash = require('lodash.set')
// const setImmutable = require('setimmutable')

// With mutable object
const nextObjMutable = setLodash(originalObj, path, 3) // Update the element and return the original object.

nextObjMutable === originalObj // true

// With immutable object
const nextObjImmutable = setImmutable(originalObj, path, 3) // Update the tree element and return a new object.

nextObjImmutable === originalObj // false

SetImmutable with complex constructors

To update the object tree is used the reference constructor. This makes a new object and assigns all old properties to the new object. But there are times when the constructor is complex and requires special properties to be declared.

// Simple Constructor
class SimpleConstructor {
    constructor() { /* ... */ }
}

// Complex Constructor
class ComplexConstructor {
    constructor(requiredArg, especialArg) { /* ... */ }
}

SetImmutable load the custom Clone to make a new object.

Example:

// const clone = require('setimmutable/clone')
function customClone (objValue, srcValue) {
    switch (objValue.constructor) {
        // My custom class
        case MyClass: return MyClass.parse(objValue) // Return new object instance of MyClass
        // My second custom class
        case MySecondClass: return new MySecondClass(...myArgs) // Return new object instance of MySecondClass
        // Set default clone
        default: return clone(objValue)
    }
}

setImmutable(originalObject, path, newValue, customClone)

API

set(object, path, value, [customClone])

Sets the value at path of object. If a portion of path doesn't exist, it's created.

Note: This not method mutates object. It re-create the object defined on the path.

Arguments

  • object (Object): The object to modify.
  • path (Array|string): The path of the property to set.
  • value (*): The value to set.
  • [customClone] (Function): The function to customize clone object.

Returns

  • (Object): Return object.

Example 1 (on RunKit)

const object = {}

set(object, '[0][1][2]', 'a')
// => { '0': { '1': {'2': 'a' } } }

Example 2 (on RunKit)

const object = []

function customClone (objValue, srcValue) {
    switch (objValue.constructor) {
        case Person: return Person.clone(objValue)
        /* ... */
        /* default: return require('setimmutable/clone')(objValue) */
    }
}

set(object, '[0].people.[1].firstName', 'Lucky', customClone)
// => [ { 'people': [..., Person { 'firstName': 'Lucky' } ] } ]

SetImmutable with Redux

With SetImmutable:

const set = require('setimmutable')

function Reducer (state = initialState, action) {
    switch (action.type) {
        case 'UPDATE_PERSON': {
            return set(state, ['people', action.id, 'firstName'], action.firstName)
        }
        /* ... */
    }
}

Without SetImmutable:

function Reducer (state = initialState, action) {
  switch (action.type) {
    case 'UPDATE_PERSON': {
      return {
        ...state,
        people: state.people.map((person, index) => {
          if (person.id === action.id) {
            return {
              ...state.people[index],
              firstName: action.firstName
            }
          } else {
            return person
          }
        })
      }
    }
    /* ... */
  }
}

Keywords

FAQs

Package last updated on 22 Feb 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