Skip to content

Commit

Permalink
Adopt strictEqual changes and only return null when the get succeeded
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbachmann committed Jul 13, 2022
1 parent bad4983 commit 4a253c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
3 changes: 1 addition & 2 deletions jsonpointer.js
Expand Up @@ -72,8 +72,7 @@ function get (obj, pointer) {
for (var p = 1; p < len;) {
obj = obj[untilde(pointer[p++])]
if (len === p) return obj
if (typeof obj !== 'object') return undefined
if (obj === null) return null
if (typeof obj !== 'object' || obj === null) return undefined
}
}

Expand Down
14 changes: 6 additions & 8 deletions test.js
Expand Up @@ -12,8 +12,8 @@ var obj = {
nullValue: null
}

assert.equal(jsonpointer.get(obj, '/nullValue'), null)
assert.equal(jsonpointer.get(obj, '/nullValue/e'), null)
assert.strictEqual(jsonpointer.get(obj, '/nullValue'), null)
assert.strictEqual(jsonpointer.get(obj, '/nullValue/e'), undefined)

// set returns old value
assert.strictEqual(jsonpointer.set(obj, '/a', 2), 1)
Expand Down Expand Up @@ -119,18 +119,16 @@ assert.strictEqual(jsonpointer.get(example, '/ '), 7)
assert.strictEqual(jsonpointer.get(example, '/m~0n'), 8)

// jsonpointer.compile(path)
var a = { foo: 'bar' }
var a = { foo: 'bar', foo2: null }
var pointer = jsonpointer.compile('/foo')
assert.strictEqual(pointer.get(a), 'bar')
assert.strictEqual(pointer.set(a, 'test'), 'bar')
assert.strictEqual(pointer.get(a), 'test')
assert.deepEqual(a, { foo: 'test' })
assert.deepEqual(a, { foo: 'test', foo2: null })


// compile read null value
var compileWithNullValue = { foo: 'bar' }
// Read subproperty of null value
var pointerNullValue = jsonpointer.compile('/foo2/baz')
assert.equal(pointer.get(pointerNullValue), null)
assert.strictEqual(pointerNullValue.get(a), undefined)

var b = {}
jsonpointer.set({}, '/constructor/prototype/boo', 'polluted')
Expand Down

0 comments on commit 4a253c0

Please sign in to comment.