Giter Club home page Giter Club logo

object-path-immutable's Introduction

object-path-immutable

Tiny JS library to modify deep object properties without modifying the original object (immutability). Works great with React (especially when using setState()) and Redux (inside a reducer).

This can be seen as a simpler and more intuitive alternative to the React Immutability Helpers and Immutable.js

NPM version Build Status Coverage Status devDependency Status Downloads

Install

Node.js

npm install object-path-immutable --save

Quick usage

The following, sets a property without modifying the original object. It will minimize the number of clones down the line. The resulting object is just a plain JS object literal, so be warned that it will not be protected against property mutations (like Immutable.js)

var obj = {
  a: {
    b: 'c',
    c: ['d', 'f']
  }
}

//set a deep property
var newObj = immutable.set(obj, 'a.b', 'f')
//returns
//var obj = {
//  a: {
//    b: 'f',
//    c: ['d', 'f']
//  }
//}

//obj !== newObj
//obj.a !== newObj.a
//obj.b !== newObj.b

//However:
//obj.c === newObj.c

API

var obj = {
  a: {
    b: 'c',
    c: ['d', 'f']
  }
}

var immutable = require("object-path-immutable")

//set deep property
var newObj = immutable.set(obj, 'a.b', 'f')
//returns
//var obj = {
//  a: {
//    b: 'f',
//    c: ['d', 'f']
//  }
//}

//it can also use an array to describe the path
var newObj = immutable.set(obj, ['a', 'b'], 'f')

//if the path is specified as a string, then numbers are automatically interpreted as array indexes
var newObj = immutable.set(obj, 'a.c.1', 'fooo')
//returns
//var obj = {
//  a: {
//    b: 'f',
//    c: ['d', 'fooo']
//  }
//}


//push into a deep array (it will create intermediate objects/arrays if necessary)
var newObj = immutable.push(obj, 'a.d', 'f')
//returns
//var obj = {
//  a: {
//    b: 'f',
//    c: ['d', 'f'],
//    d: ['f']
//  }
//}

//delete a deep property
var newObj = immutable.del(obj, 'a.c')
//returns
//var obj = {
//  a: {
//    b: 'f'
//  }
//}

//delete a deep array item (splice)
var newObj = immutable.del(obj, 'a.c.0')
//var obj = {
//  a: {
//    b: 'f',
//    c: ['f']
//  }
//}

//shallow copy properties
var newObj = immutable.assign(obj, 'a', { b: 'f', g: 'h' })
//returns
//var obj = {
//  a: {
//    b: 'f',
//    c: ['d, 'f'],
//    g: 'h'
//  }
//}

//Chaining mode. value() at the end of the chain is used to retrieve the resulting object
var newObj = immutable(obj).set('a.b', 'f').del('a.c.0').value()

Equivalent library with side effects

object-path

Credits

object-path-immutable's People

Contributors

davidworkman9 avatar lawnsea avatar mariocasciaro avatar simplesmiler avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.