react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / addons / update.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule update9*/1011"use strict";1213var assign = require('Object.assign');14var keyOf = require('keyOf');15var invariant = require('invariant');1617function shallowCopy(x) {18if (Array.isArray(x)) {19return x.concat();20} else if (x && typeof x === 'object') {21return assign(new x.constructor(), x);22} else {23return x;24}25}2627var COMMAND_PUSH = keyOf({$push: null});28var COMMAND_UNSHIFT = keyOf({$unshift: null});29var COMMAND_SPLICE = keyOf({$splice: null});30var COMMAND_SET = keyOf({$set: null});31var COMMAND_MERGE = keyOf({$merge: null});32var COMMAND_APPLY = keyOf({$apply: null});3334var ALL_COMMANDS_LIST = [35COMMAND_PUSH,36COMMAND_UNSHIFT,37COMMAND_SPLICE,38COMMAND_SET,39COMMAND_MERGE,40COMMAND_APPLY41];4243var ALL_COMMANDS_SET = {};4445ALL_COMMANDS_LIST.forEach(function(command) {46ALL_COMMANDS_SET[command] = true;47});4849function invariantArrayCase(value, spec, command) {50invariant(51Array.isArray(value),52'update(): expected target of %s to be an array; got %s.',53command,54value55);56var specValue = spec[command];57invariant(58Array.isArray(specValue),59'update(): expected spec of %s to be an array; got %s. ' +60'Did you forget to wrap your parameter in an array?',61command,62specValue63);64}6566function update(value, spec) {67invariant(68typeof spec === 'object',69'update(): You provided a key path to update() that did not contain one ' +70'of %s. Did you forget to include {%s: ...}?',71ALL_COMMANDS_LIST.join(', '),72COMMAND_SET73);7475if (spec.hasOwnProperty(COMMAND_SET)) {76invariant(77Object.keys(spec).length === 1,78'Cannot have more than one key in an object with %s',79COMMAND_SET80);8182return spec[COMMAND_SET];83}8485var nextValue = shallowCopy(value);8687if (spec.hasOwnProperty(COMMAND_MERGE)) {88var mergeObj = spec[COMMAND_MERGE];89invariant(90mergeObj && typeof mergeObj === 'object',91'update(): %s expects a spec of type \'object\'; got %s',92COMMAND_MERGE,93mergeObj94);95invariant(96nextValue && typeof nextValue === 'object',97'update(): %s expects a target of type \'object\'; got %s',98COMMAND_MERGE,99nextValue100);101assign(nextValue, spec[COMMAND_MERGE]);102}103104if (spec.hasOwnProperty(COMMAND_PUSH)) {105invariantArrayCase(value, spec, COMMAND_PUSH);106spec[COMMAND_PUSH].forEach(function(item) {107nextValue.push(item);108});109}110111if (spec.hasOwnProperty(COMMAND_UNSHIFT)) {112invariantArrayCase(value, spec, COMMAND_UNSHIFT);113spec[COMMAND_UNSHIFT].forEach(function(item) {114nextValue.unshift(item);115});116}117118if (spec.hasOwnProperty(COMMAND_SPLICE)) {119invariant(120Array.isArray(value),121'Expected %s target to be an array; got %s',122COMMAND_SPLICE,123value124);125invariant(126Array.isArray(spec[COMMAND_SPLICE]),127'update(): expected spec of %s to be an array of arrays; got %s. ' +128'Did you forget to wrap your parameters in an array?',129COMMAND_SPLICE,130spec[COMMAND_SPLICE]131);132spec[COMMAND_SPLICE].forEach(function(args) {133invariant(134Array.isArray(args),135'update(): expected spec of %s to be an array of arrays; got %s. ' +136'Did you forget to wrap your parameters in an array?',137COMMAND_SPLICE,138spec[COMMAND_SPLICE]139);140nextValue.splice.apply(nextValue, args);141});142}143144if (spec.hasOwnProperty(COMMAND_APPLY)) {145invariant(146typeof spec[COMMAND_APPLY] === 'function',147'update(): expected spec of %s to be a function; got %s.',148COMMAND_APPLY,149spec[COMMAND_APPLY]150);151nextValue = spec[COMMAND_APPLY](nextValue);152}153154for (var k in spec) {155if (!(ALL_COMMANDS_SET.hasOwnProperty(k) && ALL_COMMANDS_SET[k])) {156nextValue[k] = update(value[k], spec[k]);157}158}159160return nextValue;161}162163module.exports = update;164165166