2021-01-21 16:50:46 +01:00
|
|
|
const { HelperNames } = require("../helpers")
|
2021-01-21 19:08:04 +01:00
|
|
|
const { swapStrings, isAlphaNumeric } = require("../utilities")
|
2021-01-20 19:12:16 +01:00
|
|
|
|
2021-01-21 14:48:23 +01:00
|
|
|
const PreprocessorNames = {
|
|
|
|
SWAP_TO_DOT: "swap-to-dot-notation",
|
|
|
|
HANDLE_SPACES: "handle-spaces-in-properties",
|
|
|
|
FINALISE: "finalise",
|
|
|
|
}
|
|
|
|
|
2021-01-21 19:08:04 +01:00
|
|
|
/* eslint-disable no-unused-vars */
|
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 18:56:00 +01:00
|
|
|
module.exports.processors = [
|
2021-01-21 14:48:23 +01:00
|
|
|
new Preprocessor(PreprocessorNames.SWAP_TO_DOT, statement => {
|
2021-01-21 12:37:16 +01:00
|
|
|
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 14:48:23 +01:00
|
|
|
new Preprocessor(PreprocessorNames.HANDLE_SPACES, statement => {
|
2021-01-21 12:37:16 +01:00
|
|
|
// exclude helpers and brackets, regex will only find double brackets
|
2021-01-21 16:50:46 +01:00
|
|
|
const exclusions = HelperNames()
|
2021-01-21 12:37:16 +01:00
|
|
|
// find all the parts split by spaces
|
2021-01-21 18:30:51 +01:00
|
|
|
const splitBySpaces = statement
|
|
|
|
.split(" ")
|
|
|
|
.filter(el => el !== "{{" && el !== "}}")
|
2021-01-21 16:50:46 +01:00
|
|
|
// remove braces if they are found and weren't spaced out
|
|
|
|
splitBySpaces[0] = splitBySpaces[0].replace("{", "")
|
2021-01-21 18:30:51 +01:00
|
|
|
splitBySpaces[splitBySpaces.length - 1] = splitBySpaces[
|
|
|
|
splitBySpaces.length - 1
|
|
|
|
].replace("}", "")
|
2021-01-21 12:37:16 +01:00
|
|
|
// 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, "]")
|
|
|
|
}),
|
|
|
|
|
2021-01-21 14:48:23 +01:00
|
|
|
new Preprocessor(Preprocessor.FINALISE, statement => {
|
2021-01-21 12:37:16 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-01-21 16:50:46 +01:00
|
|
|
const possibleHelper = insideStatement.split(" ")[0]
|
|
|
|
if (HelperNames().some(option => possibleHelper === option)) {
|
2021-01-21 12:37:16 +01:00
|
|
|
insideStatement = `(${insideStatement})`
|
|
|
|
}
|
|
|
|
return `{{ all ${insideStatement} }}`
|
2021-01-21 14:48:23 +01:00
|
|
|
}),
|
2021-01-21 12:37:16 +01:00
|
|
|
]
|