Skip to content

Commit 5c0c808

Browse files
committed
Merge branch 'artemisbot-features/jpath'
2 parents fc9497f + 55aedfe commit 5c0c808

File tree

7 files changed

+388
-0
lines changed

7 files changed

+388
-0
lines changed

Gruntfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ module.exports = function (grunt) {
229229
stats: {
230230
children: false,
231231
warningsFilter: /source-map/
232+
},
233+
node: {
234+
fs: "empty"
232235
}
233236
},
234237
webDev: {

package-lock.json

Lines changed: 140 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"google-code-prettify": "^1.0.5",
7878
"jquery": "^3.1.1",
7979
"jsbn": "^1.1.0",
80+
"jsonpath": "^0.2.12",
8081
"jsrsasign": "8.0.3",
8182
"lodash": "^4.17.4",
8283
"moment": "^2.17.1",

src/core/config/Categories.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ const Categories = [
215215
"Extract dates",
216216
"Regular expression",
217217
"XPath expression",
218+
"JPath expression",
218219
"CSS selector",
219220
"Extract EXIF",
220221
]
@@ -278,6 +279,7 @@ const Categories = [
278279
"CSS Beautify",
279280
"CSS Minify",
280281
"XPath expression",
282+
"JPath expression",
281283
"CSS selector",
282284
"Strip HTML tags",
283285
"Diff",

src/core/config/OperationConfig.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,24 @@ const OperationConfig = {
22432243
}
22442244
]
22452245
},
2246+
"JPath expression": {
2247+
description: "Extract information from a JSON object with a JPath query.",
2248+
run: Code.runJpath,
2249+
inputType: "string",
2250+
outputType: "string",
2251+
args: [
2252+
{
2253+
name: "Query",
2254+
type: "string",
2255+
value: Code.JPATH_INITIAL
2256+
},
2257+
{
2258+
name: "Result delimiter",
2259+
type: "binaryShortString",
2260+
value: Code.JPATH_DELIMITER
2261+
}
2262+
]
2263+
},
22462264
"CSS selector": {
22472265
description: "Extract information from an HTML document with a CSS selector",
22482266
run: Code.runCSSQuery,

src/core/operations/Code.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Utils from "../Utils.js";
44
import vkbeautify from "vkbeautify";
55
import {DOMParser as dom} from "xmldom";
66
import xpath from "xpath";
7+
import jpath from "jsonpath";
78
import prettyPrintOne from "imports-loader?window=>global!exports-loader?prettyPrintOne!google-code-prettify/bin/prettify.min.js";
89

910

@@ -355,6 +356,48 @@ const Code = {
355356
},
356357

357358

359+
/**
360+
* @constant
361+
* @default
362+
*/
363+
JPATH_INITIAL: "",
364+
365+
/**
366+
* @constant
367+
* @default
368+
*/
369+
JPATH_DELIMITER: "\\n",
370+
371+
/**
372+
* JPath expression operation.
373+
*
374+
* @author Matt C ([email protected])
375+
* @param {string} input
376+
* @param {Object[]} args
377+
* @returns {string}
378+
*/
379+
runJpath: function(input, args) {
380+
let query = args[0],
381+
delimiter = args[1],
382+
results,
383+
obj;
384+
385+
try {
386+
obj = JSON.parse(input);
387+
} catch (err) {
388+
return "Invalid input JSON: " + err.message;
389+
}
390+
391+
try {
392+
results = jpath.query(obj, query);
393+
} catch (err) {
394+
return "Invalid JPath expression: " + err.message;
395+
}
396+
397+
return results.map(result => JSON.stringify(result)).join(delimiter);
398+
},
399+
400+
358401
/**
359402
* @constant
360403
* @default

0 commit comments

Comments
 (0)