Permalink
Browse files

Merge pull request #35 from APIs-guru/master

Fix crash than 'apply' used for recursive object
  • Loading branch information...
dchester committed Jun 17, 2016
2 parents a06ff8a + 74c0a99 commit aca0bdcd6e75a80528049da31d4269cd6f448f16
Showing with 12 additions and 1 deletion.
  1. +4 −1 lib/index.js
  2. +8 −0 test/sugar.js
View
@@ -35,7 +35,10 @@ JSONPath.prototype.apply = function(obj, string, fn) {
assert.ok(string, "we need a path");
assert.equal(typeof fn, "function", "fn needs to be function")
var nodes = this.nodes(obj, string);
var nodes = this.nodes(obj, string).sort(function(a, b) {
//Sort node to start from inner nodes and move to the root.
return b.path.length - a.path.length;
});
nodes.forEach(function(node) {
var key = node.path.pop();
View
@@ -17,6 +17,14 @@ suite('sugar', function() {
assert.equal(data.z.a, 101);
});
test('apply method to recursive object', function() {
var data = {a: {b: [1, {c: [2,3]}]}};
jp.apply(data, '$..*[?(@.length > 1)]', function(array) {
return array.reverse();
});
assert.deepEqual(data.a.b, [{c: [3, 2]}, 1]);
});
test('value method gets us a value', function() {
var data = { a: 1, b: 2, c: 3, z: { a: 100, b: 200 } };
var b = jp.value(data, '$..b')

0 comments on commit aca0bdc

Please sign in to comment.