From 5076c2f064bb14e351a253a063299304991d3edc Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 21 Jan 2021 17:56:00 +0000 Subject: [PATCH] Changing up how processors work to make it a bit easier to add to them. --- packages/string-templates/src/index.js | 6 ++-- .../string-templates/src/processors/index.js | 26 +++++++++++++++++ .../{custom => processors}/postprocessor.js | 4 +++ .../{custom => processors}/preprocessor.js | 29 +------------------ packages/string-templates/src/utilities.js | 4 +++ 5 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 packages/string-templates/src/processors/index.js rename packages/string-templates/src/{custom => processors}/postprocessor.js (52%) rename packages/string-templates/src/{custom => processors}/preprocessor.js (68%) diff --git a/packages/string-templates/src/index.js b/packages/string-templates/src/index.js index c54d3b9429..2636752488 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 { preprocess } = require("./custom/preprocessor") +const processors = require("./processors") const { cloneDeep } = require("lodash/fp") const { removeNull } = require("./utilities") @@ -88,10 +88,10 @@ module.exports.processStringSync = (string, context) => { throw "Cannot process non-string types." } let template - string = preprocess(string) + string = processors.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(clonedContext) + return processors.postprocess(template(clonedContext)) } /** diff --git a/packages/string-templates/src/processors/index.js b/packages/string-templates/src/processors/index.js new file mode 100644 index 0000000000..d781de53ad --- /dev/null +++ b/packages/string-templates/src/processors/index.js @@ -0,0 +1,26 @@ +const { FIND_HBS_REGEX } = require("../utilities") +const preprocessor = require("./preprocessor") +const postprocessor = require("./postprocessor") + +function process(string, processors) { + for (let processor of processors) { + // re-run search each time incase previous processor updated/removed a match + let regex = new RegExp(FIND_HBS_REGEX) + let matches = string.match(regex) + if (matches == null) { + continue + } + for (let match of matches) { + string = processor.process(string, match) + } + } + return string +} + +module.exports.preprocess = string => { + return process(string, preprocessor.processors) +} + +module.exports.postprocess = string => { + return process(string, postprocessor.processors) +} \ No newline at end of file diff --git a/packages/string-templates/src/custom/postprocessor.js b/packages/string-templates/src/processors/postprocessor.js similarity index 52% rename from packages/string-templates/src/custom/postprocessor.js rename to packages/string-templates/src/processors/postprocessor.js index 4dad27ab98..76a962c52f 100644 --- a/packages/string-templates/src/custom/postprocessor.js +++ b/packages/string-templates/src/processors/postprocessor.js @@ -1,6 +1,10 @@ +const { FIND_HBS_REGEX } = require("../utilities") + class Postprocessor { constructor(name, fn) { this.name = name this.fn = fn } } + +module.exports.processors = [] diff --git a/packages/string-templates/src/custom/preprocessor.js b/packages/string-templates/src/processors/preprocessor.js similarity index 68% rename from packages/string-templates/src/custom/preprocessor.js rename to packages/string-templates/src/processors/preprocessor.js index a86af5cd43..0d71445788 100644 --- a/packages/string-templates/src/custom/preprocessor.js +++ b/packages/string-templates/src/processors/preprocessor.js @@ -3,7 +3,6 @@ const { swapStrings, isAlphaNumeric, FIND_HBS_REGEX, - includesAny, } = require("../utilities") const PreprocessorNames = { @@ -25,7 +24,7 @@ class Preprocessor { } } -const PROCESSORS = [ +module.exports.processors = [ new Preprocessor(PreprocessorNames.SWAP_TO_DOT, statement => { let startBraceIdx = statement.indexOf("[") let lastIdx = 0 @@ -90,29 +89,3 @@ const PROCESSORS = [ 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 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 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 processed up handlebars statements as required. - */ -module.exports.preprocess = string => { - for (let processor of PROCESSORS) { - // re-run search each time incase previous processor updated/removed a match - let regex = new RegExp(FIND_HBS_REGEX) - let matches = string.match(regex) - if (matches == null) { - continue - } - for (let match of matches) { - string = processor.process(string, match) - } - } - return string -} diff --git a/packages/string-templates/src/utilities.js b/packages/string-templates/src/utilities.js index 8b45820250..b973b56acd 100644 --- a/packages/string-templates/src/utilities.js +++ b/packages/string-templates/src/utilities.js @@ -21,3 +21,7 @@ module.exports.removeNull = obj => { ]) ) } + +module.exports.findHbsStatements = string => { + +} \ No newline at end of file