added the jsonbath bower component's source

This commit is contained in:
Spencer Alger 2014-03-24 15:40:15 -07:00
parent 3b6762eb74
commit aafa3d3af1
15 changed files with 848 additions and 0 deletions

View file

@ -0,0 +1,13 @@
{
"name": "jsonpath",
"homepage": "https://github.com/s3u/JSONPath",
"_release": "308ef21860",
"_resolution": {
"type": "branch",
"branch": "master",
"commit": "308ef21860ae21584c0f808d67cf63006101149a"
},
"_source": "git://github.com/s3u/JSONPath.git",
"_target": "*",
"_originalSource": "jsonpath"
}

View file

@ -0,0 +1,9 @@
*.iml
.DS_Store
.idea
temp
node_modules
pids
reports
target
*.log

View file

@ -0,0 +1,2 @@
node_modules
test

View file

@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.6
- 0.8

View file

@ -0,0 +1,14 @@
## Oct 23, 2013
* Support for parent selection via `^`
* Access current path via `@path` in test statements
* Allowing for multi-statement evals
* Performance improvements
* Version 0.10
## Mar 28, 2012
* Support a sandbox arg to eval
* Use vm.runInNewContext in place of eval
* Version 0.9.0

111
src/bower_components/jsonpath/README.md vendored Normal file
View file

