Browse Source

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

gh-pages
mike12345567 6 years ago
parent
commit
eb627c52d8
  1. 126
      packages/string-templates/src/custom/preprocessor.js

126
packages/string-templates/src/custom/preprocessor.js

@ -6,67 +6,76 @@ 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
} }
function swapToDotNotation(statement) { process(fullString, statement) {
let startBraceIdx = statement.indexOf("[") const output = this.fn(statement)
let lastIdx = 0 const idx = fullString.indexOf(statement)
while (startBraceIdx !== -1) { return swapStrings(fullString, idx, statement.length, output)
// 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("[")
} }
return statement
} }
function handleSpacesInProperties(statement) { const PROCESSORS = [
// exclude helpers and brackets, regex will only find double brackets new Preprocessor("swap-to-dot-notation", statement => {
const exclusions = HelperFunctions.concat(["{{", "}}"]) let startBraceIdx = statement.indexOf("[")
// find all the parts split by spaces let lastIdx = 0
const splitBySpaces = statement.split(" ") while (startBraceIdx !== -1) {
// remove the excluded elements // if the character previous to the literal specifier is alpha-numeric this should happen
const propertyParts = splitBySpaces.filter( if (isAlphaNumeric(statement.charAt(startBraceIdx - 1))) {
part => exclusions.indexOf(part) === -1 statement = swapStrings(statement, startBraceIdx + lastIdx, 1, ".[")
) }
// rebuild to get the full property lastIdx = startBraceIdx + 1
const fullProperty = propertyParts.join(" ") startBraceIdx = statement.substring(lastIdx + 1).indexOf("[")
// 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}]`
)
} }
} return statement
// remove the edge case of double brackets being entered (in-case user already has specified) }),
return statement.replace(/\[\[/g, "[").replace(/]]/g, "]")
}
function finalise(statement) { new Preprocessor("handle-spaces-in-properties", statement => {
let insideStatement = statement.slice(2, statement.length - 2) // exclude helpers and brackets, regex will only find double brackets
if (insideStatement.charAt(0) === " ") { const exclusions = HelperFunctions.concat(["{{", "}}"])
insideStatement = insideStatement.slice(1) // find all the parts split by spaces
} const splitBySpaces = statement.split(" ")
if (insideStatement.charAt(insideStatement.length - 1) === " ") { // remove the excluded elements
insideStatement = insideStatement.slice(0, insideStatement.length - 1) const propertyParts = splitBySpaces.filter(
} part => exclusions.indexOf(part) === -1
if (includesAny(insideStatement, HelperFunctions)) { )
insideStatement = `(${insideStatement})` // rebuild to get the full property
} const fullProperty = propertyParts.join(" ")
return `{{ all ${insideStatement} }}` // 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} }}`
})
]
/** /**
* 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

Loading…
Cancel
Save