Adds initial JPath functionality

This commit is contained in:
Matt C 2017-07-25 16:27:59 +01:00
parent d46e279933
commit de80db73f2
4 changed files with 57 additions and 0 deletions

View file

@ -80,6 +80,7 @@
"lodash": "^4.17.4", "lodash": "^4.17.4",
"moment": "^2.17.1", "moment": "^2.17.1",
"moment-timezone": "^0.5.11", "moment-timezone": "^0.5.11",
"node-jpath": "^2.1.0",
"sladex-blowfish": "^0.8.1", "sladex-blowfish": "^0.8.1",
"sortablejs": "^1.5.1", "sortablejs": "^1.5.1",
"split.js": "^1.2.0", "split.js": "^1.2.0",

View file

@ -215,6 +215,7 @@ const Categories = [
"Extract dates", "Extract dates",
"Regular expression", "Regular expression",
"XPath expression", "XPath expression",
"JPath expression",
"CSS selector", "CSS selector",
"Extract EXIF", "Extract EXIF",
] ]
@ -278,6 +279,7 @@ const Categories = [
"CSS Beautify", "CSS Beautify",
"CSS Minify", "CSS Minify",
"XPath expression", "XPath expression",
"JPath expression",
"CSS selector", "CSS selector",
"Strip HTML tags", "Strip HTML tags",
"Diff", "Diff",

View file

@ -2243,6 +2243,24 @@ const OperationConfig = {
} }
] ]
}, },
"JPath expression": {
description: "Extract information from a JSON object with an JPath query",
run: Code.runJpath,
inputType: "string",
outputType: "string",
args: [
{
name: "JPath",
type: "string",
value: Code.JPATH_INITIAL
},
{
name: "Result delimiter",
type: "binaryShortString",
value: Code.JPATH_DELIMITER
}
]
},
"CSS selector": { "CSS selector": {
description: "Extract information from an HTML document with a CSS selector", description: "Extract information from an HTML document with a CSS selector",
run: Code.runCSSQuery, run: Code.runCSSQuery,

View file

@ -4,6 +4,7 @@ import Utils from "../Utils.js";
import vkbeautify from "vkbeautify"; import vkbeautify from "vkbeautify";
import {DOMParser as dom} from "xmldom"; import {DOMParser as dom} from "xmldom";
import xpath from "xpath"; import xpath from "xpath";
import jpath from "node-jpath";
import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js"; import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js";
@ -354,6 +355,41 @@ const Code = {
return nodes.map(nodeToString).join(delimiter); return nodes.map(nodeToString).join(delimiter);
}, },
/**
* @constant
* @default
*/
JPATH_INITIAL: "",
/**
* @constant
* @default
*/
JPATH_DELIMITER: "\\n",
/**
* XPath expression operation.
*
* @author Matt C (matt@artemisbot.uk)
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runJpath: function(input, args) {
let query = args[0],
delimiter = args[1];
let obj;
try {
obj = JSON.parse(input);
} catch (err) {
return "Invalid input JSON.";
}
let results = jpath.filter(obj, query);
return results.map(result => JSON.stringify(result)).join(delimiter);
},
/** /**
* @constant * @constant