@ -0,0 +1,111 @@
JSONPath [![build status](https://secure.travis-ci.org/s3u/JSONPath.png)](http://travis-ci.org/s3u/JSONPath)
========
Analyse, transform, and selectively extract data from JSON documents (and JavaScript objects).
Install
-------
npm install JSONPath
Usage
-----
In node.js:
```js
var jsonPath = require('JSONPath');
jsonPath.eval(obj, path);
```
For browser usage you can directly include `lib/jsonpath.js`, no browserify
magic necessary:
```html
<script type="text/javascript" src="lib/jsonpath.js"></script>
<script type="text/javascript">
jsonPath.eval(obj, path);
</script>
```
Examples
--------
Given the following JSON, taken from http://goessner.net/articles/JsonPath/ :
```json
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"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
}
}
}
```
XPath | JSONPath | Result
------------------- | ---------------------- | -------------------------------------
/store/book/author | $.store.book[*].author | the authors of all books in the store
//author | $..author | all authors
/store/* | $.store.* | all things in store, which are some books and a red bicycle.
/store//price | $.store..price | the price of everything in the store.
//book[3] | $..book[2] | the third book
//book[last()] | $..book[(@.length-1)] | the last book in order.
| $..book[-1:] |
//book[position()<3]| $..book[0,1] | the first two books
| $..book[:2] |
//book[isbn] | $..book[?(@.isbn)] | filter all books with isbn number
//book[price<10] | $..book[?(@.price<10)] | filter all books cheapier than 10
//*[price>19]/.. | $..[?(@.price>19)]^ | categories with things more expensive than 19
//* | $..* | all Elements in XML document. All members of JSON structure.
Development
-----------
Running the tests on node: `npm test`. For in-browser tests:
* Ensure that nodeunit is browser-compiled: `cd node_modules/nodeunit; make browser;`
* Serve the js/html files:
```sh
node -e "require('http').createServer(function(req,res) { \
var s = require('fs').createReadStream('.' + req.url); \
s.pipe(res); s.on('error', function() {}); }).listen(8082);"
```
* To run the tests visit [http://localhost:8082/test/test.html]().
License
-------
[MIT License](http://www.opensource.org/licenses/mit-license.php).

View file

@ -0,0 +1,172 @@
/* JSONPath 0.8.0 - XPath for JSON
*
* Copyright (c) 2007 Stefan Goessner (goessner.net)
* Licensed under the MIT (MIT-LICENSE.txt) licence.
*/
var isNode = false;
(function(exports, require) {
// Keep compatibility with old browsers
if (!Array.isArray) {
Array.isArray = function(vArg) {
return Object.prototype.toString.call(vArg) === "[object Array]";
};
}
// Make sure to know if we are in real node or not (the `require` variable
// could actually be require.js, for example.
var isNode = typeof module !== 'undefined' && !!module.exports;
var vm = isNode ?
require('vm') : {
runInNewContext: function(expr, context) { with (context) return eval(expr); }
};
exports.eval = jsonPath;
var cache = {};
function push(arr, elem) { arr = arr.slice(); arr.push(elem); return arr; }
function unshift(elem, arr) { arr = arr.slice(); arr.unshift(elem); return arr; }
function jsonPath(obj, expr, arg) {
var P = {
resultType: arg && arg.resultType || "VALUE",
flatten: arg && arg.flatten || false,
wrap: (arg && arg.hasOwnProperty('wrap')) ? arg.wrap : true,
sandbox: (arg && arg.sandbox) ? arg.sandbox : {},
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, function(_, front, ups, back) { return ';' + ups.split('').join(';') + ';'; })
.replace(/;;;|;;/g, ";..;")
.replace(/;$|'?\]|'$/g, "");
var exprList = normalized.split(';').map(function(expr) {
var match = expr.match(/#([0-9]+)/);
return !match || !match[1] ? expr : subx[match[1]];
})
return cache[expr] = exprList;
},
asPath: function(path) {
var x = path, p = "$";
for (var i=1,n=x.length; i<n; i++)
p += /^[0-9*]+$/.test(x[i]) ? ("["+x[i]+"]") : ("['"+x[i]+"']");
return p;
},
trace: function(expr, val, path) {
// no expr to follow? return path and value as the result of this trace branch
if (!expr.length) return [{path: path, value: val}];
var loc = expr[0], x = expr.slice(1);
// the parent sel computation is handled in the frame above using the
// ancestor object of val
if (loc === '^') return path.length ? [{path: path.slice(0,-1), expr: x, isParentSelector: true}] : [];
// we need to gather the return value of recursive trace calls in order to
// do the parent sel computation.
var ret = [];
function addRet(elems) { ret = ret.concat(elems); }
if (val && val.hasOwnProperty(loc)) // simple case, directly follow property
addRet(P.trace(x, val[loc], push(path, loc)));
else if (loc === "*") { // any property
P.walk(loc, x, val, path, function(m,l,x,v,p) {
addRet(P.trace(unshift(m, x), v, p)); });
}
else if (loc === "..") { // all chid properties
addRet(P.trace(x, val, path));
P.walk(loc, x, val, path, function(m,l,x,v,p) {
if (typeof v[m] === "object")
addRet(P.trace(unshift("..", x), v[m], push(p, m)));
});
}
else if (loc[0] === '(') { // [(expr)]
addRet(P.trace(unshift(P.eval(loc, val, path[path.length], path),x), val, path));
}
else if (loc.indexOf('?(') === 0) { // [?(expr)]
P.walk(loc, x, val, path, function(m,l,x,v,p) {
if (P.eval(l.replace(/^\?\((.*?)\)$/,"$1"),v[m],m, path))
addRet(P.trace(unshift(m,x),v,p));
});
}
else if (loc.indexOf(',') > -1) { // [name1,name2,...]
for (var parts = loc.split(','), i = 0; i < parts.length; i++)
addRet(P.trace(unshift(parts[i], x), val, path));
}
else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) { // [start:end:step] python slice syntax
addRet(P.slice(loc, x, val, path));
}
// we check the resulting values for parent selections. for parent
// selections we discard the value object and continue the trace with the
// current val object
return ret.reduce(function(all, ea) {
return all.concat(ea.isParentSelector ? P.trace(ea.expr, val, ea.path) : [ea]);
}, []);
},
walk: function(loc, expr, val, path, f) {
if (Array.isArray(val))
for (var i = 0, n = val.length; i < n; i++)
f(i, loc, expr, val, path);
else if (typeof val === "object")
for (var m in val)
if (val.hasOwnProperty(m))
f(m, loc, expr, val, path);
},
slice: function(loc, expr, val, path) {
if (!Array.isArray(val)) return;
var len = val.length, parts = loc.split(':'),
start = (parts[0] && parseInt(parts[0])) || 0,
end = (parts[1] && parseInt(parts[1])) || len,
step = (parts[2] && parseInt(parts[2])) || 1;
start = (start < 0) ? Math.max(0,start+len) : Math.min(len,start);
end = (end < 0) ? Math.max(0,end+len) : Math.min(len,end);
var ret = [];
for (var i = start; i < end; i += step)
ret = ret.concat(P.trace(unshift(i,expr), val, path));
return ret;
},
eval: function(code, _v, _vname, path) {
if (!$ || !_v) return false;
if (code.indexOf("@path") > -1) {
P.sandbox["_path"] = P.asPath(path.concat([_vname]));
code = code.replace(/@path/g, "_path");
}
if (code.indexOf("@") > -1) {
P.sandbox["_v"] = _v;
code = code.replace(/@/g, "_v");
}
try {
return vm.runInNewContext(code, P.sandbox);
}
catch(e) {
console.log(e);
throw new Error("jsonPath: " + e.message + ": " + code);
}
}
};
var $ = obj;
var resultType = P.resultType.toLowerCase();
if (expr && obj && (resultType == "value" || resultType == "path")) {
var exprList = P.normalize(expr);
if (exprList[0] === "$" && exprList.length > 1) exprList.shift();
var result = P.trace(exprList, obj, ["$"]);
result = result.filter(function(ea) { return ea && !ea.isParentSelector; });
if (!result.length) return P.wrap ? [] : false;
if (result.length === 1 && !P.wrap && !Array.isArray(result[0].value)) return result[0][resultType] || false;
return result.reduce(function(result, ea) {
var valOrPath = ea[resultType];
if (resultType === 'path') valOrPath = P.asPath(valOrPath);
if (P.flatten && Array.isArray(valOrPath)) {
result = result.concat(valOrPath);
} else {
result.push(valOrPath);
}
return result;
}, []);
}
}
})(typeof exports === 'undefined' ? this['jsonPath'] = {} : exports, typeof require == "undefined" ? null : require);

