Socket
Socket
Sign inDemoInstall

json-pointer

Package Overview
Dependencies
1
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 0.0.3

2

component.json
{
"name": "json-pointer",
"description": "Some utilities for JSON pointers described by RFC 6901",
"version": "0.0.2",
"version": "0.0.3",
"keywords": [],

@@ -6,0 +6,0 @@ "dependencies": {

'use strict';
var each = require('foreach');
module.exports = api;
var api = module.exports = {
/**
* Lookup a json object in an object
*
* @param obj
* @param pointer
* @returns {*}
*/
get: function (obj, pointer) {
return api.lookup(obj, api.parse(pointer));
},
/**
* Convenience wrapper around the api.
* Calls `.get` when called with an `object` and a `pointer`.
* 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 value
* @returns {*}
*/
/**
* Sets a value on an object
*
* @param obj
* @param pointer
* @param value
*/
set: function (obj, pointer, value) {
var refTokens = api.parse(pointer),
tok,
nextTok;
while (refTokens.length > 1) {
tok = refTokens.shift();
nextTok = refTokens[0];
function api(obj, pointer, value) {
// .set()
if (arguments.length === 3) {
return api.set(obj, pointer, value);
}
// .get()
if (arguments.length === 2) {
return api.get(obj, pointer);
}
// Return a partially applied function on `obj`.
return api.bind(api, obj);
}
if (!obj.hasOwnProperty(tok)) {
if (nextTok.match(/^\d+$/)) {
obj[tok] = [];
} else {
obj[tok] = {};
}
}
obj = obj[tok];
/**
* Lookup a json pointer in an object
*
* @param obj
* @param pointer
* @returns {*}
*/
api.get = function get(obj, pointer) {
var tok,
refTokens = api.parse(pointer);
while (refTokens.length) {
tok = refTokens.shift();
if (!obj.hasOwnProperty(tok)) {
throw new Error('Invalid reference token:' + tok);
}
obj[nextTok] = value;
},
obj = obj[tok];
}
return obj;
};
/**
* Returns a (pointer -> value) dictionary for an object
*
* @param obj
* @returns {{}}
*/
dict: function (obj) {
var results = {},
refTokens = [],
/**
* Sets a value on an object
*
* @param obj
* @param pointer
* @param value
*/
api.set = function set(obj, pointer, value) {
var refTokens = api.parse(pointer),
tok,
nextTok;
while (refTokens.length > 1) {
tok = refTokens.shift();
nextTok = refTokens[0];
mapObj = function (cur) {
var type = Object.prototype.toString.call(cur);
if (type === '[object Object]' || type === '[object Array]') {
if (!obj.hasOwnProperty(tok)) {
if (nextTok.match(/^\d+$/)) {
obj[tok] = [];
} else {
obj[tok] = {};
}
}
obj = obj[tok];
}
obj[nextTok] = value;
};
each(cur, function (value, key) {
refTokens.push(key);
mapObj(value);
refTokens.pop();
});
/**
* Returns a (pointer -> value) dictionary for an object
*
* @param obj
* @returns {{}}
*/
api.dict = function dict(obj) {
var results = {},
refTokens = [],
} else {
results[api.compile(refTokens)] = cur;
}
};
mapObj = function (cur) {
var type = Object.prototype.toString.call(cur);
if (type === '[object Object]' || type === '[object Array]') {
mapObj(obj);
return results;
},
each(cur, function (value, key) {
refTokens.push(key);
mapObj(value);
refTokens.pop();
});
/**
* Iterates over an object
* Iterator: function (value, pointer) {}
*
* @param obj
* @param iterator
*/
walk: function (obj, iterator) {
each(api.dict(obj), iterator);
},
} else {
results[api.compile(refTokens)] = cur;
}
};
/**
* Tests if an object has a value for a json pointer
*
* @param obj
* @param pointer
* @returns {boolean}
*/
has: function (obj, pointer) {
try {
api.lookup(obj, api.parse(pointer));
} catch (e) {
return false;
}
return true;
},
mapObj(obj);
return results;
};
/**
* Looks up a value at location described by a reference token array
*
* @not-so-public
*
* @param obj
* @param refTokens
* @returns {*}
*/
lookup: function (obj, refTokens) {
var tok;
while (refTokens.length) {
tok = refTokens.shift();
if (!obj.hasOwnProperty(tok)) {
throw new Error('Invalid reference token:' + tok);
}
obj = obj[tok];
}
return obj;
},
/**
* Iterates over an object
* Iterator: function (value, pointer) {}
*
* @param obj
* @param iterator
*/
api.walk = function walk(obj, iterator) {
each(api.dict(obj), iterator);
};
/**
* Escapes a reference token
*
* @param str
* @returns {string}
*/
escape: function (str) {
return str.replace(/~/g, '~0').replace(/\//g, '~1');
},
/**
* Tests if an object has a value for a json pointer
*
* @param obj
* @param pointer
* @returns {boolean}
*/
api.has = function has(obj, pointer) {
try {
api.get(obj, api.parse(pointer));
} catch (e) {
return false;
}
return true;
};
/**
* Unescapes a reference token
*
* @param str
* @returns {string}
*/
unescape: function (str) {
return str.replace(/~1/g, '/').replace(/~0/g, '~');
},
/**
* Escapes a reference token
*
* @param str
* @returns {string}
*/
api.escape = function escape(str) {
return str.replace(/~/g, '~0').replace(/\//g, '~1');
};
/**
* Converts a json pointer into a array of reference tokens
*
* @param pointer
* @returns {Array}
*/
parse: function (pointer) {
if (pointer === '') { return []; }
if (pointer.charAt(0) !== '/') { throw new Error('Invalid JSON pointer:' + pointer); }
return pointer.substring(1).split(/\//).map(api.unescape);
},
/**
* Unescapes a reference token
*
* @param str
* @returns {string}
*/
api.unescape = function unescape(str) {
return str.replace(/~1/g, '/').replace(/~0/g, '~');
};
/**
* Builds a json pointer from a array of reference tokens
*
* @param refTokens
* @returns {string}
*/
compile: function (refTokens) {
return '/' + refTokens.map(api.escape).join('/');
}
/**
* Converts a json pointer into a array of reference tokens
*
* @param pointer
* @returns {Array}
*/
api.parse = function parse(pointer) {
if (pointer === '') { return []; }
if (pointer.charAt(0) !== '/') { throw new Error('Invalid JSON pointer:' + pointer); }
return pointer.substring(1).split(/\//).map(api.unescape);
};
/**
* Builds a json pointer from a array of reference tokens
*
* @param refTokens
* @returns {string}
*/
api.compile = function compile(refTokens) {
return '/' + refTokens.map(api.escape).join('/');
};
{
"name": "json-pointer",
"description": "Some utilities for JSON pointers described by RFC 6901",
"version": "0.0.2",
"version": "0.0.3",
"author": "Manuel Stofer <manuel@takimata.ch>",

@@ -6,0 +6,0 @@ "license": "MIT",

@@ -29,3 +29,22 @@ # json-pointer

### pointer(object, [pointer, [value]])
Convenience wrapper around the api.
Calls `.get` when called with an `object` and a `pointer`.
Calls `.set` when also called with `value`.
If only supplied `object`, returns a partially applied function, mapped to the object.
```Javascript
var obj = {
existing: 'bla'
};
pointer(obj, '/new-value/bla', 'expected'); // .set a property
var objPointer = pointer(obj); // all api calls are now scoped to `obj`
objPointer('/existing') // gets '/existing' from `obj`
objPointer('/new-value/bla') // gets '/new-value/bla' from `obj`
```
### .get(object, pointer)

@@ -32,0 +51,0 @@

@@ -107,1 +107,30 @@ /*global describe, it*/

describe('convenience api wrapper', function() {
it('should call #get when passed 2 args', function() {
var obj = {
existing: 'expected'
};
pointer(obj, '/existing');
obj['existing'].should.equal('expected');
});
it('should call #set when passed 3 args', function() {
var obj = {
existing: 'bla'
};
pointer(obj, '/new-value/bla', 'expected');
obj['new-value'].bla.should.equal('expected');
});
it('should return a partially applied function when passed 1 arg', function() {
var obj = {
existing: 'bla'
};
var objPointer = pointer(obj);
objPointer('/new-value/bla', 'expected');
objPointer('/new-value').bla.should.equal('expected');
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc