Skip to content

Commit

Permalink
Throw when try to set the root object
Browse files Browse the repository at this point in the history
Before this changes:
```js
var obj = {};
pointer.set(obj, '', 'bla');
console.log(JSON.stringify(obj)); //{"undefined": "bla"}
```
  • Loading branch information
IvanGoncharov committed Oct 17, 2016
1 parent daca211 commit 76ca03b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ api.set = function set (obj, pointer, value) {
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer),
nextTok = refTokens[0];

if (refTokens.length === 0) {
throw Error('Can not set the root object');
}

for (var i = 0; i < refTokens.length - 1; ++i) {
var tok = refTokens[i];
if (tok === '-' && Array.isArray(obj)) {
Expand Down
3 changes: 3 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ describe('json-api', function () {
});

describe('#set', function () {
it('should throw when try to set the root object', function () {
expect(pointer.set.bind(pointer, {}, '', 'bla')).to.throw(Error);
});

it('should set a value on an object with pointer', function () {
var obj = {
Expand Down

0 comments on commit 76ca03b

Please sign in to comment.