View file

@ -0,0 +1,36 @@
{
"author": "Stefan Goessner",
"name": "JSONPath",
"description": "A JS implementation of JSONPath",
"contributors": [
{
"name": "Prof. Gössner",
"email": "stefan.goessner@fh-dortmund.de"
},
{
"name": "Subbu Allamaraju",
"email": "subbu@subbu.org"
},
{
"name": "Mike Brevoort",
"email": "mike@brevoort.com"
},
{
"name": "Robert Krahn",
"email": "robert.krahn@gmail.com"
}
],
"version": "0.10.0",
"repository": {
"type": "git",
"url": "git://github.com/s3u/JSONPath.git"
},
"main": "./lib/jsonpath",
"dependencies": {
"underscore": "*"
},
"devDependencies": {
"nodeunit": "*"
},
"scripts": {"test": "node_modules/nodeunit/bin/nodeunit test/test.*.js"}
}

View file

@ -0,0 +1,35 @@
var jsonpath = require("../").eval,
testCase = require('nodeunit').testCase
var json = {
"store": {
"book": { "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": [8.95, 8.94, 8.93]
},
"books": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": [8.95, 8.94, 8.93]
}
]
}
};
module.exports = testCase({
"get single": function (test) {
var expected = json.store.book;
var result = jsonpath(json, "store.book", {flatten: true, wrap: false});
test.deepEqual(expected, result);
test.done();
},
"get arr": function (test) {
var expected = json.store.books;
var result = jsonpath(json, "store.books", {flatten: true, wrap: false});
test.deepEqual(expected, result);
test.done();
}
});

View file

@ -0,0 +1,54 @@
var jsonpath = require("../").eval
, testCase = require('nodeunit').testCase
var t1 = {
simpleString: "simpleString",
"@" : "@asPropertyName",
"$" : "$asPropertyName",
"a$a": "$inPropertyName",
"$": {
"@": "withboth",
},
a: {
b: {
c: "food"
}
}
};
module.exports = testCase({
// ============================================================================
"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.done();
},
// ============================================================================
"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.done();
}
});

View file

