-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Support Relative JSON Pointers
- Loading branch information
Showing
9 changed files
with
276 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const assert = require('assert'); | ||
const { JsonPointer } = require('../dist'); | ||
|
||
// https://tools.ietf.org/id/draft-handrews-relative-json-pointer-00.html#rfc.section.5.1 | ||
|
||
const doc = { | ||
foo: ['bar', 'baz'], | ||
highly: { | ||
nested: { | ||
objects: true, | ||
}, | ||
}, | ||
}; | ||
|
||
const p = new JsonPointer('/foo/1'); | ||
assert(p.rel(doc, '0') == 'baz'); | ||
assert(p.rel(doc, '1/0') == 'bar'); | ||
assert(p.rel(doc, '2/highly/nested/objects') == true); | ||
assert(p.rel(doc, '0#') == 1); | ||
assert(p.rel(doc, '1#') == 'foo'); | ||
|
||
const p2 = new JsonPointer('/highly/nested'); | ||
assert(p2.rel(doc, '0/objects') == true); | ||
assert(p2.rel(doc, '1/nested/objects') == true); | ||
assert(p2.rel(doc, '2/foo/0') == 'bar'); | ||
assert(p2.rel(doc, '0#') == 'nested'); | ||
assert(p2.rel(doc, '1#') == 'highly'); | ||
|
||
// Pre-compile relative pointers to dramatically improve performance in | ||
// scenarios such as loops or when a piece of code will be frequently using | ||
// the same relative location: | ||
const compiled = p2.relative('1/nested/objects'); | ||
// ...once compiled, it is just another pointer... | ||
assert(compiled.get(doc, '1/nested/objects') == true); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters