Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix problem with dashed key or value for lookup #1

Merged
merged 1 commit into from
Dec 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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