@ -0,0 +1,45 @@
var jsonpath = require("../").eval,
testCase = require('nodeunit').testCase
var json = {
"store": {
"book": {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": [8.95, 8.94]
},
"books": [{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": [10.99, 12.29]
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": [8.99, 6.95]
}]
}
};
module.exports = testCase({
"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});
test.deepEqual(expected, result);
test.done();
},
"accessing current path": function (test) {
var expected = json.store.books[1];
var result = jsonpath(json, "$..[?(@path==\"$['store']['books'][1]\")]", {wrap: false});
test.deepEqual(expected, result);
test.done();
}
});

View file

@ -0,0 +1,177 @@
var jsonpath = require("../").eval
, testCase = require('nodeunit').testCase
// tests based on examples at http://goessner.net/articles/JsonPath/
var json = {"store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"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({
// ============================================================================
"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");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"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");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"all sub properties, single level": function(test) {
// ============================================================================
test.expect(1);
var expected = [json.store.book, json.store.bicycle];
var result = jsonpath(json, "$.store.*");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"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");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"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]");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"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)]");
test.deepEqual(expected, result);
result = jsonpath(json, "$..book[-1:]");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"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]");
test.deepEqual(expected, result);
result = jsonpath(json, "$..book[:2]");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"filter all properties if sub property exists,o entire tree": function(test) {
// ============================================================================
test.expect(1);
var books = json.store.book;
var expected = [books[2], books[3]];
var result = jsonpath(json, "$..book[?(@.isbn)]");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"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)]");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"all properties of a json structure": function(test) {
// ============================================================================
// test.expect(1);
var expected = [
json.store,
json.store.book,
json.store.bicycle,
];
json.store.book.forEach(function(book) { expected.push(book); });
json.store.book.forEach(function(book) { Object.keys(book).forEach(function(p) { expected.push(book[p]); })});
expected.push(json.store.bicycle.color);
expected.push(json.store.bicycle.price);
var result = jsonpath(json, "$..*");
test.deepEqual(expected, result);
test.done();
}
});

View file

@ -0,0 +1,59 @@
<html>
<head>
<title>JSONPath Tests</title>
<link rel="stylesheet" href="../node_modules/nodeunit/dist/browser/nodeunit.css" type="text/css" />
<script src="../node_modules/nodeunit/dist/browser/nodeunit.js"></script>
<script src="../lib/jsonpath.js"></script>
<script type="text/javascript">
// helper to get all the test cases
var suites = [], _testCase = nodeunit.testCase;
nodeunit.testCase = function(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;
}
var module = {exports: {}};
</script>
<script type="text/javascript">
// 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) { }
return false;
}
sendRequest(url, callback);
}
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");
nodeunit.run(suites);
</script>
</body>
</html>

View file

@ -0,0 +1,53 @@
var jsonpath = require("../").eval,
testCase = require('nodeunit').testCase
// tests based on examples at http://goessner.net/articles/JsonPath/
var json = {"store":{
"book":[
{ "category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":[8.95, 8.94, 8.93]
},
{ "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({
// ============================================================================
"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});
test.deepEqual(expected, result);
test.done();
}
});

View file

@ -0,0 +1,64 @@
var jsonpath = require("../").eval,
testCase = require('nodeunit').testCase
var json = {
"name": "root",
"children": [
{"name": "child1", "children": [{"name": "child1_1"},{"name": "child1_2"}]},
{"name": "child2", "children": [{"name": "child2_1"}]},
{"name": "child3", "children": [{"name": "child3_1"}, {"name": "child3_2"}]}
]
};
module.exports = testCase({
// ============================================================================
"simple parent selection": function(test) {
// ============================================================================
test.expect(1);
var result = jsonpath(json, "$.children[0]^", {flatten: true});
test.deepEqual(json.children, result);
test.done();
},
// ============================================================================
"parent selection with multiple matches": function(test) {
// ============================================================================
test.expect(1);
var expected = [json.children,json.children];
var result = jsonpath(json, "$.children[1:3]^");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"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$/))]");
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"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});
test.deepEqual(expected, result);
test.done();
},
// ============================================================================
"no such parent": function(test) {
// ============================================================================
test.expect(1);
var result = jsonpath(json, "name^^");
test.deepEqual([], result);
test.done();
}
});