From de80db73f26251ea28b72de2e32ed82cb2239726 Mon Sep 17 00:00:00 2001 From: Matt C Date: Tue, 25 Jul 2017 16:27:59 +0100 Subject: [PATCH] Adds initial JPath functionality --- package.json | 1 + src/core/config/Categories.js | 2 ++ src/core/config/OperationConfig.js | 18 +++++++++++++++ src/core/operations/Code.js | 36 ++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/package.json b/package.json index 208789bd..f1de5a5f 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "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", diff --git a/src/core/config/Categories.js b/src/core/config/Categories.js index ce46d221..b3e40b33 100755 --- a/src/core/config/Categories.js +++ b/src/core/config/Categories.js @@ -215,6 +215,7 @@ const Categories = [ "Extract dates", "Regular expression", "XPath expression", + "JPath expression", "CSS selector", "Extract EXIF", ] @@ -278,6 +279,7 @@ const Categories = [ "CSS Beautify", "CSS Minify", "XPath expression", + "JPath expression", "CSS selector", "Strip HTML tags", "Diff", diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index b0c005b3..c058d9e2 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -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": { description: "Extract information from an HTML document with a CSS selector", run: Code.runCSSQuery, diff --git a/src/core/operations/Code.js b/src/core/operations/Code.js index 9840797d..d3f5c0f0 100755 --- a/src/core/operations/Code.js +++ b/src/core/operations/Code.js @@ -4,6 +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 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); }, + /** + * @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