Adding a literal helper which can make sure the output of an operation is a literal value.
This commit is contained in:
parent
01f9885637
commit
bb85078300
|
@ -18,4 +18,7 @@ module.exports.HelperFunctionBuiltin = [
|
|||
module.exports.HelperFunctionNames = {
|
||||
OBJECT: "object",
|
||||
ALL: "all",
|
||||
LITERAL: "literal",
|
||||
}
|
||||
|
||||
module.exports.LITERAL_MARKER = "%LITERAL%"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const Helper = require("./Helper")
|
||||
const { SafeString } = require("handlebars")
|
||||
const externalHandlebars = require("./external")
|
||||
const { HelperFunctionNames, HelperFunctionBuiltin } = require("./constants")
|
||||
const { HelperFunctionNames, HelperFunctionBuiltin, LITERAL_MARKER } = require("./constants")
|
||||
|
||||
const HTML_SWAPS = {
|
||||
"<": "<",
|
||||
|
@ -27,6 +27,12 @@ const HELPERS = [
|
|||
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 = () => {
|
||||
|
|
|
@ -2,19 +2,23 @@ const { FIND_HBS_REGEX } = require("../utilities")
|
|||
const preprocessor = require("./preprocessor")
|
||||
const postprocessor = require("./postprocessor")
|
||||
|
||||
function process(string, processors) {
|
||||
function process(output, 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
|
||||
let regex = new RegExp(FIND_HBS_REGEX)
|
||||
let matches = string.match(regex)
|
||||
let regexp = new RegExp(FIND_HBS_REGEX)
|
||||
let matches = output.match(regexp)
|
||||
if (matches == null) {
|
||||
continue
|
||||
}
|
||||
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) => {
|
||||
|
|
|
@ -1,9 +1,43 @@
|
|||
const { LITERAL_MARKER } = require("../helpers/constants")
|
||||
|
||||
const PostProcessorNames = {
|
||||
CONVERT_LITERALS: "convert-literals",
|
||||
}
|
||||
|
||||
/* eslint-disable no-unused-vars */
|
||||
class Postprocessor {
|
||||
constructor(name, fn) {
|
||||
this.name = name
|
||||
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 ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
|
||||
|
||||
module.exports.FIND_HBS_REGEX = /{{([^{}])+}}/g
|
||||
module.exports.FIND_HBS_REGEX = /{{([^{].*?)}}/g
|
||||
|
||||
module.exports.isAlphaNumeric = char => {
|
||||
return char.match(ALPHA_NUMERIC_REGEX)
|
||||
|
|
|
@ -259,3 +259,19 @@ describe("test the comparison helpers", () => {
|
|||
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