Extended `util.parsePath` with support for all '*'

This commit is contained in:
jos 2016-04-04 21:28:23 +02:00
parent f0afcea97d
commit 8f7b656c3c
2 changed files with 6 additions and 1 deletions

View File

@ -658,7 +658,8 @@ exports.parsePath = function parsePath(jsonPath) {
throw new SyntaxError('Index expected after ['); 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); remainder = jsonPath.substr(end + 1);
} }
else { else {

View File

@ -1,6 +1,9 @@
var assert = require('assert'); var assert = require('assert');
var util = require('../src/js/util'); 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('util', function () {
describe('sanitize', function () { describe('sanitize', function () {
@ -70,6 +73,7 @@ describe('util', function () {
assert.deepEqual(util.parsePath('.foo[2]'), ['foo', 2]); assert.deepEqual(util.parsePath('.foo[2]'), ['foo', 2]);
assert.deepEqual(util.parsePath('.foo[2].bar'), ['foo', 2, 'bar']); 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["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 () { it ('should throw an exception in case of an invalid path', function () {