2021-01-21 12:30:53 +01:00
|
|
|
const { HelperFunctions } = require("../helpers")
|
2021-01-21 12:32:26 +01:00
|
|
|
const {
|
|
|
|
swapStrings,
|
|
|
|
isAlphaNumeric,
|
|
|
|
FIND_HBS_REGEX,
|
|
|
|
includesAny,
|
|
|
|
} = require("../utilities")
|
2021-01-20 19:12:16 +01:00
|
|
|
|
2021-01-21 12:37:16 +01:00
|
|
|
class Preprocessor {
|
|
|
|
constructor(name, fn) {
|
|
|
|
this.name = name
|
|
|
|
this.fn = fn
|
|
|
|
}
|
2021-01-20 19:12:16 +01:00
|
|
|
|
2021-01-21 12:37:16 +01:00
|
|
|
process(fullString, statement) {
|
|
|
|
const output = this.fn(statement)
|
|
|
|
const idx = fullString.indexOf(statement)
|
|
|
|
return swapStrings(fullString, idx, statement.length, output)
|
2021-01-20 19:12:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:37:16 +01:00
|
|
|
const PROCESSORS = [
|
|
|
|
new Preprocessor("swap-to-dot-notation", statement => {
|
|
|
|
let startBraceIdx = statement.indexOf("[")
|
|
|
|
let lastIdx = 0
|
|
|
|
while (startBraceIdx !== -1) {
|
|
|
|
// if the character previous to the literal specifier is alpha-numeric this should happen
|
|
|
|
if (isAlphaNumeric(statement.charAt(startBraceIdx - 1))) {
|
|
|
|
statement = swapStrings(statement, startBraceIdx + lastIdx, 1, ".[")
|
|
|
|
}
|
|
|
|
lastIdx = startBraceIdx + 1
|
|
|
|
startBraceIdx = statement.substring(lastIdx + 1).indexOf("[")
|
2021-01-20 19:12:16 +01:00
|
|
|
}
|
2021-01-21 12:37:16 +01:00
|
|
|
return statement
|
|
|
|
}),
|
2021-01-20 19:12:16 +01:00
|
|
|
|
2021-01-21 12:37:16 +01:00
|
|
|
new Preprocessor("handle-spaces-in-properties", statement => {
|
|
|
|
// exclude helpers and brackets, regex will only find double brackets
|
|
|
|
const exclusions = HelperFunctions.concat(["{{", "}}"])
|
|
|
|
// find all the parts split by spaces
|
|
|
|
const splitBySpaces = statement.split(" ")
|
|
|
|
// remove the excluded elements
|
|
|
|
const propertyParts = splitBySpaces.filter(
|
|
|
|
part => exclusions.indexOf(part) === -1
|
|
|
|
)
|
|
|
|
// rebuild to get the full property
|
|
|
|
const fullProperty = propertyParts.join(" ")
|
|
|
|
// now work out the dot notation layers and split them up
|
|
|
|
const propertyLayers = fullProperty.split(".")
|
|
|
|
// find the layers which need to be wrapped and wrap them
|
|
|
|
for (let layer of propertyLayers) {
|
|
|
|
if (layer.indexOf(" ") !== -1) {
|
|
|
|
statement = swapStrings(
|
|
|
|
statement,
|
|
|
|
statement.indexOf(layer),
|
|
|
|
layer.length,
|
|
|
|
`[${layer}]`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove the edge case of double brackets being entered (in-case user already has specified)
|
|
|
|
return statement.replace(/\[\[/g, "[").replace(/]]/g, "]")
|
|
|
|
}),
|
|
|
|
|
|
|
|
new Preprocessor("finalise", statement => {
|
|
|
|
let insideStatement = statement.slice(2, statement.length - 2)
|
|
|
|
if (insideStatement.charAt(0) === " ") {
|
|
|
|
insideStatement = insideStatement.slice(1)
|
|
|
|
}
|
|
|
|
if (insideStatement.charAt(insideStatement.length - 1) === " ") {
|
|
|
|
insideStatement = insideStatement.slice(0, insideStatement.length - 1)
|
|
|
|
}
|
|
|
|
if (includesAny(insideStatement, HelperFunctions)) {
|
|
|
|
insideStatement = `(${insideStatement})`
|
|
|
|
}
|
|
|
|
return `{{ all ${insideStatement} }}`
|
|
|
|
})
|
|
|
|
]
|
2021-01-20 19:12:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2021-01-21 12:30:53 +01:00
|
|
|
* like operators. These are not supported by handlebars and therefore the statement will fail. This function pre-processes will
|
2021-01-20 19:12:16 +01:00
|
|
|
* the handlebars statement so it instead reads as "{{steps.0.revision}}" which is valid and will work. It may also be expanded
|
2021-01-21 12:30:53 +01:00
|
|
|
* to include any other handlebars statement pre-process that has been deemed necessary for the system.
|
2021-01-20 19:12:16 +01:00
|
|
|
*
|
|
|
|
* @param {string} string The string which *may* contain handlebars statements, it is OK if it does not contain any.
|
2021-01-21 12:30:53 +01:00
|
|
|
* @returns {string} The string that was input with processed up handlebars statements as required.
|
2021-01-20 19:12:16 +01:00
|
|
|
*/
|
2021-01-21 12:32:26 +01:00
|
|
|
module.exports.preprocess = string => {
|
2021-01-21 12:37:16 +01:00
|
|
|
for (let processor of PROCESSORS) {
|
|
|
|
// re-run search each time incase previous processor updated/removed a match
|
2021-01-21 12:30:53 +01:00
|
|
|
let regex = new RegExp(FIND_HBS_REGEX)
|
2021-01-20 19:12:16 +01:00
|
|
|
let matches = string.match(regex)
|
|
|
|
if (matches == null) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for (let match of matches) {
|
2021-01-21 12:37:16 +01:00
|
|
|
string = processor.process(string, match)
|
2021-01-20 19:12:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return string
|
2021-01-21 12:32:26 +01:00
|
|
|
}
|