Comparing version 0.1.8 to 0.2.0
@@ -86,3 +86,3 @@ var aesprim = require('./aesprim'); | ||
'subscript-descendant-union': function(component, partial) { | ||
'subscript-descendant-union': function(component, partial, count) { | ||
@@ -96,2 +96,3 @@ var jp = require('..'); | ||
nodes.forEach(function(node) { | ||
if (results.length >= count) return; | ||
component.expression.value.forEach(function(component) { | ||
@@ -108,3 +109,3 @@ var _component = { operation: 'subscript', scope: 'child', expression: component.expression }; | ||
'subscript-child-filter_expression': function(component, partial) { | ||
'subscript-child-filter_expression': function(component, partial, count) { | ||
@@ -119,7 +120,7 @@ // slice out the expression from ?(expression) | ||
return this.descend(partial, null, passable); | ||
return this.descend(partial, null, passable, count); | ||
}, | ||
'subscript-descendant-filter_expression': function(component, partial) { | ||
'subscript-descendant-filter_expression': function(component, partial, count) { | ||
@@ -134,3 +135,3 @@ // slice out the expression from ?(expression) | ||
return this.traverse(partial, null, passable); | ||
return this.traverse(partial, null, passable, count); | ||
}, | ||
@@ -189,3 +190,3 @@ | ||
return function(partial, ref, passable) { | ||
return function(partial, ref, passable, count) { | ||
@@ -201,2 +202,3 @@ var value = partial.value; | ||
value.forEach(function(element, index) { | ||
if (results.length >= count) { return } | ||
if (passable(index, element, ref)) { | ||
@@ -207,2 +209,3 @@ results.push({ path: path.concat(index), value: element }); | ||
value.forEach(function(element, index) { | ||
if (results.length >= count) { return } | ||
if (recurse) { | ||
@@ -214,2 +217,3 @@ descend(element, path.concat(index)); | ||
this.keys(value).forEach(function(k) { | ||
if (results.length >= count) { return } | ||
if (passable(k, value[k], ref)) { | ||
@@ -220,2 +224,3 @@ results.push({ path: path.concat(k), value: value[k] }); | ||
this.keys(value).forEach(function(k) { | ||
if (results.length >= count) { return } | ||
if (recurse) { | ||
@@ -233,4 +238,4 @@ descend(value[k], path.concat(k)); | ||
function _descend(passable) { | ||
return function(component, partial) { | ||
return this.descend(partial, component.expression.value, passable); | ||
return function(component, partial, count) { | ||
return this.descend(partial, component.expression.value, passable, count); | ||
} | ||
@@ -240,4 +245,4 @@ } | ||
function _traverse(passable) { | ||
return function(component, partial) { | ||
return this.traverse(partial, component.expression.value, passable); | ||
return function(component, partial, count) { | ||
return this.traverse(partial, component.expression.value, passable, count); | ||
} | ||
@@ -244,0 +249,0 @@ } |
@@ -22,10 +22,2 @@ /* global toString */ | ||
JSONPath.prototype._first = function(obj, path) { | ||
assert.ok(obj instanceof Object, "obj needs to be an object"); | ||
assert.ok(Array.isArray(path), "we need a path array"); | ||
return this.query(obj, this.stringify(path)).shift(); | ||
} | ||
JSONPath.prototype.parent = function(obj, string) { | ||
@@ -38,3 +30,3 @@ | ||
var key = node.path.pop(); /* jshint unused:false */ | ||
return this._first(obj, node.path); | ||
return this.value(obj, node.path); | ||
} | ||
@@ -60,10 +52,10 @@ | ||
JSONPath.prototype.value = function(obj, string, value) { | ||
JSONPath.prototype.value = function(obj, path, value) { | ||
assert.ok(obj instanceof Object, "obj needs to be an object"); | ||
assert.ok(string, "we need a path"); | ||
assert.ok(path, "we need a path"); | ||
if (arguments.length >= 3) { | ||
var node = this.nodes(obj, string).shift(); | ||
if (!node) return this._vivify(obj, string, value); | ||
var node = this.nodes(obj, path).shift(); | ||
if (!node) return this._vivify(obj, path, value); | ||
var key = node.path.slice(-1).shift(); | ||
@@ -73,4 +65,3 @@ var parent = this.parent(obj, this.stringify(node.path)); | ||
} | ||
return this.query(obj, string)[0]; | ||
return this.query(obj, this.stringify(path), 1).shift(); | ||
} | ||
@@ -90,6 +81,6 @@ | ||
var key = path.pop(); | ||
var node = self._first(obj, path); | ||
var node = self.value(obj, path); | ||
if (!node) { | ||
setValue(path.concat(), typeof key === 'string' ? {} : []); | ||
node = self._first(obj, path); | ||
node = self.value(obj, path); | ||
} | ||
@@ -102,3 +93,3 @@ node[key] = value; | ||
JSONPath.prototype.query = function(obj, string) { | ||
JSONPath.prototype.query = function(obj, string, count) { | ||
@@ -108,3 +99,3 @@ assert.ok(obj instanceof Object, "obj needs to be an object"); | ||
var results = this.nodes(obj, string) | ||
var results = this.nodes(obj, string, count) | ||
.map(function(r) { return r.value }); | ||
@@ -115,3 +106,3 @@ | ||
JSONPath.prototype.paths = function(obj, string) { | ||
JSONPath.prototype.paths = function(obj, string, count) { | ||
@@ -121,3 +112,3 @@ assert.ok(obj instanceof Object, "obj needs to be an object"); | ||
var results = this.nodes(obj, string) | ||
var results = this.nodes(obj, string, count) | ||
.map(function(r) { return r.path }); | ||
@@ -128,3 +119,3 @@ | ||
JSONPath.prototype.nodes = function(obj, string) { | ||
JSONPath.prototype.nodes = function(obj, string, count) { | ||
@@ -134,2 +125,4 @@ assert.ok(obj instanceof Object, "obj needs to be an object"); | ||
if (count === 0) return []; | ||
var path = this.parser.parse(string); | ||
@@ -147,2 +140,3 @@ var handlers = this.handlers; | ||
if (matches.length >= count) return; | ||
var handler = handlers.resolve(component); | ||
@@ -152,4 +146,6 @@ var _partials = []; | ||
partials.forEach(function(p) { | ||
var results = handler(component, p); | ||
if (matches.length >= count) return; | ||
var results = handler(component, p, count); | ||
if (index == path.length - 1) { | ||
@@ -168,3 +164,3 @@ // if we're through the components we're done | ||
return matches; | ||
return count ? matches.slice(0, count) : matches; | ||
}; | ||
@@ -171,0 +167,0 @@ |
{ | ||
"name": "jsonpath", | ||
"description": "Query JavaScript objects with JSONPath expressions. Robust / safe JSONPath engine for Node.js.", | ||
"version": "0.1.8", | ||
"version": "0.2.0", | ||
"author": "david@fmail.co.uk", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -101,2 +101,4 @@ [![Build Status](https://travis-ci.org/dchester/jsonpath.png?branch=master)](https://travis-ci.org/dchester/jsonpath) | ||
`$..book[?(@.price<10)]` | Filter all books cheaper than 10 | ||
`$..book[?(@.price==8.95)]` | Filter all books that cost 8.95 | ||
`$..book[?(@.price<30 && @.category=="fiction")]` | Filter all fiction books cheaper than 30 | ||
`$..*` | All members of JSON structure | ||
@@ -107,5 +109,5 @@ | ||
#### jp.query(obj, pathExpression) | ||
#### jp.query(obj, pathExpression[, count]) | ||
Find elements in `obj` matching `pathExpression`. Returns an array of elements that satisfy the provided JSONPath expression, or an empty array if none were matched. | ||
Find elements in `obj` matching `pathExpression`. Returns an array of elements that satisfy the provided JSONPath expression, or an empty array if none were matched. Returns only first `count` elements if specified. | ||
@@ -117,6 +119,7 @@ ```javascript | ||
#### jp.paths(obj, pathExpression) | ||
#### jp.paths(obj, pathExpression[, count]) | ||
Find elements in `obj` matching `pathExpression`. Returns an array of element paths that satisfy the provided JSONPath expression. Each path is itself an array of keys representing the location within `obj` of the matching element. | ||
Find paths to elements in `obj` matching `pathExpression`. Returns an array of element paths that satisfy the provided JSONPath expression. Each path is itself an array of keys representing the location within `obj` of the matching element. Returns only first `count` paths if specified. | ||
```javascript | ||
@@ -132,5 +135,5 @@ var paths = jp.paths(data, '$..author'); | ||
#### jp.nodes(obj, pathExpression) | ||
#### jp.nodes(obj, pathExpression[, count]) | ||
Find elements and their corresponding paths in `obj` matching `pathExpression`. Returns an array of node objects where each node has a `path` containing an array of keys representing the location within `obj`, and a `value` pointing to the matched element. | ||
Find elements and their corresponding paths in `obj` matching `pathExpression`. Returns an array of node objects where each node has a `path` containing an array of keys representing the location within `obj`, and a `value` pointing to the matched element. Returns only first `count` nodes if specified. | ||
@@ -147,3 +150,3 @@ ```javascript | ||
#### jp.value(obj, pathExpression, [newValue]) | ||
#### jp.value(obj, pathExpression[, newValue]) | ||
@@ -150,0 +153,0 @@ Returns the value of the first element matching `pathExpression`. If `newValue` is provided, sets the value of the first matching element and returns the new value. |
@@ -89,2 +89,18 @@ var assert = require('assert'); | ||
test('first ten of all elements', function() { | ||
var results = jp.nodes(data, '$..*', 10); | ||
assert.deepEqual(results, [ | ||
{ path: [ '$', 'store' ], value: data.store }, | ||
{ path: [ '$', 'store', 'book' ], value: data.store.book }, | ||
{ path: [ '$', 'store', 'bicycle' ], value: data.store.bicycle }, | ||
{ path: [ '$', 'store', 'book', 0 ], value: data.store.book[0] }, | ||
{ path: [ '$', 'store', 'book', 1 ], value: data.store.book[1] }, | ||
{ path: [ '$', 'store', 'book', 2 ], value: data.store.book[2] }, | ||
{ path: [ '$', 'store', 'book', 3 ], value: data.store.book[3] }, | ||
{ path: [ '$', 'store', 'book', 0, 'category' ], value: 'reference' }, | ||
{ path: [ '$', 'store', 'book', 0, 'author' ], value: 'Nigel Rees' }, | ||
{ path: [ '$', 'store', 'book', 0, 'title' ], value: 'Sayings of the Century' } | ||
]) | ||
}); | ||
test('all elements', function() { | ||
@@ -91,0 +107,0 @@ var results = jp.nodes(data, '$..*'); |
@@ -58,2 +58,8 @@ var assert = require('assert'); | ||
test('paths with a count gets us back count many paths', function() { | ||
data = [ { a: [ 1, 2, 3 ], b: [ -1, -2, -3 ] }, { } ] | ||
paths = jp.paths(data, '$..*', 3) | ||
assert.deepEqual(paths, [ ['$', '0'], ['$', '1'], ['$', '0', 'a'] ]); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
384472
7664
217
30