json-pointer-rfc6901 provides convenient methods for handling json pointers as defined by RFC6901
npm install json-pointer-rfc6901
bower install json-pointer-rfc6901
var pointer = require('json-pointer-rfc6901');
console.log(pointer({ a: 1 }, '/a'));
<html>
<head>
<script type="text/javascript" src="json-pointer-rfc6901.web.min.js"></script>
</head>
<body>
<script>
console.log(JSON.pointer({ a: 1 }, '/a');
</script>
</body>
</html>
require.config({
paths: {
"json-pointer-rfc6901": "json-pointer-rfc6901.web.min.js"
}
});
require(['json-pointer-rfc6901'], function (pointer) {
console.log(pointer({ a: 1 }, '/a');
});
Finds the value in object
as specified by pointer
.
console.log('result:', pointer.get({ a: 1 }, '/a'));
// result: 1
console.log('result:', pointer.get({ a: 1 }, '/b'));
// result: undefined
Sets the location in object
, specified by pointer
, to value
.
Returns the modified object
.
var obj = { a: 1 };
console.log('result:', pointer.set(obj, '/a', 2));
// result: { a: 2 }
console.log('result:', pointer.set(obj, '/b', 3));
// result: { a: 2, b: 3 }
console.log('result:', pointer.set(obj, '/c/0/a', 4));
// result: { a: 2, b: 3, c: [ { a: 4 } ] }
console.log('result:', pointer.set(obj, '/c/-/b', 5));
// result: { a: 2, b: 3, c: [ { a: 4 }, { b: 5 } ] }
Returns true iff the location, specified by pointer
, exists in object
.
console.log('result:', pointer.has({ a: 1 }, '/a'));
// result: true
console.log('result:', pointer.has({ a: 1 }, '/b'));
// result: false
Removes the location, specified by pointer
, from object
.
Returns the modified object
, or undefined if the pointer
is empty.
var obj = { a: 1 };
console.log('result:', pointer.del(obj, '/b'));
// result: { a: 1 }
console.log('result:', pointer.del(obj, '/a'));
// result: {}
console.log('result:', pointer.del(obj, ''));
// result: undefined
Escapes the given path segment as described by RFC6901.
Notably, '~'
's are replaced with '~0'
and '/'
's are replaced with '~1'
console.log('result:', pointer.escape('abc'));
// result: abc
console.log('result:', pointer.escape('~'));
// result: ~0
console.log('result:', pointer.escape('/'));
// result: ~1
Escapes the given path fragment segment as described by RFC6901.
Notably, '~'
's are replaced with '~0'
and '/'
's are replaced with '~1'
and finally the string is URI encoded.
console.log('result:', pointer.escapeFragment('a b'));
// result: a%20b
Un-Escapes the given path segment, reversing the actions of .escape
Notably, '~1'
's are replaced with '/'
and '~0'
's are replaced with '~'
console.log('result:', pointer.unescape('abc'));
// result: abc
console.log('result:', pointer.unescape('~0'));
// result: ~
console.log('result:', pointer.unescape('~1'));
// result: /
Un-Escapes the given path fragment segment, reversing the actions of .escapeFragment
.
Notably, the string is URI decoded and then '~1'
's are replaced with '/'
and '~0'
's are replaced with '~'
.
console.log('result:', pointer.unescapeFragment('a%20b'));
// result: a b
Returns true iff str
is a valid json pointer value
console.log('result:', pointer.isPointer('/a'));
// result: true
Returns true iff str
is a valid json fragment pointer value
console.log('result:', pointer.isFragment('#/'));
// result: true
Parses a json-pointer or json fragment pointer, as described by RFC901, into an array of path segments.
console.log('result:', pointer.parse('/abc'));
// result: [ 'abc' ]
console.log('result:', pointer.parse('#/abc'));
// result: [ 'abc' ]
Parses a json-pointer, as described by RFC901, into an array of path segments.
console.log('result:', pointer.parsePointer('/abc'));
// result: [ 'abc' ]
Parses a json-pointer or json fragment pointer, as described by RFC901, into an array of path segments.
console.log('result:', pointer.parseFragment('#/abc'));
// result: [ 'abc' ]
Converts an array of path segments into a json pointer.
This method is the reverse of .parsePointer
console.log('result:', pointer.compile([ 'abc' ]));
// result: /abc
console.log('result:', pointer.compile([ '~', '/', 'abc' ]));
// result: '/~0/~1/abc'
console.log('result:', pointer.compile([ '' ]));
// result: '/'
console.log('result:', pointer.compile([]));
// result:
Converts an array of path segments into a json pointer.
This method is the reverse of .parsePointer
console.log('result:', pointer.compilePointer([ 'abc' ]));
// result: /abc
Converts an array of path segments into a json pointer.
This method is the reverse of .compileFragment
console.log('result:', pointer.compileFragment([ 'abc' ]));
// result: #/abc
Convenience function for choosing between .smartBind
, .get
, and .set
, depending on the number of arguments.
var obj = { a: 1 };
console.log('result:', pointer(obj, '/a', 2));
// result: { a: 2 }
console.log('result:', pointer(obj, '/a'));
// result: 2
var bound = pointer(obj);
console.log('result:', bound.get('/a'));
// result: 2
console.log('result:', bound('/a'));
// result: 2
console.log('result:', bound.set('/a', 3));
// result: { a: 3 }
console.log('result:', bound('/a', 4));
// result: { a: 4 }
Creates a clone of the api, with ./.get/.has/.set/.del/.smartBind
method signatures adjusted.
var obj = { a: 1 };
console.log('result:', pointer.smartBind({ object: obj })('/a'));
// result: 1
console.log('result:', pointer.smartBind({ object: obj }).get('/a'));
// result: 1
console.log('result:', pointer.smartBind({ pointer: '/a' }).get(obj));
// result: 1
console.log('result:', pointer.smartBind({ object: obj }).smartBind({ pointer: '/a' }).get());
// result: 1
get/set bound pointer value
get/set bound pointer value as fragment
get/set bound object
get/set bound options
Returns true iff obj
contains key
and obj
is either an Array or an Object.
Ignores the prototype chain.
Default value for options.hasProp
.
Returns true iff obj
contains key
, disregarding the prototype chain.
Returns true iff obj
contains key
, including via the prototype chain.
Finds the given key
in obj
.
Default value for options.getProp
.
Sets the given key
in obj
to value
.
Default value for options.setProp
.
Returns the value to use when .get
fails to locate a pointer segment.
Default value for options.getNotFound
.
Returns the value to use when .set
fails to locate a pointer segment.
Default value for options.setNotFound
.
Performs an action when .del
fails to locate a pointer segment.
Default value for options.delNotFound
.
Raises a JsonPointerError when the given pointer segment is not found.
May be used in place of the above methods via the options
argument of ./.get/.set/.has/.del/.simpleBind
.
try {
console.log('result:', pointer.get({a: 1}, '/b', {getNotFound: pointer.errorNotFound}));
}
catch (ex)
{
console.log(ex.name, ':', ex.message);
}
// exception: JsonPointerError : Unable to find json path: /b
console.log('result:', pointer.get([1,2,3], '/length'));
// result: undefined
console.log('result:', pointer.get([1,2,3], '/length', { hasProp: pointer.hasOwnProp }));
// result: 3
console.log('result:', pointer.get([1,2,3], '/length', { hasProp: pointer.hasProp }));
// result: 3
console.log('result:', pointer.get([1,2,3], '/push'));
// result: undefined
console.log('result:', pointer.get([1,2,3], '/push', { hasProp: pointer.hasOwnProp }));
// result: undefined
console.log('result:', pointer.get([1,2,3], '/push', { hasProp: pointer.hasProp }));
// result: function push() { [native code] }