Mentions légales du service

Skip to content
Snippets Groups Projects
Commit d01c1620 authored by Adam Powers's avatar Adam Powers
Browse files

Merge pull request #50 from apowers313/master

Fixes s3u/JSONPath#49 - parsing error when bracket paths contain dot
parents 9610f9a8 a8bfb7a4
Branches
No related tags found
No related merge requests found
......@@ -92,7 +92,7 @@ JSONPath.prototype._normalize = function (expr) {
if (cache[expr]) {return cache[expr];}
var subx = [];
var normalized = expr.replace(/[\['](\??\(.*?\))[\]']/g, function ($0, $1) {return '[#' + (subx.push($1) - 1) + ']';})
.replace(/'?\.'?|\['?/g, ';')
.replace(/'?\.'?(?![^\[]*\])|\['?/g, ';')
.replace(/(?:;)?(\^+)(?:;)?/g, function ($0, ups) {return ';' + ups.split('').join(';') + ';';})
.replace(/;;;|;;/g, ';..;')
.replace(/;$|'?\]|'$/g, '');
......
var JSONPath = require('../'),
testCase = require('nodeunit').testCase
// tests based on examples at http://goessner.net/articles/JsonPath/
var json = {"store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"application/vnd.wordperfect": "sotc.wpd",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
};
module.exports = testCase({
// ============================================================================
'dot notation': function(test) {
// ============================================================================
test.expect(1);
var books = json.store.book;
var expected = [books[0].author, books[1].author, books[2].author, books[3].author];
var result = JSONPath({json: json, path: '$.store.book[*].author'});
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
'bracket notation': function(test) {
// ============================================================================
test.expect(1);
var books = json.store.book;
var expected = [books[0].author, books[1].author, books[2].author, books[3].author];
var result = JSONPath({json: json, path: "$['store']['book'][*]['author']"});
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
'bracket notation without quotes': function(test) {
// ============================================================================
test.expect(1);
var books = json.store.book;
var expected = [books[0].author, books[1].author, books[2].author, books[3].author];
var result = JSONPath({json: json, path: "$[store][book][*][author]"});
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
'mixed notation': function(test) {
// ============================================================================
test.expect(1);
var books = json.store.book;
var expected = [books[0].author, books[1].author, books[2].author, books[3].author];
var result = JSONPath({json: json, path: "$.store.book[*]['author']"});
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
'bracket notation containing dots': function(test) {
// ============================================================================
test.expect(1);
var books = json.store.book;
var expected = [books[0]["application/vnd.wordperfect"]];
var result = JSONPath({json: json, path: "$['store']['book'][*]['application/vnd.wordperfect']"});
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
'mixed notation continaing dots': function(test) {
// ============================================================================
test.expect(1);
var books = json.store.book;
var expected = [books[0]["application/vnd.wordperfect"]];
var result = JSONPath({json: json, path: "$.store.book[*]['application/vnd.wordperfect']"});
test.deepEqual(expected, result);
test.done();
},
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment