Skip to content

Commit

Permalink
Merge pull request #9 from hildjj/master
Browse files Browse the repository at this point in the history
Return null when value not found
  • Loading branch information
janl committed Jan 20, 2013
2 parents f1b7967 + ea7d1ce commit 99a7865
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is an implementation of [JSON Pointer](http://tools.ietf.org/html/draft-iet
var three = jsonpointer.get(obj, "/qux/0");
var four = jsonpointer.get(obj, "/qux/1");
var five = jsonpointer.get(obj, "/qux/2");
var notfound = jsonpointer.get(obj, "/quo"); // returns null

jsonpointer.set(obj, "/foo", 6); // obj.foo = 6;

Expand Down
5 changes: 2 additions & 3 deletions jsonpointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ var untilde = function(str) {
var traverse = function(obj, pointer, value) {
// assert(isArray(pointer))
var part = untilde(pointer.shift());
if(typeof obj[part] === "undefined") {
throw("Value for pointer '" + pointer + "' not found.");
return;
if(!obj.hasOwnProperty(part)) {
return null;
}
if(pointer.length !== 0) { // keep traversin!
return traverse(obj[part], pointer, value);
Expand Down
17 changes: 7 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ assert.equal(jsonpointer.get(obj, "/d/e/2/c"), 6);

assert.equal(jsonpointer.get(obj, ""), obj);
assert.throws(function() {
assert.equal(jsonpointer.get(obj, "a"), 3);
jsonpointer.get(obj, "a");
});
assert.throws(function() {
jsonpointer.get(obj, "a/");
});

var complexKeys = {
Expand All @@ -51,19 +54,13 @@ assert.equal(jsonpointer.get(complexKeys, "/a~1b/c"), 1);
assert.equal(jsonpointer.get(complexKeys, "/d/e~1f"), 2);
assert.equal(jsonpointer.get(complexKeys, "/~01"), 3);
assert.equal(jsonpointer.get(complexKeys, "/01"), 4);
assert.throws(function() {
assert.equal(jsonpointer.get(complexKeys, "/a/b/c"), 1);
});
assert.throws(function() {
assert.equal(jsonpointer.get(complexKeys, "/~1"), 3);
});
assert.equal(jsonpointer.get(complexKeys, "/a/b/c"), null);
assert.equal(jsonpointer.get(complexKeys, "/~1"), null);

// draft-ietf-appsawg-json-pointer-08 has special array rules
var ary = [ "zero", "one", "two" ];
assert.equal(jsonpointer.get(ary, "/01"), null);

assert.throws(function() {
assert.equal(jsonpointer.get(ary, "/01"), "one");
});
//assert.equal(jsonpointer.set(ary, "/-", "three"), null);
//assert.equal(ary[3], "three");

Expand Down

0 comments on commit 99a7865

Please sign in to comment.