immutable-data
v2.0.5
Published
Easily update nested objects and arrays in a declarative and immutable manner
Downloads
57
Maintainers
Readme
immutable-data
Easily "set", "merge", "remove" nested objects and arrays in an immutable manner.
"immutable-data" lets you specify the part of the data you want to change, and then set the value, then it can change the part that you want to change, at the same time, the rest data that you do not want to change will not be affected.
api
set(data, map)
data: original data you want to change
map: the path map
{ 'a.b.0': 1 // or 'a["b"][0]': 1 }
eg:
var set = require("immutable-data").set;
var data = {
a: {
b: 2
},
c: [{
d: 2
}]
};
set(data, {
'a.b': 1,
'c.0.d': 1
})
//return
{
a: {
b: 1
},
c: [{
d: 1
}]
}
merge(data, object)
data: original data you want to change
object: deep merge object
eg:
var merge = require("immutable-data").merge;
var data = {
a: {
b: 2
},
c: [{
d: 2
}]
};
merge(data, {
a: {
b: 1
},
c: {
"0": {
d: 1
}
}
})
//return
{
a: {
b: 1
},
c: [{
d: 1
}]
}
Tip: If the type of a value is an array, it will be assigned a direct value.
merge({list:[1,2]}, {list:[0]})
//return
{list:[0]}
merge({list:[1,2]}, {list:{"0":0}})
//return
{list:[0,2]}
remove(data, path)
data: original data you want to change
path: String or Array
"a.b" ["a.b","a.c"]
eg:
var remove = require("immutable-data").remove;
var data = {
a: {
b: 2
},
c: [{
d: 2
}]
};
remove(data, [
'a.b',
'c.0'
])
// return
{
a: {},
c: []
}
dev
$ npm install
$ npm run dev
$ npm test
$ npm run build
License
MIT License