Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose #20

Merged
merged 2 commits into from
Sep 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.')