Skip to content

Commit

Permalink
Merge pull request #1 from AleksandrZhukov/master
Browse files Browse the repository at this point in the history
fix problem with dashed key or value for lookup
  • Loading branch information
akuzko authored Dec 10, 2017
2 parents 84e526a + adafd2e commit efc9911
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function updateDel(obj, path) {
}

function _update(current, path, fn) {
var match = path.match(/^([{\w\d:_-}]+)\.?(.+)?$/);
var match = path.match(/^([{\w\d:_\-}]+)\.?(.+)?$/);
var key = match[1], rest = match[2];

if (isLookupKey(key)) {
Expand Down
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ describe('update', function() {
}, 'no object found by {id:2}. autocreate is not supported');
});
});

context('when lookup key or value has `-`', function() {
it('performs lookup and carefully sets deeply nested item', function() {
var item1 = { 'item-name': 'item-1', baz: 2 };
var item2 = { 'item-name': 'item-2', baz: 3 };
var obj = { foo: { bar: [item1, item2] } };
var upd = update(obj, 'foo.bar.{item-name:item-2}.baz', 5);

assert.equal(upd.foo.bar[1].baz, 5);
assert.notStrictEqual(upd.foo.bar[1], item2, 'updated item should not be updated in place');
assert.strictEqual(upd.foo.bar[0], item1, 'items in the collection should not be cloned');
assert.notStrictEqual(upd.foo.bar, obj.foo.bar, 'collection should not be updated in place');
assert.notStrictEqual(upd.foo, obj.foo, 'object should not be updated in place');
});
});
});

describe('update.unshift', function() {
Expand Down

0 comments on commit efc9911

Please sign in to comment.