update

The update module is used for fast data processing.

 

'use strict';

var _prodInvariant = require('./reactProdInvariant'),
    _assign = require('object-assign');

var invariant = require('fbjs/lib/invariant');
var hasOwnProperty = {}.hasOwnProperty;

function shallowCopy(x) {
  if (Array.isArray(x)) {
    return x.concat();
  } else if (x && typeof x === 'object') {
    return _assign(new x.constructor(), x);// use the constructor of the x object to create an object
  } else {
    return x;
  }
}

var COMMAND_PUSH = '$push';
var COMMAND_UNSHIFT = '$unshift';
var COMMAND_SPLICE = '$splice';
var COMMAND_SET = '$set';
var COMMAND_MERGE = '$ merge';
var COMMAND_APPLY = '$apply';

var ALL_COMMANDS_LIST = [COMMAND_PUSH, COMMAND_UNSHIFT, COMMAND_SPLICE, COMMAND_SET, COMMAND_MERGE, COMMAND_APPLY];

var ALL_COMMANDS_SET = {};

ALL_COMMANDS_LIST.forEach(function (command) {
  ALL_COMMANDS_SET[command] = true;
});

function invariantArrayCase(value, spec, command) {
  // value needs to be in the form of an array
  !Array.isArray(value) ?
    process.env.NODE_ENV !== 'production' ?
      invariant(false, 'update(): expected target of %s to be an array; got %s.', command, value)
      : _prodInvariant('1', command, value)
    : void 0;

  // spec[command] needs to be in array form
  var specValue = spec[command];
  !Array.isArray(specValue) ?
    process.env.NODE_ENV !== 'production' ?
      invariant(false, 'update(): expected spec of %s to be an array; got %s.'
        + ' Did you forget to wrap your parameter in an array?', command, specValue)
      : _prodInvariant('2', command, specValue)
    : void 0;
}

// The configuration form returns after quick data processing, supports '$push', '$unshift', '$splice', '$set', '$merge', '$apply'
// const newData = update(myData, {
//   x: {y: {z: {$set: 7}}},
//   a: {b: {$push: [9]}}
// });
function update(value, spec) {
  // spec cannot be a non-object
  !(typeof spec === 'object') ?
    process.env.NODE_ENV !== 'production' ?
      invariant(false,
        'update(): You provided a key path to update() that did not contain one of %s. '
        + 'Did you forget to include {%s: ...}?', ALL_COMMANDS_LIST.join(', '), COMMAND_SET)
      : _prodInvariant('3', ALL_COMMANDS_LIST.join(', '), COMMAND_SET) : void 0;

  // When the spec contains the $set attribute, it can only accept a single attribute
  if (hasOwnProperty.call(spec, COMMAND_SET)) {
    !(Object.keys(spec).length === 1) ?
      process.env.NODE_ENV !== 'production' ?
        invariant(false, 'Cannot have more than one key in an object with %s', COMMAND_SET)
        : _prodInvariant('4', COMMAND_SET) : void 0;

    return spec[COMMAND_SET];
  }

  var nextValue = shallowCopy(value);

  // When the spec contains the $merge attribute, merge the sepc["$merge"] object into the value object
  if (hasOwnProperty.call(spec, COMMAND_MERGE)) {
    var mergeObj = spec[COMMAND_MERGE];

    // sepc["$merge"] needs to be in object form
    !(mergeObj && typeof mergeObj === 'object') ?
      process.env.NODE_ENV !== 'production' ?
        invariant(false, 'update(): %s expects a spec of type \'object\'; got %s',
          COMMAND_MERGE, mergeObj)
        : _prodInvariant ('5', COMMAND_MERGE, mergeObj)
      : void 0;

    // value needs to be in object form
    !(nextValue && typeof nextValue === 'object') ?
      process.env.NODE_ENV !== 'production' ?
        invariant(false, 'update(): %s expects a target of type \'object\'; got %s',
          COMMAND_MERGE, nextValue)
        : _prodInvariant('6', COMMAND_MERGE, nextValue)
      : void 0;

    _assign(nextValue, spec[COMMAND_MERGE]);
  }

  // When the spec contains the $push attribute, add the sepc["$push"] array to the end of the value array
  if (hasOwnProperty.call(spec, COMMAND_PUSH)) {
    invariantArrayCase(value, spec, COMMAND_PUSH);
    spec[COMMAND_PUSH].forEach(function (item) {
      nextValue.push(item);
    });
  }

  // When the spec contains the $unshift attribute, add the sepc["$unshift"] array to the top of the value array
  if (hasOwnProperty.call(spec, COMMAND_UNSHIFT)) {
    invariantArrayCase(value, spec, COMMAND_UNSHIFT);
    spec[COMMAND_UNSHIFT].forEach(function (item) {
      nextValue.unshift(item);
    });
  }

  // When the spec contains the $splice attribute, the value is processed by splice and returned
  if (hasOwnProperty.call(spec, COMMAND_SPLICE)) {
    // value needs to be an array
    !Array.isArray(value) ?
      process.env.NODE_ENV !== 'production' ?
        invariant(false, 'Expected %s target to be an array; got %s', COMMAND_SPLICE, value)
        : _prodInvariant('7', COMMAND_SPLICE, value)
      : void 0;

    // spec["$splice"] must be an array
    !Array.isArray(spec[COMMAND_SPLICE]) ?
      process.env.NODE_ENV !== 'production' ?
        invariant(false, 'update(): expected spec of %s to be an array of arrays;'
          + ' got %s. Did you forget to wrap your parameters in an array?',
          COMMAND_SPLICE, spec[COMMAND_SPLICE])
        : _prodInvariant('8', COMMAND_SPLICE, spec[COMMAND_SPLICE])
      : void 0;


    spec[COMMAND_SPLICE].forEach(function (args) {
      // spec["$splice"] array elements must be arrays
      !Array.isArray(args) ?
        process.env.NODE_ENV !== 'production' ?
          invariant(false, 'update(): expected spec of %s to be an array of arrays;'
            + ' got %s. Did you forget to wrap your parameters in an array?',
            COMMAND_SPLICE, spec[COMMAND_SPLICE])
          : _prodInvariant('8', COMMAND_SPLICE, spec[COMMAND_SPLICE])
        : void 0;

      // Use the apply method to disassemble args into a single parameter and pass it to the splice method. args must be the starting item number, the number of deleted elements, and the elements to be added.
      nextValue.splice.apply(nextValue, args);
    });
  }

  // When the spec contains the $apply attribute, call the spec["$apply"] function to process the value and return
  if (hasOwnProperty.call(spec, COMMAND_APPLY)) {
    // spec["$apply"] must be a function
    !(typeof spec[COMMAND_APPLY] === 'function') ?
      process.env.NODE_ENV !== 'production' ?
        invariant(false, 'update(): expected spec of %s to be a function; got %s.',
          COMMAND_APPLY, spec[COMMAND_APPLY])
        : _prodInvariant('9', COMMAND_APPLY, spec[COMMAND_APPLY])
      : void 0;

    nextValue = spec[COMMAND_APPLY](nextValue);
  }

  // recursive call
  for (var k in spec) {
    if (!(ALL_COMMANDS_SET.hasOwnProperty(k) && ALL_COMMANDS_SET[k])) {
      nextValue[k] = update(value[k], spec[k]);
    }
  }

  return nextValue;
}

module.exports = update;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326860691&siteId=291194637