1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict';
|
14 |
|
15 | var _prodInvariant = require('./reactProdInvariant'),
|
16 | _assign = require('object-assign');
|
17 |
|
18 | var invariant = require('fbjs/lib/invariant');
|
19 | var hasOwnProperty = {}.hasOwnProperty;
|
20 |
|
21 | function 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 |
|
31 | var COMMAND_PUSH = '$push';
|
32 | var COMMAND_UNSHIFT = '$unshift';
|
33 | var COMMAND_SPLICE = '$splice';
|
34 | var COMMAND_SET = '$set';
|
35 | var COMMAND_MERGE = '$merge';
|
36 | var COMMAND_APPLY = '$apply';
|
37 |
|
38 | var ALL_COMMANDS_LIST = [COMMAND_PUSH, COMMAND_UNSHIFT, COMMAND_SPLICE, COMMAND_SET, COMMAND_MERGE, COMMAND_APPLY];
|
39 |
|
40 | var ALL_COMMANDS_SET = {};
|
41 |
|
42 | ALL_COMMANDS_LIST.forEach(function (command) {
|
43 | ALL_COMMANDS_SET[command] = true;
|
44 | });
|
45 |
|
46 | function 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 |
|
54 |
|
55 |
|
56 | function 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 |
|
111 | module.exports = update; |
\ | No newline at end of file |