Comparing version 0.1.1 to 0.2.0
74
index.js
@@ -50,3 +50,7 @@ /* jshint laxbreak: true, laxcomma: true*/ | ||
while (++i < len) { | ||
res.push(path[i].replace('~', '~0').replace('/', '~1')); | ||
if (typeof path[i] === 'string') { | ||
res.push(path[i].replace('~', '~0').replace('/', '~1')); | ||
} else { | ||
res.push(path[i]); | ||
} | ||
} | ||
@@ -79,3 +83,4 @@ return "/".concat(res.join('/')); | ||
while (++i < len) { | ||
res.push(encodeURIComponent(path[i].replace('~', '~0').replace('/', '~1'))); | ||
var segment = '' + path[i]; | ||
res.push(encodeURIComponent(segment.replace('~', '~0').replace('/', '~1'))); | ||
} | ||
@@ -98,2 +103,57 @@ return "#/".concat(res.join('/')); | ||
function list(obj, observe, path, key, stack, ptrs) { | ||
var i, len; | ||
path = path || []; | ||
var currentPath = path.slice(0); | ||
if (typeof key !== 'undefined') { | ||
currentPath.push(key); | ||
} | ||
var type = typeof obj; | ||
if (type === 'undefined') { | ||
return; // should only happen at the top level. | ||
} else { | ||
var ptr = encodeUriFragmentIdentifier(currentPath); | ||
if (type === 'object' && obj !== null) { | ||
stack = stack || []; | ||
ptrs = ptrs || []; | ||
var circ = stack.indexOf(obj); | ||
if (circ < 0) { | ||
stack.push(obj); | ||
ptrs.push(ptr); | ||
observe({ | ||
fragmentId: ptr, | ||
value: obj | ||
}); | ||
if (Array.isArray(obj)) { | ||
i = -1; | ||
len = obj.length; | ||
while (++i < len) { | ||
list(obj[i], observe, currentPath, i, stack, ptrs); | ||
} | ||
} else { | ||
var props = Object.getOwnPropertyNames(obj); | ||
i = -1; | ||
len = props.length; | ||
while (++i < len) { | ||
list(obj[props[i]], observe, currentPath, props[i], stack, ptrs); | ||
} | ||
} | ||
stack.length = stack.length - 1; | ||
ptrs.length = ptrs.length - 1; | ||
} else { | ||
observe({ | ||
fragmentId: ptr, | ||
value: { '$ref': ptrs[circ] }, | ||
circular: true | ||
}); | ||
} | ||
} else { | ||
observe({ | ||
fragmentId: ptr, | ||
value: obj | ||
}); | ||
} | ||
} | ||
} | ||
function get(obj, path) { | ||
@@ -221,2 +281,12 @@ if (typeof obj !== 'undefined') { | ||
}; | ||
JsonPointer.list = function (obj, observe) { | ||
var res = []; | ||
observe = observe || function (observation) { | ||
res.push(observation); | ||
}; | ||
list(obj, observe); | ||
if (res.length) { | ||
return res; | ||
} | ||
}; | ||
JsonPointer.decodePointer = decodePointer; | ||
@@ -223,0 +293,0 @@ JsonPointer.encodePointer = encodePointer; |
{ | ||
"name": "json-ptr", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": "Phillip Clark <phillip@flitbit.com>", | ||
"description": "A complete implementation of JSON Pointer (RFC 6901) for nodejs and modern browsers.", | ||
"keywords": [ | ||
"6901", | ||
"json", | ||
"pointers", | ||
"fragmentid" | ||
], | ||
"main": "index.js", | ||
@@ -7,0 +13,0 @@ "directories": { |
@@ -12,2 +12,8 @@ # json-ptr [![Build Status](https://travis-ci.org/flitbit/json-ptr.png)](http://travis-ci.org/flitbit/json-ptr) | ||
## Releases | ||
``` | ||
2014-10-21 - 0.2.0 Added #list function to enumerate all properties in a graph, producing fragmentId/value pairs. | ||
``` | ||
## Tests | ||
@@ -48,3 +54,3 @@ | ||
```html | ||
<script src="json-ptr-0.1.0.min.js"></script> | ||
<script src="json-ptr-0.2.0.min.js"></script> | ||
``` | ||
@@ -71,6 +77,21 @@ | ||
* `#path` - an array of property names used to descend the object graph from the origin to the referenced item | ||
* `#list` - lists every property in the object graph, starting from specified object – and returns each fragmentId/value pair | ||
#### #noConflict | ||
When used in an environment that has a global namespace the only variable used is `JsonPointer`. If you utilize other libraries that already occupy that name you can use the `noConflict` function to restore the obstructing value and assign `json-ptr` to another variable. | ||
```html | ||
<!-- ur codez --> | ||
<script src="/json-ptr-0.2.0.min.js"></script> | ||
<script> | ||
// At this point, JsonPointer is assigned to json-ptr's capability. | ||
var ptr = JsonPointer.noConflict(); | ||
// and now it is restored to whatever it was before the json-ptr import. | ||
</script> | ||
``` | ||
## Example | ||
This example queries the live flikr api for recent images with 'surf' and 'pipeline'. It then extracts the author and the referenced media item. | ||
@@ -77,0 +98,0 @@ |
@@ -798,2 +798,48 @@ /*jshint laxcomma: true*/ | ||
describe('when inpsecting objects', function () { | ||
var data = { | ||
a: 1, | ||
b: { | ||
c: 2 | ||
}, | ||
d: { | ||
e: [{ a:3 }, { b:4 }, { c:5 }] | ||
}, | ||
f: null | ||
}; | ||
it('#list should return all fragment-value combinations', function () { | ||
var items = ptr.list(data); | ||
expect(items[0]).to.have.keys('fragmentId', 'value'); | ||
expect(items[0].fragmentId).to.be('#'); | ||
expect(items[0].value).to.be(data); | ||
expect(items[1]).to.have.keys('fragmentId', 'value'); | ||
expect(items[1].fragmentId).to.be('#/a'); | ||
expect(items[1].value).to.be(data.a); | ||
expect(items[2]).to.have.keys('fragmentId', 'value'); | ||
expect(items[2].fragmentId).to.be('#/b'); | ||
expect(items[2].value).to.be(data.b); | ||
expect(items[3]).to.have.keys('fragmentId', 'value'); | ||
expect(items[3].fragmentId).to.be('#/b/c'); | ||
expect(items[3].value).to.be(data.b.c); | ||
// ... | ||
expect(items[9]).to.have.keys('fragmentId', 'value'); | ||
expect(items[9].fragmentId).to.be('#/d/e/1/b'); | ||
expect(items[9].value).to.be(data.d.e[1].b); | ||
// ... | ||
expect(items[12]).to.have.keys('fragmentId', 'value'); | ||
expect(items[12].fragmentId).to.be('#/f'); | ||
expect(items[12].value).to.be(data.f); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56988
16
1185
155