`

update

 
阅读更多

update模块用于快速作数据处理。

 

'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);// 使用x对象的构造函数创建对象
  } 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需要为数组形式
  !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]需要为数组形式
  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;
}

// 配置形式快速作数据处理后返回,支持'$push'、'$unshift'、'$splice'、'$set'、'$merge'、'$apply'
// const newData = update(myData, {
//   x: {y: {z: {$set: 7}}},
//   a: {b: {$push: [9]}}
// });
function update(value, spec) {
  // spec不能是非对象
  !(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;

  // spec含$set属性时,只能接受单个属性
  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);

  // spec含$merge属性时,将sepc["$merge"]对象合并到value对象中
  if (hasOwnProperty.call(spec, COMMAND_MERGE)) {
    var mergeObj = spec[COMMAND_MERGE];

    // sepc["$merge"]需要为对象形式
    !(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需要为对象形式
    !(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]);
  }

  // spec含$push属性时,将sepc["$push"]数组添加到value数组的尾部
  if (hasOwnProperty.call(spec, COMMAND_PUSH)) {
    invariantArrayCase(value, spec, COMMAND_PUSH);
    spec[COMMAND_PUSH].forEach(function (item) {
      nextValue.push(item);
    });
  }

  // spec含$unshift属性时,将sepc["$unshift"]数组添加到value数组的顶部
  if (hasOwnProperty.call(spec, COMMAND_UNSHIFT)) {
    invariantArrayCase(value, spec, COMMAND_UNSHIFT);
    spec[COMMAND_UNSHIFT].forEach(function (item) {
      nextValue.unshift(item);
    });
  }

  // spec含$splice属性时,对value作splice处理后返回
  if (hasOwnProperty.call(spec, COMMAND_SPLICE)) {
    // value需要为数组
    !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"]须是数组
    !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.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;

      // 使用apply方法将args拆解为单参数传给splice方法,args须是起始项序号、删除元素个数、待添加的元素
      nextValue.splice.apply(nextValue, args);
    });
  }

  // spec含$apply属性时,调用spec["$apply"]函数处理value后返回
  if (hasOwnProperty.call(spec, COMMAND_APPLY)) {
    // spec["$apply"]须是函数
    !(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);
  }

  // 递归调用
  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;

 

0
0
分享到:
评论

相关推荐

    Mysql Update批量更新的几种方式

    UPDATE mytable SET myfield='value' WHERE other_field='other_value'; 但是,如果你想更新多行数据,并且每行记录的各字段值都是各不一样,你会怎么办呢?刚开始你可能会想到使用循环执行多条UPDATE语句的方式,就...

    vs2013_update5下载链接(请使用迅雷下载)

    Visual Studio Premium 2013 with Update 5 (x86) – DVD (Chinese-Simplified) 文件名:cn_visual_studio_premium_2013_with_update_5_x86_dvd_6815741.iso SHA1:9A2B64D73AAC916EA606B42B164A9C6E88D062D6 文件...

    华为官方固件UPDATE.APP解包打包工具

    非常威猛的官方固件UPDATE.APP解包打包工具(转自XDA论坛) Huawei Update Extractor After messing around a bit with the perl tools available for extracting Huawei update.app files, i got the idea to ...

    oracle执行update语句时卡住问题分析及解决办法

    这种只有update无法执行其他语句可以执行的其实是因为记录锁导致的,在oracle中,执行了update或者insert语句后,都会要求commit,如果不commit却强制关闭连接,oracle就会将这条提交的记录锁住。由于我的java程序...

    FixUpdate与Update的区别1

    FixUpdate与Update的区别1

    Huawei Update Extractor 0.9.9.5

    Huawei Update Extractor是华为官方ROM的打包封包工具,是自定义华为官方的ROM的神器。此版本是2017年的最新版本。 周围可用于提取华为update.app文件perl的工具有点搞乱后, 我得到了主意,创建一个自己的(窗口)...

    Windows更新WindowsUpdate重置工具Reset-WindowsUpdate.rar

    Windows更新WindowsUpdate重置工具Reset-WindowsUpdate.rar,如果WindowsUpdate不正常可以试试这个工具。

    系统更新包制作 update.zip

    ./build/tools/releasetools/ota_from_target_files -x pagesize=2048 -n -i update1.zip update2.zip update.zip 3、recovery执行系统更新 adb push SystemUpdate.apk /system/app 运行SystemUpdate.apk 完成...

    Microsoft Visual C++ 2015 Redistributable Update 3 RC

    要了解 Visual Studio 2015 Update 3 RC 中的新增功能,请参阅 Visual Studio 2015 Update 3 RC 发行说明。有关已修复的 Bug 和已知问题列表,请参阅 Visual Studio 2015 Update 3 RC MSDN 文章。 支持的操作系统 ...

    update 语句优化update 语句优化update 语句优化

    update 语句优化

    Android A/B分区OTA系统升级应用层调用UpdateEngine Apk源码

    后面各种查资料,结合系统代码,写了一个解析类 只需传入update.zip包,解析后升级,爽的一批。需要注意的坑在代码里也备注了,有需要的码友要注意下,特别是如果卡住了找不到原因所在的情况。参考博文: ...

    Visual Studio 2005 Service Pack 1 Update for Windows Vista

    1.安装本更新之前,强烈建议您阅读 Visual Studio 2005 Service Pack 1 Update for Windows Vista 发行说明。 2.如果您的计算机安装了 Visual Studio 2005 Service Pack 1 Update for Windows Vista Beta,请务必...

    数据库oracle for update of和for update的区别

    数据库oracle锁: for update of和for update的区别,解释得很清楚,有很多详细例子说明。

    xmind-8-update7-windows

    xmind-8-update7-windows.exe 下载路径:https://www.xmind.net/download/win/ 1.首先要断网 2.打开"c:\windows\system32\drivers\etc",以文本方式打开hosts,添加以下内容 # XMind Mind-Mapper 0.0.0.0 xmind...

    Tiger Update实现自动更新

    Tiger Update控件使用指南 Tiger Update是Tiger Installer安装制作软件附带的一个控件,它可以帮助 你轻松地在你的软件上加入自动更新功能。这个控件是免费而且开放源代码的。 Tiger Update实现自动更新实现的原理...

    UN Regulation No.156 - Software update and software update manag

    软件更新管理:UNECE R156 SUMS(software update management system)

    Update和Select结合使用

    Update和Select结合使用,批量做更新。

    WindowsUpdate修复工具.zip

    Windows Update服务有问题的时候,使用这两个工具修复Windows Update组件和服务。

    DelphiXE Update3 破解

    1、针对 Update2,结合 Freecat 的 AutoPatch 重新制作,感谢 Pingos、Freecat 和 Yinsim 2011.10.10 - v5.2 1、针对 Update1 调整文件补丁 2011.09.20 - v5.1 1、首次发布 BTW: 为啥不采用支持安装的 KeyGen...

    delphi XE5 update2 Patch

    1. Install RAD Studio XE5 Update 2 with generated serial. 2. Patch "bds.exe" and "sanctuarylib.dll". 3. Click "Generate Activation File". 4. Start RAD Studio. It's recommended to block all internet ...

Global site tag (gtag.js) - Google Analytics