diff --git a/packages/string-templates/src/custom/postprocessor.js b/packages/string-templates/src/custom/postprocessor.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/string-templates/src/cleaning.js b/packages/string-templates/src/custom/preprocessor.js similarity index 75% rename from packages/string-templates/src/cleaning.js rename to packages/string-templates/src/custom/preprocessor.js index 671382da10..f562c67c6a 100644 --- a/packages/string-templates/src/cleaning.js +++ b/packages/string-templates/src/custom/preprocessor.js @@ -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 -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) { +function handleProcessor(string, match, fn) { const output = fn(match) const idx = string.indexOf(match) return swapStrings(string, idx, match.length, output) @@ -60,31 +50,34 @@ function finalise(statement) { if (insideStatement.charAt(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 * 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 - * 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 - * 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. - * @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) => { - let cleaners = [swapToDotNotation, handleSpacesInProperties, finalise] - for (let cleaner of cleaners) { +module.exports.preprocess = (string) => { + let preprocessors = [swapToDotNotation, handleSpacesInProperties, finalise] + for (let processor of preprocessors) { // 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) if (matches == null) { continue } for (let match of matches) { - string = handleCleaner(string, match, cleaner) + string = handleProcessor(string, match, processor) } } return string diff --git a/packages/string-templates/src/index.js b/packages/string-templates/src/index.js index 4bde727ea5..2ca00135b8 100644 --- a/packages/string-templates/src/index.js +++ b/packages/string-templates/src/index.js @@ -1,6 +1,6 @@ const handlebars = require("handlebars") const { registerAll } = require("./helpers/index") -const { cleanHandlebars } = require("./cleaning") +const { preprocess } = require("./custom/preprocessor") const hbsInstance = handlebars.create() registerAll(hbsInstance) @@ -84,8 +84,7 @@ module.exports.processStringSync = (string, context) => { console.log(string) console.log(context) let template - string = cleanHandlebars(string) - console.log(string) + string = preprocess(string) // this does not throw an error when template can't be fulfilled, have to try correct beforehand template = hbsInstance.compile(string) return template(context) diff --git a/packages/string-templates/src/utilities.js b/packages/string-templates/src/utilities.js new file mode 100644 index 0000000000..c7179aac75 --- /dev/null +++ b/packages/string-templates/src/utilities.js @@ -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)) +} diff --git a/packages/string-templates/test/helpers.spec.js b/packages/string-templates/test/helpers.spec.js index a73000ac08..8eb64d9838 100644 --- a/packages/string-templates/test/helpers.spec.js +++ b/packages/string-templates/test/helpers.spec.js @@ -9,4 +9,5 @@ describe("test the custom helpers we have applied", () => { }) expect(output).toBe("object is {\"a\":1}") }) + }) \ No newline at end of file