Skip to content

Commit

Permalink
accept array of reference tokens as a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Feb 20, 2016
1 parent 645e7da commit 8d10eaf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var pointer = require('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.

```Javascript
var obj = {
example: {
Expand All @@ -55,7 +57,7 @@ pointer.set(obj, '/example/bla', 'hello');

### .remove(object, pointer)

Removes an attribute of object referenced by pointer
Removes an attribute of object referenced by pointer.

```Javascript
var obj = {
Expand Down
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = api;
* Calls `.set` when also called with `value`.
* If only supplied `object`, returns a partially applied function, mapped to the object.
*
* @param obj
* @param pointer
* @param {Object} obj
* @param {String|Array} pointer
* @param value
* @returns {*}
*/
Expand Down Expand Up @@ -41,13 +41,13 @@ function api (obj, pointer, value) {
/**
* Lookup a json pointer in an object
*
* @param obj
* @param pointer
* @param {Object} obj
* @param {String|Array} pointer
* @returns {*}
*/
api.get = function get (obj, pointer) {
var tok,
refTokens = api.parse(pointer);
refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer);
while (refTokens.length) {
tok = refTokens.shift();
if (!(typeof obj == 'object' && tok in obj)) {
Expand All @@ -61,12 +61,12 @@ api.get = function get (obj, pointer) {
/**
* Sets a value on an object
*
* @param obj
* @param pointer
* @param {Object} obj
* @param {String|Array} pointer
* @param value
*/
api.set = function set (obj, pointer, value) {
var refTokens = api.parse(pointer),
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer),
tok,
nextTok = refTokens[0];
while (refTokens.length > 1) {
Expand Down Expand Up @@ -95,11 +95,11 @@ api.set = function set (obj, pointer, value) {
/**
* Removes an attribute
*
* @param obj
* @param pointer
* @param {Object} obj
* @param {String|Array} pointer
*/
api.remove = function (obj, pointer) {
var refTokens = api.parse(pointer);
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer);
var finalToken = refTokens.pop();
if (finalToken === undefined) {
throw new Error('Invalid JSON pointer for remove: "' + pointer + '"');
Expand Down

0 comments on commit 8d10eaf

Please sign in to comment.