json-pointer
Advanced tools
Comparing version 0.1.0 to 0.2.1
{ | ||
"name": "json-pointer", | ||
"description": "Some utilities for JSON pointers described by RFC 6901", | ||
"version": "0.1.0", | ||
"version": "0.2.1", | ||
"keywords": [], | ||
"dependencies": { | ||
"manuelstofer/foreach": "*" | ||
"manuelstofer/foreach": "2.0.4" | ||
}, | ||
@@ -9,0 +9,0 @@ "development": {}, |
70
index.js
@@ -19,3 +19,3 @@ 'use strict'; | ||
function api(obj, pointer, value) { | ||
function api (obj, pointer, value) { | ||
// .set() | ||
@@ -49,3 +49,3 @@ if (arguments.length === 3) { | ||
*/ | ||
api.get = function get(obj, pointer) { | ||
api.get = function get (obj, pointer) { | ||
var tok, | ||
@@ -70,3 +70,3 @@ refTokens = api.parse(pointer); | ||
*/ | ||
api.set = function set(obj, pointer, value) { | ||
api.set = function set (obj, pointer, value) { | ||
var refTokens = api.parse(pointer), | ||
@@ -111,24 +111,10 @@ tok, | ||
* @param obj | ||
* @returns {{}} | ||
* @param {function} descend | ||
* @returns {} | ||
*/ | ||
api.dict = function dict(obj) { | ||
var results = {}, | ||
refTokens = [], | ||
mapObj = function (cur) { | ||
var type = Object.prototype.toString.call(cur); | ||
if (type === '[object Object]' || type === '[object Array]') { | ||
each(cur, function (value, key) { | ||
refTokens.push(String(key)); | ||
mapObj(value); | ||
refTokens.pop(); | ||
}); | ||
} else { | ||
results[api.compile(refTokens)] = cur; | ||
} | ||
}; | ||
mapObj(obj); | ||
api.dict = function dict (obj, descend) { | ||
var results = {}; | ||
api.walk(obj, function (value, pointer) { | ||
results[pointer] = value; | ||
}, descend); | ||
return results; | ||
@@ -142,6 +128,24 @@ }; | ||
* @param obj | ||
* @param iterator | ||
* @param {function} iterator | ||
* @param {function} descend | ||
*/ | ||
api.walk = function walk(obj, iterator) { | ||
each(api.dict(obj), iterator); | ||
api.walk = function walk (obj, iterator, descend) { | ||
var refTokens = []; | ||
descend = descend || function (value) { | ||
var type = Object.prototype.toString.call(value); | ||
return type === '[object Object]' || type === '[object Array]'; | ||
}; | ||
(function next (cur) { | ||
each(cur, function (value, key) { | ||
refTokens.push(String(key)); | ||
if (descend(value)) { | ||
next(value); | ||
} else { | ||
iterator(value, api.compile(refTokens)); | ||
} | ||
refTokens.pop(); | ||
}); | ||
}(obj)); | ||
}; | ||
@@ -156,3 +160,3 @@ | ||
*/ | ||
api.has = function has(obj, pointer) { | ||
api.has = function has (obj, pointer) { | ||
try { | ||
@@ -172,4 +176,4 @@ api.get(obj, pointer); | ||
*/ | ||
api.escape = function escape(str) { | ||
return str.replace(/~/g, '~0').replace(/\//g, '~1'); | ||
api.escape = function escape (str) { | ||
return str.toString().replace(/~/g, '~0').replace(/\//g, '~1'); | ||
}; | ||
@@ -183,3 +187,3 @@ | ||
*/ | ||
api.unescape = function unescape(str) { | ||
api.unescape = function unescape (str) { | ||
return str.replace(/~1/g, '/').replace(/~0/g, '~'); | ||
@@ -194,3 +198,3 @@ }; | ||
*/ | ||
api.parse = function parse(pointer) { | ||
api.parse = function parse (pointer) { | ||
if (pointer === '') { return []; } | ||
@@ -207,5 +211,5 @@ if (pointer.charAt(0) !== '/') { throw new Error('Invalid JSON pointer: ' + pointer); } | ||
*/ | ||
api.compile = function compile(refTokens) { | ||
api.compile = function compile (refTokens) { | ||
if (refTokens.length === 0) { return ''; } | ||
return '/' + refTokens.map(api.escape).join('/'); | ||
}; |
{ | ||
"name": "json-pointer", | ||
"description": "Some utilities for JSON pointers described by RFC 6901", | ||
"version": "0.1.0", | ||
"author": "Manuel Stofer <manuel@takimata.ch>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"foreach": "*" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1.9.0", | ||
"chai": "*", | ||
"component": "*", | ||
"mocha-phantomjs": "*" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"testling": { | ||
"harness" : "mocha", | ||
"files": "test/*.js", | ||
"browsers": [ | ||
"iexplore/6.0..latest", | ||
"firefox/3.0", | ||
"firefox/15.0..latest", | ||
"firefox/nightly", | ||
"chrome/4.0", | ||
"chrome/22.0..latest", | ||
"chrome/canary", | ||
"opera/10.0..latest", | ||
"opera/next", | ||
"safari/5.0.5..latest", | ||
"ipad/6.0..latest", | ||
"iphone/6.0..latest" | ||
] | ||
} | ||
"name": "json-pointer", | ||
"description": "Some utilities for JSON pointers described by RFC 6901", | ||
"version": "0.2.1", | ||
"author": "Manuel Stofer <manuel@takimata.ch>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"foreach": "^2.0.4" | ||
}, | ||
"homepage": "https://github.com/manuelstofer/json-pointer", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/manuelstofer/json-pointer.git" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^1.9.0", | ||
"chai": "^1.9.1" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
} | ||
} |
@@ -31,31 +31,3 @@ # 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 `object` is supplied, it 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` | ||
``` | ||
The wrapper supports chainable object oriented style. | ||
```Javascript | ||
var obj = {anything: 'bla'}; | ||
var objPointer = pointer(obj); | ||
objPointer.set('/example', 'bla').dict(); | ||
``` | ||
### .get(object, pointer) | ||
@@ -174,1 +146,20 @@ | ||
``` | ||
### pointer(object, [pointer, [value]]) | ||
Convenience wrapper around the api. | ||
```Javascript | ||
pointer(object) // bind object | ||
pointer(object, pointer) // get | ||
pointer(object, pointer, value) // set | ||
``` | ||
The wrapper supports chainable object oriented style. | ||
```Javascript | ||
var obj = {anything: 'bla'}; | ||
var objPointer = pointer(obj); | ||
objPointer.set('/example', 'bla').dict(); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Floating dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
2
413
1
0
0
16745
163
Updatedforeach@^2.0.4