Skip to content

Commit

Permalink
handle numeric literal members referring to like string keys; fixes d…
Browse files Browse the repository at this point in the history
  • Loading branch information
dchester committed Nov 23, 2016
1 parent d10c6c4 commit c469537
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Handlers.prototype._fns = {
'subscript-child-numeric_literal':
_descend(function(key, value, ref) { return key === ref }),

'member-child-numeric_literal':
_descend(function(key, value, ref) { return String(key) === String(ref) }),

'subscript-descendant-numeric_literal':
_traverse(function(key, value, ref) { return key === ref }),

Expand Down Expand Up @@ -150,9 +153,6 @@ Handlers.prototype._fns = {
Handlers.prototype._fns['subscript-child-string_literal'] =
Handlers.prototype._fns['member-child-identifier'];

Handlers.prototype._fns['member-child-numeric_literal'] =
Handlers.prototype._fns['subscript-child-numeric_literal'];

Handlers.prototype._fns['member-descendant-numeric_literal'] =
Handlers.prototype._fns['subscript-descendant-string_literal'] =
Handlers.prototype._fns['member-descendant-identifier'];
Expand Down
6 changes: 6 additions & 0 deletions test/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ suite('query', function() {
assert.deepEqual(results, [ { path: [ '$', 'store', 'book', 0 ], value: data.store.book[0] } ]);
});

test('member numeric literal matches string-numeric key', function() {
var data = { authors: { '1': 'Herman Melville', '2': 'J. R. R. Tolkien' } };
var results = jp.nodes(data, '$.authors.1');
assert.deepEqual(results, [ { path: [ '$', 'authors', 1 ], value: 'Herman Melville' } ]);
});

test('descendant numeric literal gets first element', function() {
var results = jp.nodes(data, '$.store.book..0');
assert.deepEqual(results, [ { path: [ '$', 'store', 'book', 0 ], value: data.store.book[0] } ]);
Expand Down

0 comments on commit c469537

Please sign in to comment.