diff --git a/README.md b/README.md index 5b8c779f0f5e577e8ee97fd3f7dc25077255234e..81866022a47b7f82774c668725fdb7c4942362e3 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ Usage In node.js: ```js -var jsonPath = require('JSONPath'); -jsonPath.eval(obj, path); +var JSONPath = require('JSONPath'); +JSONPath({json: obj, path: path}); ``` For browser usage you can directly include `lib/jsonpath.js`, no browserify @@ -24,7 +24,7 @@ magic necessary: ```html <script src="lib/jsonpath.js"></script> <script> - jsonPath.eval(obj, path); + JSONPath({json: obj, path: path}); </script> ``` diff --git a/test/test.arr.js b/test/test.arr.js index 4314fd236d4b61fd024e260948164fd887d5a0b5..d206071ee6f20bca2dde1c633cba6c54fc9c8519 100644 --- a/test/test.arr.js +++ b/test/test.arr.js @@ -1,4 +1,4 @@ -var jsonpath = require("../").eval, +var JSONPath = require('../'), testCase = require('nodeunit').testCase var json = { @@ -19,16 +19,16 @@ var json = { }; module.exports = testCase({ - "get single": function (test) { + 'get single': function (test) { var expected = json.store.book; - var result = jsonpath(json, "store.book", {flatten: true, wrap: false}); + var result = JSONPath({json: json, path: 'store.book', flatten: true, wrap: false}); test.deepEqual(expected, result); test.done(); }, - "get arr": function (test) { + 'get arr': function (test) { var expected = json.store.books; - var result = jsonpath(json, "store.books", {flatten: true, wrap: false}); + var result = JSONPath({json: json, path: 'store.books', flatten: true, wrap: false}); test.deepEqual(expected, result); test.done(); } diff --git a/test/test.at_and_dollar.js b/test/test.at_and_dollar.js index e99d7dbf6d6770ab57a257f75c5372bed5c547cb..9c0eb42fae2f08aac4ed16e0d93cf20f2d253ef6 100644 --- a/test/test.at_and_dollar.js +++ b/test/test.at_and_dollar.js @@ -1,5 +1,5 @@ -var jsonpath = require("../").eval - , testCase = require('nodeunit').testCase +var JSONPath = require('../'), + testCase = require('nodeunit').testCase var t1 = { @@ -22,29 +22,29 @@ module.exports = testCase({ // ============================================================================ - "test undefined, null": function(test) { + 'test undefined, null': function(test) { // ============================================================================ test.expect(5); - test.equal(undefined, jsonpath(undefined, "foo")); - test.equal(null, jsonpath(null, "foo")); - test.equal(undefined, jsonpath({}, "foo")[0]); - test.equal(undefined, jsonpath({ a: "b" }, "foo")[0]); - test.equal(undefined, jsonpath({ a: "b" }, "foo")[100]); + test.equal(undefined, JSONPath({json: undefined, path: 'foo'})); + test.equal(null, JSONPath({json: null, path: 'foo'})); + test.equal(undefined, JSONPath({json: {}, path: 'foo'})[0]); + test.equal(undefined, JSONPath({json: { a: 'b' }, path: 'foo'})[0]); + test.equal(undefined, JSONPath({json: { a: 'b' }, path: 'foo'})[100]); test.done(); }, // ============================================================================ - "test $ and @": function(test) { + 'test $ and @': function(test) { // ============================================================================ test.expect(7); - test.equal(t1["$"], jsonpath(t1, "\$")[0]); - test.equal(t1["$"], jsonpath(t1, "$")[0]); - test.equal(t1["a$a"], jsonpath(t1, "a$a")[0]); - test.equal(t1["@"], jsonpath(t1, "\@")[0]); - test.equal(t1["@"], jsonpath(t1, "@")[0]); - test.equal(t1["$"]["@"], jsonpath(t1, "$.$.@")[0]); - test.equal(undefined, jsonpath(t1, "\@")[1]); + test.equal(t1['$'], JSONPath({json: t1, path: '\$'})[0]); + test.equal(t1['$'], JSONPath({json: t1, path: '$'})[0]); + test.equal(t1['a$a'], JSONPath({json: t1, path: 'a$a'})[0]); + test.equal(t1['@'], JSONPath({json: t1, path: '\@'})[0]); + test.equal(t1['@'], JSONPath({json: t1, path: '@'})[0]); + test.equal(t1['$']['@'], JSONPath({json: t1, path: '$.$.@'})[0]); + test.equal(undefined, JSONPath({json: t1, path: '\@'})[1]); test.done(); } diff --git a/test/test.eval.js b/test/test.eval.js index 8da68e90879e4bc7af97071482a150c92c893a4e..98b3459844f5f7cba4e4d633d38ff6b0f7e43eea 100644 --- a/test/test.eval.js +++ b/test/test.eval.js @@ -1,4 +1,4 @@ -var jsonpath = require("../").eval, +var JSONPath = require('../'), testCase = require('nodeunit').testCase var json = { @@ -26,19 +26,19 @@ var json = { module.exports = testCase({ - "multi statement eval": function (test) { + 'multi statement eval': function (test) { var expected = json.store.books[0]; - var selector = "$..[?(" - + "var sum = @.price && @.price[0]+@.price[1];" - + "sum > 20;)]" - var result = jsonpath(json, selector, {wrap: false}); + var selector = '$..[?(' + + 'var sum = @.price && @.price[0]+@.price[1];' + + 'sum > 20;)]' + var result = JSONPath({json: json, path: selector, wrap: false}); test.deepEqual(expected, result); test.done(); }, - "accessing current path": function (test) { + 'accessing current path': function (test) { var expected = json.store.books[1]; - var result = jsonpath(json, "$..[?(@path==\"$['store']['books'][1]\")]", {wrap: false}); + var result = JSONPath({json: json, path: "$..[?(@path==\"$['store']['books'][1]\")]", wrap: false}); test.deepEqual(expected, result); test.done(); } diff --git a/test/test.examples.js b/test/test.examples.js index 053272568478962fde72051223f76427f7679b56..a28e3bbfa2b9d121d521151f70db5847b79c2060 100644 --- a/test/test.examples.js +++ b/test/test.examples.js @@ -1,10 +1,10 @@ -var jsonpath = require("../").eval - , testCase = require('nodeunit').testCase +var JSONPath = require('../'), + testCase = require('nodeunit').testCase // tests based on examples at http://goessner.net/articles/JsonPath/ var json = {"store": { - "book": [ + "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", @@ -37,123 +37,123 @@ var json = {"store": { module.exports = testCase({ - - // ============================================================================ - "wildcards": function(test) { - // ============================================================================ + + // ============================================================================ + 'wildcards': 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, "$.store.book[*].author"); + var result = JSONPath({json: json, path: '$.store.book[*].author'}); test.deepEqual(expected, result); - + test.done(); }, - - // ============================================================================ - "all properties, entire tree": function(test) { - // ============================================================================ + + // ============================================================================ + 'all properties, entire tree': 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, "$..author"); + var result = JSONPath({json: json, path: '$..author'}); test.deepEqual(expected, result); - + test.done(); }, - - // ============================================================================ - "all sub properties, single level": function(test) { - // ============================================================================ + + // ============================================================================ + 'all sub properties, single level': function(test) { + // ============================================================================ test.expect(1); var expected = [json.store.book, json.store.bicycle]; - var result = jsonpath(json, "$.store.*"); + var result = JSONPath({json: json, path: '$.store.*'}); test.deepEqual(expected, result); - + test.done(); }, - // ============================================================================ - "all sub properties, entire tree": function(test) { - // ============================================================================ + // ============================================================================ + 'all sub properties, entire tree': function(test) { + // ============================================================================ test.expect(1); var books = json.store.book; var expected = [books[0].price, books[1].price, books[2].price, books[3].price, json.store.bicycle.price]; - var result = jsonpath(json, "$.store..price"); + var result = JSONPath({json: json, path: '$.store..price'}); test.deepEqual(expected, result); - + test.done(); }, - - // ============================================================================ - "n property of entire tree": function(test) { - // ============================================================================ + + // ============================================================================ + 'n property of entire tree': function(test) { + // ============================================================================ test.expect(1); var books = json.store.book; var expected = [books[2]]; - var result = jsonpath(json, "$..book[2]"); + var result = JSONPath({json: json, path: '$..book[2]'}); test.deepEqual(expected, result); - + test.done(); }, - // ============================================================================ - "last property of entire tree": function(test) { - // ============================================================================ + // ============================================================================ + 'last property of entire tree': function(test) { + // ============================================================================ test.expect(2); var books = json.store.book; var expected = [books[3]]; - var result = jsonpath(json, "$..book[(@.length-1)]"); + var result = JSONPath({json: json, path: '$..book[(@.length-1)]'}); test.deepEqual(expected, result); - - result = jsonpath(json, "$..book[-1:]"); + + result = JSONPath({json: json, path: '$..book[-1:]'}); test.deepEqual(expected, result); - + test.done(); }, - - // ============================================================================ - "range of property of entire tree": function(test) { - // ============================================================================ + + // ============================================================================ + 'range of property of entire tree': function(test) { + // ============================================================================ test.expect(2); var books = json.store.book; var expected = [books[0], books[1]]; - var result = jsonpath(json, "$..book[0,1]"); + var result = JSONPath({json: json, path: '$..book[0,1]'}); test.deepEqual(expected, result); - - result = jsonpath(json, "$..book[:2]"); + + result = JSONPath({json: json, path: '$..book[:2]'}); test.deepEqual(expected, result); - + test.done(); }, - - // ============================================================================ - "filter all properties if sub property exists,o entire tree": function(test) { - // ============================================================================ + + // ============================================================================ + 'filter all properties if sub property exists, of entire tree': function(test) { + // ============================================================================ test.expect(1); var books = json.store.book; var expected = [books[2], books[3]]; - var result = jsonpath(json, "$..book[?(@.isbn)]"); + var result = JSONPath({json: json, path: '$..book[?(@.isbn)]'}); test.deepEqual(expected, result); - + test.done(); }, - - // ============================================================================ - "filter all properties if sub property greater than of entire tree": function(test) { - // ============================================================================ + + // ============================================================================ + 'filter all properties if sub property greater than of entire tree': function(test) { + // ============================================================================ test.expect(1); var books = json.store.book; var expected = [books[0], books[2]]; - var result = jsonpath(json, "$..book[?(@.price<10)]"); + var result = JSONPath({json: json, path: '$..book[?(@.price<10)]'}); test.deepEqual(expected, result); - + test.done(); }, - - // ============================================================================ - "all properties of a json structure": function(test) { - // ============================================================================ + + // ============================================================================ + 'all properties of a JSON structure': function(test) { + // ============================================================================ // test.expect(1); var expected = [ json.store, @@ -165,13 +165,13 @@ module.exports = testCase({ expected.push(json.store.bicycle.color); expected.push(json.store.bicycle.price); - var result = jsonpath(json, "$..*"); + var result = JSONPath({json: json, path: '$..*'}); test.deepEqual(expected, result); - + test.done(); } - - - - + + + + }); diff --git a/test/test.html b/test/test.html index f9089200ec6bbf50c22ac689dfe634e0c1ef226a..505fd4f35ac098851b9248889680e1eb771cba24 100644 --- a/test/test.html +++ b/test/test.html @@ -7,54 +7,61 @@ <script src="../node_modules/nodeunit/dist/browser/nodeunit.js"></script> <script src="../lib/jsonpath.js"></script> <script> + /*jslint nomen: true, white: true, browser:true, plusplus: true, evil:true*/ + /*global nodeunit, JSONPath, ActiveXObject*/ // helper to get all the test cases + 'use strict'; var suites = [], _testCase = nodeunit.testCase; nodeunit.testCase = function(tc) { - suites.push(tc); return _testCase(tc) }; + suites.push(tc); + return _testCase(tc); + }; // stubs to load nodejs tests function require(path) { - if (path === 'nodeunit') return nodeunit; - if (path.match(/^\.\.\/?$/)) return jsonPath; + if (path === 'nodeunit') {return nodeunit;} + if (path.match(/^\.\.\/?$/)) {return JSONPath;} } var module = {exports: {}}; - </script> - <script> + + // synchronous load function for JS code, uses XMLHttpRequest abstraction from // http://www.quirksmode.org/js/xmlhttp.html // Since the tests are written in node.js style we need to wrap their code into // a function, otherwise they would pollute the global NS and interfere with each // other function get(url, callback) { - function sendRequest(url,callback) { - var req = createXMLHTTPObject(); - req.open("GET",url,false/*sync*/); - req.onreadystatechange = function () { req.readyState == 4 && callback(req); } - if (req.readyState != 4) req.send(); - }; function createXMLHTTPObject() { - var XMLHttpFactories = [ - function () {return new XMLHttpRequest()}, - function () {return new ActiveXObject("Msxml2.XMLHTTP")}, - function () {return new ActiveXObject("Msxml3.XMLHTTP")}, - function () {return new ActiveXObject("Microsoft.XMLHTTP")}]; - for (var i=0;i<XMLHttpFactories.length;i++) - try { return XMLHttpFactories[i](); } catch (e) { } + var i, XMLHttpFactories = [ + function () {return new XMLHttpRequest();}, + function () {return new ActiveXObject('Msxml2.XMLHTTP');}, + function () {return new ActiveXObject('Msxml3.XMLHTTP');}, + function () {return new ActiveXObject('Microsoft.XMLHTTP');}]; + for (i = 0; i < XMLHttpFactories.length; i++) { + try {return XMLHttpFactories[i]();} + catch (ignore) {} + } return false; } + function sendRequest(url,callback) { + var req = createXMLHTTPObject(); + req.open('GET', url, false /*sync*/); + req.onreadystatechange = function () { if (req.readyState === 4) { callback(req); } }; + if (req.readyState !== 4) {req.send();} + } sendRequest(url, callback); } - function loadJS(url) { get(url, function(req) { new Function(req.responseText)(); })} + function loadJS(url) { get(url, function(req) { new Function(req.responseText)(); });} </script> </head> <body> <h1 id="nodeunit-header">JSONPath Tests</h1> <script> loadJS('test.arr.js'); - loadJS("test.at_and_dollar.js"); - loadJS("test.eval.js"); - loadJS("test.examples.js"); - loadJS("test.intermixed.arr.js"); - loadJS("test.parent-selector.js"); + loadJS('test.at_and_dollar.js'); + loadJS('test.eval.js'); + loadJS('test.examples.js'); + loadJS('test.intermixed.arr.js'); + loadJS('test.parent-selector.js'); nodeunit.run(suites); </script> </body> diff --git a/test/test.intermixed.arr.js b/test/test.intermixed.arr.js index 1cf622d5d798fb538c2a7164e03b460f2be06c45..7a9eaa035da20429f0c38a88bd63d424110fe19a 100644 --- a/test/test.intermixed.arr.js +++ b/test/test.intermixed.arr.js @@ -1,4 +1,4 @@ -var jsonpath = require("../").eval, +var JSONPath = require('../'), testCase = require('nodeunit').testCase // tests based on examples at http://goessner.net/articles/JsonPath/ @@ -39,13 +39,13 @@ var json = {"store":{ module.exports = testCase({ // ============================================================================ - "all sub properties, entire tree":function (test) { + 'all sub properties, entire tree': function (test) { // ============================================================================ test.expect(1); var books = json.store.book; var expected = [books[1].price, books[2].price, books[3].price, json.store.bicycle.price]; expected = books[0].price.concat(expected); - var result = jsonpath(json, "$.store..price", {flatten: true}); + var result = JSONPath({json: json, path: '$.store..price', flatten: true}); test.deepEqual(expected, result); test.done(); diff --git a/test/test.parent-selector.js b/test/test.parent-selector.js index 678c9e3f883b8a5d19d3cd1e206d2fe42d9ae34f..6f9c422495554c96ca6643a37ccec5ce7d4c9514 100644 --- a/test/test.parent-selector.js +++ b/test/test.parent-selector.js @@ -1,4 +1,4 @@ -var jsonpath = require("../").eval, +var JSONPath = require('../'), testCase = require('nodeunit').testCase var json = { @@ -14,49 +14,49 @@ var json = { module.exports = testCase({ // ============================================================================ - "simple parent selection": function(test) { + 'simple parent selection': function(test) { // ============================================================================ test.expect(1); - var result = jsonpath(json, "$.children[0]^", {flatten: true}); + var result = JSONPath({json: json, path: '$.children[0]^', flatten: true}); test.deepEqual(json.children, result); test.done(); }, // ============================================================================ - "parent selection with multiple matches": function(test) { + 'parent selection with multiple matches': function(test) { // ============================================================================ test.expect(1); var expected = [json.children,json.children]; - var result = jsonpath(json, "$.children[1:3]^"); + var result = JSONPath({json: json, path: '$.children[1:3]^'}); test.deepEqual(expected, result); test.done(); }, // ============================================================================ - "select sibling via parent": function(test) { + 'select sibling via parent': function(test) { // ============================================================================ test.expect(1); var expected = [{"name": "child3_2"}]; - var result = jsonpath(json, "$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]"); + var result = JSONPath({json: json, path: '$..[?(@.name && @.name.match(/3_1$/))]^[?(@.name.match(/_2$/))]'}); test.deepEqual(expected, result); test.done(); }, // ============================================================================ - "parent parent parent": function(test) { + 'parent parent parent': function(test) { // ============================================================================ test.expect(1); var expected = json.children[0].children; - var result = jsonpath(json, "$..[?(@.name && @.name.match(/1_1$/))].name^^", {flatten: true}); + var result = JSONPath({json: json, path: '$..[?(@.name && @.name.match(/1_1$/))].name^^', flatten: true}); test.deepEqual(expected, result); test.done(); }, // ============================================================================ - "no such parent": function(test) { + 'no such parent': function(test) { // ============================================================================ test.expect(1); - var result = jsonpath(json, "name^^"); + var result = JSONPath({json: json, path: 'name^^'}); test.deepEqual([], result); test.done(); }