1.1.0 • Published 7 years ago
cc-update-prop v1.1.0
cc-update-prop: updateProp
- this is mainly used to be a helper for the dva's reducer
- also can be treated as a fast copy tool without deep walking into the json tree
- of course, redux reducer can also be applied with updateProp
- support default value as the placeholder
- the updateProp.save can be a useful method to cancel the ctrl+c|ctrl+v work
the array test
var updateProp = require("cc-update-prop");
var state = [
{
x: {
aaa: 1
}
},
{
x: 2
}
]
var action = {
payload: {
ee: "asasas"
}
};
var cb = updateProp({
props: ["0", "x"],
cb(state, payload) {
return {
ee: payload.ee + "!!!!"
}
}
});
var ret = cb(state, action);
console.log(ret);
// [ { x: { aaa: 1, ee: 'asasas!!!!' } }, { x: 2 } ]
the object test
var updateProp = require("cc-update-prop");
var obj1 = {x: 1}
var state = {
aa: 1,
a: {
bb: obj1,
b: {
cc: 3,
c: {
dd: "cc, haha, I am here !"
}
}
}
};
var action = {
payload: {
ee: "asasas"
}
};
var cb = updateProp({
props: ["a", "b", "c"],
cb(state, payload) {
return {
ee: state.aa + payload.ee + "!!!!"
}
}
});
var ret = cb(state, action);
console.log(ret.a.b.c); // { dd: 'cc, haha, I am here !', ee: '1asasas!!!!' }
console.log(ret === state); // false
console.log(ret.a.b.c === state.a.b.c); // false
console.log(ret.a.bb === state.a.bb); // true