Add Moustache Template

This commit is contained in:
GCHQ 77703 2018-08-26 21:11:38 +01:00
parent 3905c01a0d
commit 542809d5f5
5 changed files with 100 additions and 1 deletions

View File

@ -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",

View File

@ -342,7 +342,8 @@
"Remove EXIF",
"Extract EXIF",
"Numberwang",
"XKCD Random Number"
"XKCD Random Number",
"Render Moustache"
]
},
{

View File

@ -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;

View File

@ -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 = {

View File

@ -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}}.`]
}
],
},
]);