1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _prodInvariant = require('./reactProdInvariant'),
|
14 | _assign = require('object-assign');
|
15 |
|
16 | var invariant = require('fbjs/lib/invariant');
|
17 | var hasOwnProperty = {}.hasOwnProperty;
|
18 |
|
19 | function shallowCopy(x) {
|
20 | if (Array.isArray(x)) {
|
21 | return x.concat();
|
22 | } else if (x && typeof x === 'object') {
|
23 | return _assign(new x.constructor(), x);
|
24 | } else {
|
25 | return x;
|
26 | }
|
27 | }
|
28 |
|
29 | var COMMAND_PUSH = '$push';
|
30 | var COMMAND_UNSHIFT = '$unshift';
|
31 | var COMMAND_SPLICE = '$splice';
|
32 | var COMMAND_SET = '$set';
|
33 | var COMMAND_MERGE = '$merge';
|
34 | var COMMAND_APPLY = '$apply';
|
35 |
|
36 | var ALL_COMMANDS_LIST = [COMMAND_PUSH, COMMAND_UNSHIFT, COMMAND_SPLICE, COMMAND_SET, COMMAND_MERGE, COMMAND_APPLY];
|
37 |
|
38 | var ALL_COMMANDS_SET = {};
|
39 |
|
40 | ALL_COMMANDS_LIST.forEach(function (command) {
|
41 | ALL_COMMANDS_SET[command] = true;
|
42 | });
|
43 |
|
44 | function invariantArrayCase(value, spec, command) {
|
45 | !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;
|
46 | var specValue = spec[command];
|
47 | !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;
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function update(value, spec) {
|
55 | !(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;
|
56 |
|
57 | if (hasOwnProperty.call(spec, COMMAND_SET)) {
|
58 | !(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;
|
59 |
|
60 | return spec[COMMAND_SET];
|
61 | }
|
62 |
|
63 | var nextValue = shallowCopy(value);
|
64 |
|
65 | if (hasOwnProperty.call(spec, COMMAND_MERGE)) {
|
66 | var mergeObj = spec[COMMAND_MERGE];
|
67 | !(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;
|
68 | !(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;
|
69 | _assign(nextValue, spec[COMMAND_MERGE]);
|
70 | }
|
71 |
|
72 | if (hasOwnProperty.call(spec, COMMAND_PUSH)) {
|
73 | invariantArrayCase(value, spec, COMMAND_PUSH);
|
74 | spec[COMMAND_PUSH].forEach(function (item) {
|
75 | nextValue.push(item);
|
76 | });
|
77 | }
|
78 |
|
79 | if (hasOwnProperty.call(spec, COMMAND_UNSHIFT)) {
|
80 | invariantArrayCase(value, spec, COMMAND_UNSHIFT);
|
81 | spec[COMMAND_UNSHIFT].forEach(function (item) {
|
82 | nextValue.unshift(item);
|
83 | });
|
84 | }
|
85 |
|
86 | if (hasOwnProperty.call(spec, COMMAND_SPLICE)) {
|
87 | !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;
|
88 | !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;
|
89 | spec[COMMAND_SPLICE].forEach(function (args) {
|
90 | !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;
|
91 | nextValue.splice.apply(nextValue, args);
|
92 | });
|
93 | }
|
94 |
|
95 | if (hasOwnProperty.call(spec, COMMAND_APPLY)) {
|
96 | !(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;
|
97 | nextValue = spec[COMMAND_APPLY](nextValue);
|
98 | }
|
99 |
|
100 | for (var k in spec) {
|
101 | if (!(ALL_COMMANDS_SET.hasOwnProperty(k) && ALL_COMMANDS_SET[k])) {
|
102 | nextValue[k] = update(value[k], spec[k]);
|
103 | }
|
104 | }
|
105 |
|
106 | return nextValue;
|
107 | }
|
108 |
|
109 | module.exports = update; |
\ | No newline at end of file |