Adding a literal helper which can make sure the output of an operation is a literal value.
This commit is contained in:
parent
766200e59e
commit
673ae8d031
|
@ -18,4 +18,7 @@ module.exports.HelperFunctionBuiltin = [
|
||||||
module.exports.HelperFunctionNames = {
|
module.exports.HelperFunctionNames = {
|
||||||
OBJECT: "object",
|
OBJECT: "object",
|
||||||
ALL: "all",
|
ALL: "all",
|
||||||
|
LITERAL: "literal",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.LITERAL_MARKER = "%LITERAL%"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const Helper = require("./Helper")
|
const Helper = require("./Helper")
|
||||||
const { SafeString } = require("handlebars")
|
const { SafeString } = require("handlebars")
|
||||||
const externalHandlebars = require("./external")
|
const externalHandlebars = require("./external")
|
||||||
const { HelperFunctionNames, HelperFunctionBuiltin } = require("./constants")
|
const { HelperFunctionNames, HelperFunctionBuiltin, LITERAL_MARKER } = require("./constants")
|
||||||
|
|
||||||
const HTML_SWAPS = {
|
const HTML_SWAPS = {
|
||||||
"<": "<",
|
"<": "<",
|
||||||
|
@ -27,6 +27,12 @@ const HELPERS = [
|
||||||
return HTML_SWAPS[tag] || tag
|
return HTML_SWAPS[tag] || tag
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
|
// adds a note for post-processor
|
||||||
|
new Helper(HelperFunctionNames.LITERAL, value => {
|
||||||
|
const type = typeof(value)
|
||||||
|
const outputVal = type === "object" ? JSON.stringify(value) : value
|
||||||
|
return `{{-${LITERAL_MARKER}-${type}-${outputVal}-}}`
|
||||||
|
})
|
||||||
]
|
]
|
||||||
|
|
||||||
module.exports.HelperNames = () => {
|
module.exports.HelperNames = () => {
|
||||||
|
|
|
@ -2,19 +2,23 @@ const { FIND_HBS_REGEX } = require("../utilities")
|
||||||
const preprocessor = require("./preprocessor")
|
const preprocessor = require("./preprocessor")
|
||||||
const postprocessor = require("./postprocessor")
|
const postprocessor = require("./postprocessor")
|
||||||
|
|
||||||
function process(string, processors) {
|
function process(output, processors) {
|
||||||
for (let processor of processors) {
|
for (let processor of processors) {
|
||||||
|
// if a literal statement has occurred stop
|
||||||
|
if (typeof output !== "string") {
|
||||||
|
break
|
||||||
|
}
|
||||||
// re-run search each time incase previous processor updated/removed a match
|
// re-run search each time incase previous processor updated/removed a match
|
||||||
let regex = new RegExp(FIND_HBS_REGEX)
|
let regexp = new RegExp(FIND_HBS_REGEX)
|
||||||
let matches = string.match(regex)
|
let matches = output.match(regexp)
|
||||||
if (matches == null) {
|
if (matches == null) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for (let match of matches) {
|
for (let match of matches) {
|
||||||
string = processor.process(string, match)
|
output = processor.process(output, match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return string
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.preprocess = (string, finalise = true) => {
|
module.exports.preprocess = (string, finalise = true) => {
|
||||||
|
|
|
@ -1,9 +1,43 @@
|
||||||
|
const { LITERAL_MARKER } = require("../helpers/constants")
|
||||||
|
|
||||||
|
const PostProcessorNames = {
|
||||||
|
CONVERT_LITERALS: "convert-literals",
|
||||||
|
}
|
||||||
|
|
||||||
/* eslint-disable no-unused-vars */
|
/* eslint-disable no-unused-vars */
|
||||||
class Postprocessor {
|
class Postprocessor {
|
||||||
constructor(name, fn) {
|
constructor(name, fn) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.fn = fn
|
this.fn = fn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process(statement) {
|
||||||
|
return this.fn(statement)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.processors = []
|
module.exports.processors = [
|
||||||
|
new Postprocessor(PostProcessorNames.CONVERT_LITERALS, statement => {
|
||||||
|
if (!statement.includes(LITERAL_MARKER)) {
|
||||||
|
return statement
|
||||||
|
}
|
||||||
|
|
||||||
|
const components = statement.split("-")
|
||||||
|
// pop and shift remove the empty array elements from the first and last dash
|
||||||
|
components.pop()
|
||||||
|
components.shift()
|
||||||
|
const type = components[1]
|
||||||
|
const value = components[2]
|
||||||
|
switch (type) {
|
||||||
|
case "string":
|
||||||
|
return value
|
||||||
|
case "number":
|
||||||
|
return parseFloat(value)
|
||||||
|
case "boolean":
|
||||||
|
return value === "true"
|
||||||
|
case "object":
|
||||||
|
return JSON.parse(value)
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
})
|
||||||
|
]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const _ = require("lodash")
|
const _ = require("lodash")
|
||||||
const ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
|
const ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
|
||||||
|
|
||||||
module.exports.FIND_HBS_REGEX = /{{([^{}])+}}/g
|
module.exports.FIND_HBS_REGEX = /{{([^{].*?)}}/g
|
||||||
|
|
||||||
module.exports.isAlphaNumeric = char => {
|
module.exports.isAlphaNumeric = char => {
|
||||||
return char.match(ALPHA_NUMERIC_REGEX)
|
return char.match(ALPHA_NUMERIC_REGEX)
|
||||||
|
|
|
@ -259,3 +259,19 @@ describe("test the comparison helpers", () => {
|
||||||
expect(output).toBe("s")
|
expect(output).toBe("s")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("Test the literal helper", () => {
|
||||||
|
it("should allow use of the literal specifier for a number", async () => {
|
||||||
|
const output = await processString(`{{literal a}}`, {
|
||||||
|
a: 51,
|
||||||
|
})
|
||||||
|
expect(output).toBe(51)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should allow use of the literal specifier for an object", async () => {
|
||||||
|
const output = await processString(`{{literal a}}`, {
|
||||||
|
a: {b: 1},
|
||||||
|
})
|
||||||
|
expect(output.b).toBe(1)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue