diff --git a/package.json b/package.json index a978f947..76e84e46 100644 --- a/package.json +++ b/package.json @@ -106,6 +106,7 @@ "loglevel-message-prefix": "^3.0.0", "moment": "^2.22.2", "moment-timezone": "^0.5.21", + "mustache": "^2.3.2", "node-forge": "^0.7.5", "node-md6": "^0.1.0", "notepack.io": "^2.1.3", diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 66663f4a..868cff25 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -342,7 +342,8 @@ "Remove EXIF", "Extract EXIF", "Numberwang", - "XKCD Random Number" + "XKCD Random Number", + "Render Moustache" ] }, { diff --git a/src/core/operations/RenderMoustache.mjs b/src/core/operations/RenderMoustache.mjs new file mode 100644 index 00000000..69e34632 --- /dev/null +++ b/src/core/operations/RenderMoustache.mjs @@ -0,0 +1,49 @@ +/** + * @author gchq77703 [] + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ + +import Operation from "../Operation"; +import Moustache from "mustache"; + +/** + * Render Moustache operation + */ +class RenderMoustache extends Operation { + + /** + * Render Moustache constructor + */ + constructor() { + super(); + + this.name = "Render Moustache"; + this.module = "Default"; + this.description = "Uses a provided Moustache template in order to render a JSON value."; + this.infoURL = "https://mustache.github.io/mustache.5.html"; + this.inputType = "JSON"; + this.outputType = "html"; + this.args = [ + { + name: "Template", + type: "text", + value: "Hello {{name}}." + }, + ]; + } + + /** + * @param {JSON} input + * @param {Object[]} args + * @returns {html} + */ + run(input, args) { + const [template] = args; + + return Moustache.render(template, input) + } + +} + +export default RenderMoustache; diff --git a/test/index.mjs b/test/index.mjs index 8cf69732..2ff6aeeb 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -64,6 +64,7 @@ import "./tests/operations/SetUnion"; import "./tests/operations/SymmetricDifference"; import "./tests/operations/TranslateDateTimeFormat"; import "./tests/operations/Magic"; +import "./tests/operations/RenderMoustache" let allTestsPassing = true; const testStatusCounts = { diff --git a/test/tests/operations/RenderMoustache.mjs b/test/tests/operations/RenderMoustache.mjs new file mode 100644 index 00000000..5a49cbf6 --- /dev/null +++ b/test/tests/operations/RenderMoustache.mjs @@ -0,0 +1,47 @@ +/** + * Render Moustache tests. + * + * @author gchq77703 [] + * + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ +import TestRegister from "../../TestRegister"; + + +TestRegister.addTests([ + { + name: "Render Moustache", + input: `{ + "name": "Dave", + "value": "£50,000", + "in_england": true, + "taxed_value": "£40,000" +}`, + expectedOutput: `Hello Dave +You have just won £50,000! +Well, £40,000 after taxes. +`, + recipeConfig: [ + { + op: "Render Moustache", + args: [`Hello {{name}} +You have just won {{value}}! +{{#in_england}} +Well, {{taxed_value}} after taxes. +{{/in_england}}`] + } + ], + }, + { + name: "Render Moustache", + input: `{}`, + expectedOutput: `Hello .`, + recipeConfig: [ + { + op: "Render Moustache", + args: [`Hello {{name}}.`] + } + ], + }, +]);