Changing up how processors work to make it a bit easier to add to them.
This commit is contained in:
parent
1a87b2caaf
commit
5076c2f064
|
@ -1,6 +1,6 @@
|
||||||
const handlebars = require("handlebars")
|
const handlebars = require("handlebars")
|
||||||
const { registerAll } = require("./helpers/index")
|
const { registerAll } = require("./helpers/index")
|
||||||
const { preprocess } = require("./custom/preprocessor")
|
const processors = require("./processors")
|
||||||
const { cloneDeep } = require("lodash/fp")
|
const { cloneDeep } = require("lodash/fp")
|
||||||
const { removeNull } = require("./utilities")
|
const { removeNull } = require("./utilities")
|
||||||
|
|
||||||
|
@ -88,10 +88,10 @@ module.exports.processStringSync = (string, context) => {
|
||||||
throw "Cannot process non-string types."
|
throw "Cannot process non-string types."
|
||||||
}
|
}
|
||||||
let template
|
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
|
// 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(clonedContext)
|
return processors.postprocess(template(clonedContext))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -1,6 +1,10 @@
|
||||||
|
const { FIND_HBS_REGEX } = require("../utilities")
|
||||||
|
|
||||||
class Postprocessor {
|
class Postprocessor {
|
||||||
constructor(name, fn) {
|
constructor(name, fn) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.fn = fn
|
this.fn = fn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.processors = []
|
|
@ -3,7 +3,6 @@ const {
|
||||||
swapStrings,
|
swapStrings,
|
||||||
isAlphaNumeric,
|
isAlphaNumeric,
|
||||||
FIND_HBS_REGEX,
|
FIND_HBS_REGEX,
|
||||||
includesAny,
|
|
||||||
} = require("../utilities")
|
} = require("../utilities")
|
||||||
|
|
||||||
const PreprocessorNames = {
|
const PreprocessorNames = {
|
||||||
|
@ -25,7 +24,7 @@ class Preprocessor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const PROCESSORS = [
|
module.exports.processors = [
|
||||||
new Preprocessor(PreprocessorNames.SWAP_TO_DOT, statement => {
|
new Preprocessor(PreprocessorNames.SWAP_TO_DOT, statement => {
|
||||||
let startBraceIdx = statement.indexOf("[")
|
let startBraceIdx = statement.indexOf("[")
|
||||||
let lastIdx = 0
|
let lastIdx = 0
|
||||||
|
@ -90,29 +89,3 @@ const PROCESSORS = [
|
||||||
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
|
|
||||||
* 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
|
|
||||||
}
|
|
|
@ -21,3 +21,7 @@ module.exports.removeNull = obj => {
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.findHbsStatements = string => {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue