Fixing issue found by cheeks with all helper trying to wrap helpers even if no other helpers present.

This commit is contained in:
mike12345567 2021-01-21 11:30:53 +00:00
parent afe0996b70
commit 3d33199f16
5 changed files with 33 additions and 24 deletions

View File

@ -1,17 +1,7 @@
const { HelperFunctions } = require("./helpers/index") const { HelperFunctions } = require("../helpers")
const { swapStrings, isAlphaNumeric, FIND_HBS_REGEX, includesAny } = require("../utilities")
const HBS_CLEANING_REGEX = /{{[^}}]*}}/g function handleProcessor(string, match, fn) {
const ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
function isAlphaNumeric(char) {
return char.match(ALPHA_NUMERIC_REGEX)
}
function swapStrings(string, start, length, swap) {
return string.slice(0, start) + swap + string.slice(start + length)
}
function handleCleaner(string, match, fn) {
const output = fn(match) const output = fn(match)
const idx = string.indexOf(match) const idx = string.indexOf(match)
return swapStrings(string, idx, match.length, output) return swapStrings(string, idx, match.length, output)
@ -60,31 +50,34 @@ function finalise(statement) {
if (insideStatement.charAt(insideStatement.length - 1) === " ") { if (insideStatement.charAt(insideStatement.length - 1) === " ") {
insideStatement = insideStatement.slice(0, insideStatement.length - 1) insideStatement = insideStatement.slice(0, insideStatement.length - 1)
} }
return `{{ all (${insideStatement}) }}` if (includesAny(insideStatement, HelperFunctions)) {
insideStatement = `(${insideStatement})`
}
return `{{ all ${insideStatement} }}`
} }
/** /**
* When running handlebars statements to execute on the context of the automation it possible user's may input handlebars * When running handlebars statements to execute on the context of the automation it possible user's may input handlebars
* in a few different forms, some of which are invalid but are logically valid. An example of this would be the handlebars * in a few different forms, some of which are invalid but are logically valid. An example of this would be the handlebars
* statement "{{steps[0].revision}}" here it is obvious the user is attempting to access an array or object using array * statement "{{steps[0].revision}}" here it is obvious the user is attempting to access an array or object using array
* like operators. These are not supported by handlebars and therefore the statement will fail. This function will clean up * like operators. These are not supported by handlebars and therefore the statement will fail. This function pre-processes will
* the handlebars statement so it instead reads as "{{steps.0.revision}}" which is valid and will work. It may also be expanded * the handlebars statement so it instead reads as "{{steps.0.revision}}" which is valid and will work. It may also be expanded
* to include any other handlebars statement cleanup that has been deemed necessary for the system. * to include any other handlebars statement pre-process that has been deemed necessary for the system.
* *
* @param {string} string The string which *may* contain handlebars statements, it is OK if it does not contain any. * @param {string} string The string which *may* contain handlebars statements, it is OK if it does not contain any.
* @returns {string} The string that was input with cleaned up handlebars statements as required. * @returns {string} The string that was input with processed up handlebars statements as required.
*/ */
module.exports.cleanHandlebars = (string) => { module.exports.preprocess = (string) => {
let cleaners = [swapToDotNotation, handleSpacesInProperties, finalise] let preprocessors = [swapToDotNotation, handleSpacesInProperties, finalise]
for (let cleaner of cleaners) { for (let processor of preprocessors) {
// re-run search each time incase previous cleaner update/removed a match // re-run search each time incase previous cleaner update/removed a match
let regex = new RegExp(HBS_CLEANING_REGEX) let regex = new RegExp(FIND_HBS_REGEX)
let matches = string.match(regex) let matches = string.match(regex)
if (matches == null) { if (matches == null) {
continue continue
} }
for (let match of matches) { for (let match of matches) {
string = handleCleaner(string, match, cleaner) string = handleProcessor(string, match, processor)
} }
} }
return string return string

View File

@ -1,6 +1,6 @@
const handlebars = require("handlebars") const handlebars = require("handlebars")
const { registerAll } = require("./helpers/index") const { registerAll } = require("./helpers/index")
const { cleanHandlebars } = require("./cleaning") const { preprocess } = require("./custom/preprocessor")
const hbsInstance = handlebars.create() const hbsInstance = handlebars.create()
registerAll(hbsInstance) registerAll(hbsInstance)
@ -84,7 +84,7 @@ module.exports.processStringSync = (string, context) => {
throw "Cannot process non-string types." throw "Cannot process non-string types."
} }
let template let template
string = cleanHandlebars(string) string = preprocess(string)
// this does not throw an error when template can't be fulfilled, have to try correct beforehand // this does not throw an error when template can't be fulfilled, have to try correct beforehand
template = hbsInstance.compile(string) template = hbsInstance.compile(string)
return template(context) return template(context)

View File

@ -0,0 +1,15 @@
const ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
module.exports.FIND_HBS_REGEX = /{{[^}}]*}}/g
module.exports.isAlphaNumeric = (char) => {
return char.match(ALPHA_NUMERIC_REGEX)
}
module.exports.swapStrings = (string, start, length, swap) => {
return string.slice(0, start) + swap + string.slice(start + length)
}
module.exports.includesAny = (string, options) => {
return options.some(option => string.includes(option))
}

View File

@ -9,4 +9,5 @@ describe("test the custom helpers we have applied", () => {
}) })
expect(output).toBe("object is {\"a\":1}") expect(output).toBe("object is {\"a\":1}")
}) })
}) })