npm.io
1.1.0 • Published 9 years ago

cc-update-prop

Licence
ISC
Version
1.1.0
Deps
1
Vulns
0
Weekly
0

cc-update-prop: updateProp

  1. this is mainly used to be a helper for the dva's reducer
  2. also can be treated as a fast copy tool without deep walking into the json tree
  3. of course, redux reducer can also be applied with updateProp
  4. support default value as the placeholder
  5. 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

Keywords