- Notifications
You must be signed in to change notification settings - Fork47
Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.
License
mariocasciaro/object-path-immutable
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Tiny JS library to modify deep object properties without modifying the original object (immutability).Works great with React (especially when usingsetState()) and Redux (inside a reducer).
This can be seen as a simpler and more intuitive alternative to theReact Immutability Helpers andImmutable.js.
npm install object-path-immutable --saveThe 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 (likeImmutable.js)
constobj={a:{b:'c',c:['d','f']}}constnewObj=immutable.set(obj,'a.b','f')// {// a: {// b: 'f',// c: ['d', 'f']// }// }// obj !== newObj// obj.a !== newObj.a// obj.a.b !== newObj.a.b// However:// obj.a.c === newObj.a.c
You can also chain the api's and callvalue() at the end to retrieve the resulting object.
constnewObj=immutable.wrap(obj).set('a.b','f').del('a.c.0').value()
// Premisesconstobj={a:{b:'c',c:['d','f']}}import*asimmutablefrom'object-path-immutable'
Changes an object property.
- Path can be either a string or an array.
constnewObj1=immutable.set(obj,'a.b','f')constnewObj2=immutable.set(obj,['a','b'],'f')// {// a: {// b: 'f',// c: ['d', 'f']// }// }// Note that if the path is specified as a string, numbers are automatically interpreted as array indexes.constnewObj=immutable.set(obj,'a.c.1','fooo')// {// a: {// b: 'f',// c: ['d', 'fooo']// }// }
Updates an object property.
constobj={a:{b:1}}constnewObj=immutable.update(obj,['a','b'],v=>v+1)// {// a: {// b: 2,// }// }
Push into a deep array (it will create intermediate objects/arrays if necessary).
constnewObj=immutable.push(obj,'a.d','f')// {// a: {// b: 'f',// c: ['d', 'f'],// d: ['f']// }// }
Deletes a property.
constnewObj=immutable.del(obj,'a.c')// {// a: {// b: 'f'// }// }
Can also delete a deep array item using splice
constnewObj=immutable.del(obj,'a.c.0')// {// a: {// b: 'f',// c: ['f']// }// }
Shallow copy properties.
constnewObj=immutable.assign(obj,'a',{b:'f',g:'h'})// {// a: {// b: 'f',// c: ['d, 'f'],// g: 'h'// }// }
Insert property at the specific array index.
constnewObj=immutable.insert(obj,'a.c','k',1)// var obj = {// a: {// b: 'c',// c: ['d, 'k' 'f'],// }// }
Deep merge properties.
constnewObj=immutable.merge(obj,'a.c',{b:'d'})
Retrieve a deep object property. Imported fromobject-path for convenience.
About
Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.