npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

setimmutable

v0.1.9

Published

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

Downloads

2,130

Readme

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
          }
        })
      }
    }
    /* ... */
  }
}