Basic fork of json-pointer using Vue.set and Vue.delete to trigger Vue's reactivity system.
Note: If you're not using Vue, is safer to use the original library
$ npm install vue-json-pointer
import pointer from 'vue-json-pointer';
Looks up a JSON pointer in an object.
Array of reference tokens, e.g. returned by api.parse, can be passed as a pointer to .get, .set and .remove methods.
...
state: {
obj: {
example: {
bla: 'hello'
}
}
}
...
pointer.get(state.obj, '/example/bla');
Sets a new value on object at the location described by pointer.
...
state: {
obj: {}
}
...
pointer.set(state.obj, '/example/bla', 'hello');
Removes an attribute of object referenced by pointer.
...
state: {
obj: {
example: 'hello'
}
}
...
pointer.remove(state.obj, '/example');
// state.obj -> {}
Creates a dictionary object (pointer -> value).
var obj = {
hello: {bla: 'example'}
};
pointer.dict(state.obj);
// Returns:
// {
// '/hello/bla': 'example'
// }
Just like:
each(pointer.dict(obj), iterator);
Tests if an object has a value for a JSON pointer.
var obj = {
bla: 'hello'
};
pointer.has(obj, '/bla'); // -> true
pointer.has(obj, '/non/existing'); // -> false
Escapes a reference token.
pointer.escape('hello~bla'); // -> 'hello~0bla'
pointer.escape('hello/bla'); // -> 'hello~1bla'
Unescape a reference token.
pointer.unescape('hello~0bla'); // -> 'hello~bla'
pointer.unescape('hello~1bla'); // -> 'hello/bla'
Converts a JSON pointer into an array of reference tokens.
pointer.parse('/hello/bla'); // -> ['hello', 'bla']
Builds a json pointer from an array of reference tokens.
pointer.compile(['hello', 'bla']); // -> '/hello/bla'
Convenience wrapper around the api.
pointer(object) // bind object
pointer(object, pointer) // get
pointer(object, pointer, value) // set
The wrapper supports chainable object oriented style.
var obj = {anything: 'bla'};
var objPointer = pointer(obj);
objPointer.set('/example', 'bla').dict();