Skip to content

Commit

Permalink
Merge pull request #20 from marcbachmann/compose
Browse files Browse the repository at this point in the history
jsonpointer.compile(pointer) feature
  • Loading branch information
marcbachmann committed Sep 29, 2015
2 parents cabb37d + 781cdd0 commit dc1e2c6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
This is an implementation of [JSON Pointer](http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-08).

## Usage

var jsonpointer = require("jsonpointer");
var obj = { foo: 1, bar: { baz: 2}, qux: [3, 4, 5]};
var one = jsonpointer.get(obj, "/foo");
var two = jsonpointer.get(obj, "/bar/baz");
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;
```javascript
var jsonpointer = require('jsonpointer');
var obj = { foo: 1, bar: { baz: 2}, qux: [3, 4, 5]};

jsonpointer.get(obj, '/foo'); // returns 1
jsonpointer.get(obj, '/bar/baz'); // returns 2
jsonpointer.get(obj, '/qux/0'); // returns 3
jsonpointer.get(obj, '/qux/1'); // returns 4
jsonpointer.get(obj, '/qux/2'); // returns 5
jsonpointer.get(obj, '/quo'); // returns null

jsonpointer.set(obj, '/foo', 6); // sets obj.foo = 6;
jsonpointer.set(obj, '/qux/-', 6) // sets obj.qux = [3, 4, 5, 6]

pointer = jsonpointer.compile('/foo')
pointer.get(obj) // returns 1
pointer.set(obj, 1) // sets obj.foo = 1
```

## Testing

Expand Down
18 changes: 18 additions & 0 deletions jsonpointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ var compilePointer = function (pointer) {
if (!(pointer instanceof Array)) {
pointer = pointer.split('/')
if (pointer.shift() !== '') throw new Error('Invalid JSON pointer.')
} else {
// Clone the pointer array
var newPointer = []
for (var i = 0; i < pointer.length; i++) newPointer[i] = pointer[i]
pointer = newPointer
}

return pointer
Expand Down Expand Up @@ -94,5 +99,18 @@ var set = function (obj, pointer, value) {
return traverse(obj, pointer, value)
}

var compile = function (pointer) {
var compiled = compilePointer(pointer)
return {
get: function (object) {
return get(object, compiled)
},
set: function (object, value) {
return set(object, compiled, value)
}
}
}

exports.get = get
exports.set = set
exports.compile = compile
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,12 @@ assert.equal(jsonpointer.get(example, '/k\'l'), 6)
assert.equal(jsonpointer.get(example, '/ '), 7)
assert.equal(jsonpointer.get(example, '/m~0n'), 8)

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

console.log('All tests pass.')

0 comments on commit dc1e2c6

Please sign in to comment.