@samitouri / QOS-React-2 / commits / c5bf2ada4b

[addons] Renamed update() directives to commands

plievone committed Mar 22, 2014 at 01:40 UTC c5bf2ada4b91ddf8dbed38944d80d64e61554f3d
3 files changed +47 -47
docs/docs/09.6-update.md
+2 -2
@@ -51,9 +51,9 @@ var newData = React.addons.update(myData, {
51
52 While the syntax takes a little getting used to (though it's inspired by [MongoDB's query language](http://docs.mongodb.org/manual/core/crud-introduction/#query)) there's no redundancy, it's statically analyzable and it's not much more typing than the mutative version.
53
54 -The `$`-prefixed keys are called *directives*. The data structure they are "mutating" is called the *target*.
54 +The `$`-prefixed keys are called *commands*. The data structure they are "mutating" is called the *target*.
55
56 -## Available directives
56 +## Available commands
57
58 * `{$push: array}` `push()` all the items in `array` on the target
59 * `{$unshift: array}` `unshift()` all the items in `array` on the target
src/addons/__tests__/update-test.js
+1 -1
@@ -85,7 +85,7 @@ describe('update', function() {
85 });
86 });
87
88 - it('should require a directive', function() {
88 + it('should require a command', function() {
89 expect(update.bind(null, {a: 'b'}, {a: 'c'})).toThrow(
90 'Invariant Violation: update(): You provided a key path to update() ' +
91 'that did not contain one of $push, $unshift, $splice, $set, $merge. ' +
src/addons/update.js
+44 -44
@@ -32,39 +32,39 @@ function shallowCopy(x) {
32 }
33 }
34
35 -var DIRECTIVE_PUSH = keyOf({$push: null});
36 -var DIRECTIVE_UNSHIFT = keyOf({$unshift: null});
37 -var DIRECTIVE_SPLICE = keyOf({$splice: null});
38 -var DIRECTIVE_SET = keyOf({$set: null});
39 -var DIRECTIVE_MERGE = keyOf({$merge: null});
40 -
41 -var ALL_DIRECTIVES_LIST = [
42 - DIRECTIVE_PUSH,
43 - DIRECTIVE_UNSHIFT,
44 - DIRECTIVE_SPLICE,
45 - DIRECTIVE_SET,
46 - DIRECTIVE_MERGE
35 +var COMMAND_PUSH = keyOf({$push: null});
36 +var COMMAND_UNSHIFT = keyOf({$unshift: null});
37 +var COMMAND_SPLICE = keyOf({$splice: null});
38 +var COMMAND_SET = keyOf({$set: null});
39 +var COMMAND_MERGE = keyOf({$merge: null});
40 +
41 +var ALL_COMMANDS_LIST = [
42 + COMMAND_PUSH,
43 + COMMAND_UNSHIFT,
44 + COMMAND_SPLICE,
45 + COMMAND_SET,
46 + COMMAND_MERGE
47 ];
48
49 -var ALL_DIRECTIVES_SET = {};
49 +var ALL_COMMANDS_SET = {};
50
51 -ALL_DIRECTIVES_LIST.forEach(function(directive) {
52 - ALL_DIRECTIVES_SET[directive] = true;
51 +ALL_COMMANDS_LIST.forEach(function(command) {
52 + ALL_COMMANDS_SET[command] = true;
53 });
54
55 -function invariantArrayCase(value, spec, directive) {
55 +function invariantArrayCase(value, spec, command) {
56 invariant(
57 Array.isArray(value),
58 'update(): expected target of %s to be an array; got %s.',
59 - directive,
59 + command,
60 value
61 );
62 - var specValue = spec[directive];
62 + var specValue = spec[command];
63 invariant(
64 Array.isArray(specValue),
65 'update(): expected spec of %s to be an array; got %s. ' +
66 'Did you forget to wrap your parameter in an array?',
67 - directive,
67 + command,
68 specValue
69 );
70 }
@@ -74,81 +74,81 @@ function update(value, spec) {
74 typeof spec === 'object',
75 'update(): You provided a key path to update() that did not contain one ' +
76 'of %s. Did you forget to include {%s: ...}?',
77 - ALL_DIRECTIVES_LIST.join(', '),
78 - DIRECTIVE_SET
77 + ALL_COMMANDS_LIST.join(', '),
78 + COMMAND_SET
79 );
80
81 - if (spec.hasOwnProperty(DIRECTIVE_SET)) {
81 + if (spec.hasOwnProperty(COMMAND_SET)) {
82 invariant(
83 Object.keys(spec).length === 1,
84 'Cannot have more than one key in an object with %s',
85 - DIRECTIVE_SET
85 + COMMAND_SET
86 );
87
88 - return spec[DIRECTIVE_SET];
88 + return spec[COMMAND_SET];
89 }
90
91 var nextValue = shallowCopy(value);
92
93 - if (spec.hasOwnProperty(DIRECTIVE_MERGE)) {
94 - var mergeObj = spec[DIRECTIVE_MERGE];
93 + if (spec.hasOwnProperty(COMMAND_MERGE)) {
94 + var mergeObj = spec[COMMAND_MERGE];
95 invariant(
96 mergeObj && typeof mergeObj === 'object',
97 'update(): %s expects a spec of type \'object\'; got %s',
98 - DIRECTIVE_MERGE,
98 + COMMAND_MERGE,
99 mergeObj
100 );
101 invariant(
102 nextValue && typeof nextValue === 'object',
103 'update(): %s expects a target of type \'object\'; got %s',
104 - DIRECTIVE_MERGE,
104 + COMMAND_MERGE,
105 nextValue
106 );
107 - copyProperties(nextValue, spec[DIRECTIVE_MERGE]);
107 + copyProperties(nextValue, spec[COMMAND_MERGE]);
108 }
109
110 - if (spec.hasOwnProperty(DIRECTIVE_PUSH)) {
111 - invariantArrayCase(value, spec, DIRECTIVE_PUSH);
112 - spec[DIRECTIVE_PUSH].forEach(function(item) {
110 + if (spec.hasOwnProperty(COMMAND_PUSH)) {
111 + invariantArrayCase(value, spec, COMMAND_PUSH);
112 + spec[COMMAND_PUSH].forEach(function(item) {
113 nextValue.push(item);
114 });
115 }
116
117 - if (spec.hasOwnProperty(DIRECTIVE_UNSHIFT)) {
118 - invariantArrayCase(value, spec, DIRECTIVE_UNSHIFT);
119 - spec[DIRECTIVE_UNSHIFT].forEach(function(item) {
117 + if (spec.hasOwnProperty(COMMAND_UNSHIFT)) {
118 + invariantArrayCase(value, spec, COMMAND_UNSHIFT);
119 + spec[COMMAND_UNSHIFT].forEach(function(item) {
120 nextValue.unshift(item);
121 });
122 }
123
124 - if (spec.hasOwnProperty(DIRECTIVE_SPLICE)) {
124 + if (spec.hasOwnProperty(COMMAND_SPLICE)) {
125 invariant(
126 Array.isArray(value),
127 'Expected %s target to be an array; got %s',
128 - DIRECTIVE_SPLICE,
128 + COMMAND_SPLICE,
129 value
130 );
131 invariant(
132 - Array.isArray(spec[DIRECTIVE_SPLICE]),
132 + Array.isArray(spec[COMMAND_SPLICE]),
133 'update(): expected spec of %s to be an array of arrays; got %s. ' +
134 'Did you forget to wrap your parameters in an array?',
135 - DIRECTIVE_SPLICE,
136 - spec[DIRECTIVE_SPLICE]
135 + COMMAND_SPLICE,
136 + spec[COMMAND_SPLICE]
137 );
138 - spec[DIRECTIVE_SPLICE].forEach(function(args) {
138 + spec[COMMAND_SPLICE].forEach(function(args) {
139 invariant(
140 Array.isArray(args),
141 'update(): expected spec of %s to be an array of arrays; got %s. ' +
142 'Did you forget to wrap your parameters in an array?',
143 - DIRECTIVE_SPLICE,
144 - spec[DIRECTIVE_SPLICE]
143 + COMMAND_SPLICE,
144 + spec[COMMAND_SPLICE]
145 );
146 nextValue.splice.apply(nextValue, args);
147 });
148 }
149
150 for (var k in spec) {
151 - if (!ALL_DIRECTIVES_SET[k]) {
151 + if (!ALL_COMMANDS_SET[k]) {
152 nextValue[k] = update(value[k], spec[k]);
153 }
154 }