Skip to content

Commit

Permalink
Support nibling directives
Browse files Browse the repository at this point in the history
  • Loading branch information
Moshe Kolodny authored and kolodny committed Jan 8, 2017
1 parent 954aa16 commit 1dc63ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
33 changes: 15 additions & 18 deletions index.js
Expand Up @@ -54,27 +54,24 @@ function newContext() {
Object.keys(commands).join(', ')
);

var newObject = object;
var nextObject = object;
var specKeys = getAllKeys(spec)
var index, key;
for (index = 0; index < specKeys.length; index++) {
var key = specKeys[index];
if (hasOwnProperty.call(commands, key)) {
return commands[key](spec[key], newObject, spec, object);
nextObject = commands[key](spec[key], nextObject, spec, object);
} else {
var nextValueForKey = update(object[key], spec[key]);
if (nextValueForKey !== nextObject[key]) {
if (nextObject === object) {
nextObject = copy(object);
}
nextObject[key] = nextValueForKey;
}
}
}
for (index = 0; index < specKeys.length; index++) {
var key = specKeys[index];
var nextValueForKey = update(object[key], spec[key]);
if (nextValueForKey === object[key]) {
continue;
}
if (newObject === object) {
newObject = copy(object);
}
newObject[key] = nextValueForKey;
}
return newObject;
return nextObject;
}

}
Expand All @@ -88,8 +85,8 @@ var defaultCommands = {
invariantPushAndUnshift(original, spec, '$unshift');
return value.concat(original);
},
$splice: function(value, newObject, spec, object) {
var originalValue = newObject === object ? copy(object) : newObject;
$splice: function(value, nextObject, spec, object) {
var originalValue = nextObject === object ? copy(object) : nextObject;
invariantSplices(originalValue, spec);
value.forEach(function(args) {
invariantSplice(args);
Expand All @@ -101,8 +98,8 @@ var defaultCommands = {
invariantSet(spec);
return value;
},
$merge: function(value, newObject, spec, object) {
var originalValue = newObject === object ? copy(object) : newObject;
$merge: function(value, nextObject, spec, object) {
var originalValue = nextObject === object ? copy(object) : nextObject;
invariantMerge(originalValue, value);
getAllKeys(value).forEach(function(key) {
originalValue[key] = value[key];
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "immutability-helper",
"version": "2.1.0",
"version": "2.1.1",
"description": "mutate a copy of data without changing the original source",
"main": "index.js",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions test.js
Expand Up @@ -279,6 +279,15 @@ describe('update', function() {
expect(myUpdate.bind(null, {$addtax: 0.10}, {$addtax: 0.10})).toNotThrow();
});

it('can handle nibling directives', function() {
var obj = {a: [1, 2, 3], b: "me"};
var spec = {
a: {$splice: [[0, 2]]},
$merge: {b: "you"},
};
expect(update(obj, spec)).toEqual({"a":[3],"b":"you"})
});

});

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

0 comments on commit 1dc63ab

Please sign in to comment.