Added JSONPath tests & changed lib
parent
e0905255ba
commit
31bfd8664a
|
@ -4687,11 +4687,6 @@
|
|||
"lower-case": "1.1.4"
|
||||
}
|
||||
},
|
||||
"node-jpath": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/node-jpath/-/node-jpath-2.1.0.tgz",
|
||||
"integrity": "sha1-IYckZgObw5adyzhjq0JnH2H/UbI="
|
||||
},
|
||||
"node-libs-browser": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/node-libs-browser/-/node-libs-browser-2.0.0.tgz",
|
||||
|
|
|
@ -80,7 +80,6 @@
|
|||
"lodash": "^4.17.4",
|
||||
"moment": "^2.17.1",
|
||||
"moment-timezone": "^0.5.11",
|
||||
"node-jpath": "^2.1.0",
|
||||
"sladex-blowfish": "^0.8.1",
|
||||
"sortablejs": "^1.5.1",
|
||||
"split.js": "^1.2.0",
|
||||
|
|
|
@ -2244,7 +2244,7 @@ const OperationConfig = {
|
|||
]
|
||||
},
|
||||
"JPath expression": {
|
||||
description: "Extract information from a JSON object with an JPath query",
|
||||
description: "Extract information from a JSON object with a JPath query.",
|
||||
run: Code.runJpath,
|
||||
inputType: "string",
|
||||
outputType: "string",
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -4,7 +4,7 @@ import Utils from "../Utils.js";
|
|||
import vkbeautify from "vkbeautify";
|
||||
import {DOMParser as dom} from "xmldom";
|
||||
import xpath from "xpath";
|
||||
import jpath from "node-jpath";
|
||||
import jpath from "../lib/jsonpath.js";
|
||||
import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js";
|
||||
|
||||
|
||||
|
@ -377,16 +377,19 @@ const Code = {
|
|||
*/
|
||||
runJpath: function(input, args) {
|
||||
let query = args[0],
|
||||
delimiter = args[1];
|
||||
|
||||
let obj;
|
||||
delimiter = args[1],
|
||||
results,
|
||||
obj;
|
||||
try {
|
||||
obj = JSON.parse(input);
|
||||
} catch (err) {
|
||||
return "Invalid input JSON.";
|
||||
}
|
||||
|
||||
let results = jpath.filter(obj, query);
|
||||
try {
|
||||
results = jpath.query(obj, query);
|
||||
} catch (e) {
|
||||
return "Invalid JPath expression.";
|
||||
}
|
||||
return results.map(result => JSON.stringify(result)).join(delimiter);
|
||||
},
|
||||
|
||||
|
|
|
@ -2,12 +2,54 @@
|
|||
* Code tests.
|
||||
*
|
||||
* @author tlwr [toby@toby.codes]
|
||||
* @author Matt C [matt@artemisbot.uk]
|
||||
*
|
||||
* @copyright Crown Copyright 2017
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
import TestRegister from "../../TestRegister.js";
|
||||
|
||||
const JPATH_TEST_DATA = {
|
||||
"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
|
||||
},
|
||||
"newspaper": [{
|
||||
"format": "broadsheet",
|
||||
"title": "Financial Times",
|
||||
"price": 2.75
|
||||
}, {
|
||||
"format": "tabloid",
|
||||
"title": "The Guardian",
|
||||
"price": 2.00
|
||||
}]
|
||||
}
|
||||
};
|
||||
|
||||
TestRegister.addTests([
|
||||
{
|
||||
name: "To Camel case (dumb)",
|
||||
|
@ -129,4 +171,143 @@ TestRegister.addTests([
|
|||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: Empty JSON",
|
||||
input: "",
|
||||
expectedOutput: "Invalid input JSON.",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: Empty expression",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: "Invalid JPath expression.",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: Fetch of values from specific object",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: [
|
||||
"\"Nigel Rees\"",
|
||||
"\"Evelyn Waugh\"",
|
||||
"\"Herman Melville\"",
|
||||
"\"J. R. R. Tolkien\""
|
||||
].join("\n"),
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["$.store.book[*].author", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: Fetch of all values with matching key",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: [
|
||||
"\"Sayings of the Century\"",
|
||||
"\"Sword of Honour\"",
|
||||
"\"Moby Dick\"",
|
||||
"\"The Lord of the Rings\"",
|
||||
"\"Financial Times\"",
|
||||
"\"The Guardian\""
|
||||
].join("\n"),
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["$..title", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: All data in object",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: [
|
||||
"[{\"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}]",
|
||||
"{\"color\":\"red\",\"price\":19.95}",
|
||||
"[{\"format\":\"broadsheet\",\"title\":\"Financial Times\",\"price\":2.75},{\"format\":\"tabloid\",\"title\":\"The Guardian\",\"price\":2}]"
|
||||
].join("\n"),
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["$.store.*", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: Last element in array",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: "{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}",
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["$..book[-1:]", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: First 2 elements in array",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: [
|
||||
"{\"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}"
|
||||
].join("\n"),
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["$..book[:2]", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: All elements in array with property",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: [
|
||||
"{\"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}"
|
||||
].join("\n"),
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["$..book[?(@.isbn)]", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: All elements in array which meet condition",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: [
|
||||
"{\"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}"
|
||||
].join("\n"),
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["$..book[?(@.price<30 && @.category==\"fiction\")]", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "JPath Expression: All elements in object",
|
||||
input: JSON.stringify(JPATH_TEST_DATA),
|
||||
expectedOutput: [
|
||||
"{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95}",
|
||||
"{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99}"
|
||||
].join("\n"),
|
||||
recipeConfig: [
|
||||
{
|
||||
"op": "JPath expression",
|
||||
"args": ["$..book[?(@.price<10)]", "\n"]
|
||||
}
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
|
Loading…
Reference in New Issue