https://github.com/ln613/ipath
Ensure immutability by updating javascript objects using a path similar to CSS/jQuery selector, great for react/redux applications where immutability is required, especially when working with deeply nested objects.
https://github.com/ln613/ipath
Last synced: 11 months ago
JSON representation
Ensure immutability by updating javascript objects using a path similar to CSS/jQuery selector, great for react/redux applications where immutability is required, especially when working with deeply nested objects.
- Host: GitHub
- URL: https://github.com/ln613/ipath
- Owner: ln613
- License: mit
- Created: 2017-12-16T02:10:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T12:06:05.000Z (over 3 years ago)
- Last Synced: 2025-06-27T20:53:37.097Z (12 months ago)
- Language: JavaScript
- Size: 823 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ipath
[](https://travis-ci.org/ln613/ipath)
Ensure immutability by updating javascript objects using a path similar to CSS/jQuery selector, great for react/redux applications where immutability is required, especially when working with deeply nested objects.
[documetation](https://ln613.gitbooks.io/ipath/)
## Install
`npm i -S ipath`
## Usage
```js
import { update } from 'ipath';
const obj = {
artists: [
{
id: 3,
name: 'A1',
albums: [
{ id: 1, name: 'a1', year: 1990 },
{ id: 2, name: 'a2', year: 1995 },
]
}
]
};
let newObj = update(obj, 'artists[id=3].albums[name=a2].year', 1992);
// obj doesn't change, newObj.artists[0].albums[1].year = 1992
let newAlbum = { id: 3, name: 'a3', year: 2000 }
newObj = update(obj, 'artists[0].albums[]', newAlbum);
// add newAlbum to the albums list
newObj = update(obj, 'artists[id=3].albums[name=a2]', null);
// delete an item from array
newObj = update({ a: { b: 1 } }, 'a.b', x => x + 1);
// calculate the new value based on the existing value
```