Explorar o código

Adds initial JPath functionality

Matt C %!s(int64=8) %!d(string=hai) anos
pai
achega
de80db73f2

+ 1 - 0
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",

+ 2 - 0
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",

+ 18 - 0
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,

+ 36 - 0
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