UNPKG

5.17 kBJavaScriptView Raw
1/**
2 * Copyright 2013-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 */
10
11/* global hasOwnProperty:true */
12
13'use strict';
14
15var _prodInvariant = require('./reactProdInvariant'),
16 _assign = require('object-assign');
17
18var invariant = require('fbjs/lib/invariant');
19var hasOwnProperty = {}.hasOwnProperty;
20
21function shallowCopy(x) {
22 if (Array.isArray(x)) {
23 return x.concat();
24 } else if (x && typeof x === 'object') {
25 return _assign(new x.constructor(), x);
26 } else {
27 return x;
28 }
29}
30
31var COMMAND_PUSH = '$push';
32var COMMAND_UNSHIFT = '$unshift';
33var COMMAND_SPLICE = '$splice';
34var COMMAND_SET = '$set';
35var COMMAND_MERGE = '$merge';
36var COMMAND_APPLY = '$apply';
37
38var ALL_COMMANDS_LIST = [COMMAND_PUSH, COMMAND_UNSHIFT, COMMAND_SPLICE, COMMAND_SET, COMMAND_MERGE, COMMAND_APPLY];
39
40var ALL_COMMANDS_SET = {};
41
42ALL_COMMANDS_LIST.forEach(function (command) {
43 ALL_COMMANDS_SET[command] = true;
44});
45
46function invariantArrayCase(value, spec, command) {
47 !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;
48 var specValue = spec[command];
49 !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;
50}
51
52/**
53 * Returns a updated shallow copy of an object without mutating the original.
54 * See https://facebook.github.io/react/docs/update.html for details.
55 */
56function update(value, spec) {
57 !(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;
58
59 if (hasOwnProperty.call(spec, COMMAND_SET)) {
60 !(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;
61
62 return spec[COMMAND_SET];
63 }
64
65 var nextValue = shallowCopy(value);
66
67 if (hasOwnProperty.call(spec, COMMAND_MERGE)) {
68 var mergeObj = spec[COMMAND_MERGE];
69 !(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;
70 !(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;
71 _assign(nextValue, spec[COMMAND_MERGE]);
72 }
73
74 if (hasOwnProperty.call(spec, COMMAND_PUSH)) {
75 invariantArrayCase(value, spec, COMMAND_PUSH);
76 spec[COMMAND_PUSH].forEach(function (item) {
77 nextValue.push(item);
78 });
79 }
80
81 if (hasOwnProperty.call(spec, COMMAND_UNSHIFT)) {
82 invariantArrayCase(value, spec, COMMAND_UNSHIFT);
83 spec[COMMAND_UNSHIFT].forEach(function (item) {
84 nextValue.unshift(item);
85 });
86 }
87
88 if (hasOwnProperty.call(spec, COMMAND_SPLICE)) {
89 !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;
90 !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;
91 spec[COMMAND_SPLICE].forEach(function (args) {
92 !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;
93 nextValue.splice.apply(nextValue, args);
94 });
95 }
96
97 if (hasOwnProperty.call(spec, COMMAND_APPLY)) {
98 !(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;
99 nextValue = spec[COMMAND_APPLY](nextValue);
100 }
101
102 for (var k in spec) {
103 if (!(ALL_COMMANDS_SET.hasOwnProperty(k) && ALL_COMMANDS_SET[k])) {
104 nextValue[k] = update(value[k], spec[k]);
105 }
106 }
107
108 return nextValue;
109}
110
111module.exports = update;
\No newline at end of file