Skip to content

Commit

Permalink
Extended util.parsePath with support for all '*'
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Apr 4, 2016
1 parent f0afcea commit 8f7b656
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ exports.parsePath = function parsePath(jsonPath) {
throw new SyntaxError('Index expected after [');
}

prop = JSON.parse(jsonPath.substring(1, end));
var value = jsonPath.substring(1, end);
prop = value === '*' ? value : JSON.parse(value); // parse string and number
remainder = jsonPath.substr(end + 1);
}
else {
Expand Down
4 changes: 4 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var assert = require('assert');
var util = require('../src/js/util');

// console.log('TEST', util.parsePath('.items[3].name'));
// console.log('TEST', util.parsePath('.items[*].name'));

describe('util', function () {

describe('sanitize', function () {
Expand Down Expand Up @@ -70,6 +73,7 @@ describe('util', function () {
assert.deepEqual(util.parsePath('.foo[2]'), ['foo', 2]);
assert.deepEqual(util.parsePath('.foo[2].bar'), ['foo', 2, 'bar']);
assert.deepEqual(util.parsePath('.foo["prop with spaces"]'), ['foo', 'prop with spaces']);
assert.deepEqual(util.parsePath('.foo[*].bar'), ['foo', '*', 'bar']);
});

it ('should throw an exception in case of an invalid path', function () {
Expand Down

0 comments on commit 8f7b656

Please sign in to comment.