Skip to content

Commit 1dc63ab

Browse files
Moshe Kolodnykolodny
authored andcommitted
Support nibling directives
1 parent 954aa16 commit 1dc63ab

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

index.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,24 @@ function newContext() {
5454
Object.keys(commands).join(', ')
5555
);
5656

57-
var newObject = object;
57+
var nextObject = object;
5858
var specKeys = getAllKeys(spec)
5959
var index, key;
6060
for (index = 0; index < specKeys.length; index++) {
6161
var key = specKeys[index];
6262
if (hasOwnProperty.call(commands, key)) {
63-
return commands[key](spec[key], newObject, spec, object);
63+
nextObject = commands[key](spec[key], nextObject, spec, object);
64+
} else {
65+
var nextValueForKey = update(object[key], spec[key]);
66+
if (nextValueForKey !== nextObject[key]) {
67+
if (nextObject === object) {
68+
nextObject = copy(object);
69+
}
70+
nextObject[key] = nextValueForKey;
71+
}
6472
}
6573
}
66-
for (index = 0; index < specKeys.length; index++) {
67-
var key = specKeys[index];
68-
var nextValueForKey = update(object[key], spec[key]);
69-
if (nextValueForKey === object[key]) {
70-
continue;
71-
}
72-
if (newObject === object) {
73-
newObject = copy(object);
74-
}
75-
newObject[key] = nextValueForKey;
76-
}
77-
return newObject;
74+
return nextObject;
7875
}
7976

8077
}
@@ -88,8 +85,8 @@ var defaultCommands = {
8885
invariantPushAndUnshift(original, spec, '$unshift');
8986
return value.concat(original);
9087
},
91-
$splice: function(value, newObject, spec, object) {
92-
var originalValue = newObject === object ? copy(object) : newObject;
88+
$splice: function(value, nextObject, spec, object) {
89+
var originalValue = nextObject === object ? copy(object) : nextObject;
9390
invariantSplices(originalValue, spec);
9491
value.forEach(function(args) {
9592
invariantSplice(args);
@@ -101,8 +98,8 @@ var defaultCommands = {
10198
invariantSet(spec);
10299
return value;
103100
},
104-
$merge: function(value, newObject, spec, object) {
105-
var originalValue = newObject === object ? copy(object) : newObject;
101+
$merge: function(value, nextObject, spec, object) {
102+
var originalValue = nextObject === object ? copy(object) : nextObject;
106103
invariantMerge(originalValue, value);
107104
getAllKeys(value).forEach(function(key) {
108105
originalValue[key] = value[key];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "immutability-helper",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"description": "mutate a copy of data without changing the original source",
55
"main": "index.js",
66
"scripts": {

test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,15 @@ describe('update', function() {
279279
expect(myUpdate.bind(null, {$addtax: 0.10}, {$addtax: 0.10})).toNotThrow();
280280
});
281281

282+
it('can handle nibling directives', function() {
283+
var obj = {a: [1, 2, 3], b: "me"};
284+
var spec = {
285+
a: {$splice: [[0, 2]]},
286+
$merge: {b: "you"},
287+
};
288+
expect(update(obj, spec)).toEqual({"a":[3],"b":"you"})
289+
});
290+
282291
});
283292

284293
if (typeof Symbol === 'function' && Symbol('TEST').toString() === 'Symbol(TEST)') {

0 commit comments

Comments
 (0)