1.1.0 • Published 7 years ago

cc-update-prop v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

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
1.1.0

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago