Skip to content

Commit

Permalink
Correctly handle remove from array
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Oct 9, 2016
1 parent 1189b27 commit ae22861
Showing 1 changed file with 12 additions and 1 deletion.
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

0 comments on commit ae22861

Please sign in to comment.