Re-writing pre-processor to be a bit clearer.

This commit is contained in:
mike12345567 2021-01-21 11:37:16 +00:00
parent 8ecabc91cc
commit eb627c52d8
1 changed files with 67 additions and 59 deletions

View File

@ -6,13 +6,21 @@ const {
includesAny, includesAny,
} = require("../utilities") } = require("../utilities")
function handleProcessor(string, match, fn) { class Preprocessor {
const output = fn(match) constructor(name, fn) {
const idx = string.indexOf(match) this.name = name
return swapStrings(string, idx, match.length, output) this.fn = fn
}
process(fullString, statement) {
const output = this.fn(statement)
const idx = fullString.indexOf(statement)
return swapStrings(fullString, idx, statement.length, output)
}
} }
function swapToDotNotation(statement) { const PROCESSORS = [
new Preprocessor("swap-to-dot-notation", statement => {
let startBraceIdx = statement.indexOf("[") let startBraceIdx = statement.indexOf("[")
let lastIdx = 0 let lastIdx = 0
while (startBraceIdx !== -1) { while (startBraceIdx !== -1) {
@ -24,9 +32,9 @@ function swapToDotNotation(statement) {
startBraceIdx = statement.substring(lastIdx + 1).indexOf("[") startBraceIdx = statement.substring(lastIdx + 1).indexOf("[")
} }
return statement return statement
} }),
function handleSpacesInProperties(statement) { new Preprocessor("handle-spaces-in-properties", statement => {
// exclude helpers and brackets, regex will only find double brackets // exclude helpers and brackets, regex will only find double brackets
const exclusions = HelperFunctions.concat(["{{", "}}"]) const exclusions = HelperFunctions.concat(["{{", "}}"])
// find all the parts split by spaces // find all the parts split by spaces
@ -52,9 +60,9 @@ function handleSpacesInProperties(statement) {
} }
// remove the edge case of double brackets being entered (in-case user already has specified) // remove the edge case of double brackets being entered (in-case user already has specified)
return statement.replace(/\[\[/g, "[").replace(/]]/g, "]") return statement.replace(/\[\[/g, "[").replace(/]]/g, "]")
} }),
function finalise(statement) { new Preprocessor("finalise", statement => {
let insideStatement = statement.slice(2, statement.length - 2) let insideStatement = statement.slice(2, statement.length - 2)
if (insideStatement.charAt(0) === " ") { if (insideStatement.charAt(0) === " ") {
insideStatement = insideStatement.slice(1) insideStatement = insideStatement.slice(1)
@ -66,7 +74,8 @@ function finalise(statement) {
insideStatement = `(${insideStatement})` insideStatement = `(${insideStatement})`
} }
return `{{ all ${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
@ -80,16 +89,15 @@ function finalise(statement) {
* @returns {string} The string that was input with processed up handlebars statements as required. * @returns {string} The string that was input with processed up handlebars statements as required.
*/ */
module.exports.preprocess = string => { module.exports.preprocess = string => {
let preprocessors = [swapToDotNotation, handleSpacesInProperties, finalise] for (let processor of PROCESSORS) {
for (let processor of preprocessors) { // re-run search each time incase previous processor updated/removed a match
// re-run search each time incase previous cleaner update/removed a match
let regex = new RegExp(FIND_HBS_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 = handleProcessor(string, match, processor) string = processor.process(string, match)
} }
} }
return string return string