Skip to content

Commit

Permalink
Merge pull request #23 from APIs-guru/master
Browse files Browse the repository at this point in the history
Correctly handle remove from an array
  • Loading branch information
manuelstofer committed Oct 17, 2016
2 parents 6355037 + 528127b commit cbf06fb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ api.remove = function (obj, pointer) {
if (finalToken === undefined) {
throw new Error('Invalid JSON pointer for remove: "' + pointer + '"');
}
delete api.get(obj, refTokens.slice(0, -1))[finalToken];

var parent = api.get(obj, refTokens.slice(0, -1));
if (Array.isArray(parent)) {
var index = +finalToken;
if (finalToken === '' && isNaN(index)) {
throw new Error('Invalid array index: "' + finalToken + '"');
}

Array.prototype.splice.call(parent, index, 1);
} else {
delete parent[finalToken];
}
};

/**
Expand Down
53 changes: 39 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,24 +177,49 @@ describe('json-api', function () {

describe('#remove', function () {
each(Object.keys(rfcValues), function (p) {
if (p !== '') {
it('should work for "' + p + '"', function () {
pointer.remove(rfcExample, p);
expect(pointer.get.bind(pointer, rfcExample, p)).to.throw(Error);
});
}
if (p === '' || p === '/foo/0') return;

it('should work for "' + p + '"', function () {
pointer.remove(rfcExample, p);
expect(pointer.get.bind(pointer, rfcExample, p)).to.throw(Error);
});
});

it('should work for "/foo/0"', function () {
var p = '/foo/0';
pointer.remove(rfcExample, p);
expect(pointer.get(rfcExample, p)).to.equal('baz');
});

it('should work for "/foo/1"', function () {
var p = '/foo/1';
pointer.remove(rfcExample, p);
expect(pointer.get.bind(pointer, rfcExample, p)).to.throw(Error);
});

each(Object.keys(rfcParsed), function (p) {
if (p !== '') {
it('should work for ' + JSON.stringify(rfcParsed[p].tokens), function () {
pointer.remove(rfcExample, immutable(rfcParsed[p].tokens));
expect(function() {
pointer.get(pointer, rfcExample, immutable(rfcParsed[p].tokens));
}).to.throw(Error);
});
}
if (p === '' || p === '/foo/0') return;

it('should work for ' + JSON.stringify(rfcParsed[p].tokens), function () {
pointer.remove(rfcExample, immutable(rfcParsed[p].tokens));
expect(function() {
pointer.get(rfcExample, immutable(rfcParsed[p].tokens));
}).to.throw(Error);
});
});

it('should work for ["foo","0"]', function () {
var p = immutable(['foo', '0']);
pointer.remove(rfcExample, p);
expect(pointer.get(rfcExample, p)).to.equal('baz');
});

it('should work for ["foo","1"]', function () {
var p = immutable(['foo', '1']);
pointer.remove(rfcExample, p);
expect(pointer.get.bind(pointer, rfcExample, p)).to.throw(Error);
});

});

describe('#dict', function () {
Expand Down

0 comments on commit cbf06fb

Please sign in to comment.