1
0
Fork 0
mirror of synced 2024-07-03 13:30:46 +12:00

Merge branch 'feature/handlebars-migration' of github.com:Budibase/budibase into form-builder

This commit is contained in:
Andrew Kingston 2021-01-21 11:32:26 +00:00
commit 34cc4ac5bf
5 changed files with 33 additions and 25 deletions

View file

@ -1,17 +1,7 @@
const { HelperFunctions } = require("./helpers/index")
const { HelperFunctions } = require("../helpers")
const { swapStrings, isAlphaNumeric, FIND_HBS_REGEX, includesAny } = require("../utilities")
const HBS_CLEANING_REGEX = /{{[^}}]*}}/g
const ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
function isAlphaNumeric(char) {
return char.match(ALPHA_NUMERIC_REGEX)
}
function swapStrings(string, start, length, swap) {
return string.slice(0, start) + swap + string.slice(start + length)
}
function handleCleaner(string, match, fn) {
function handleProcessor(string, match, fn) {
const output = fn(match)
const idx = string.indexOf(match)
return swapStrings(string, idx, match.length, output)
@ -60,31 +50,34 @@ function finalise(statement) {
if (insideStatement.charAt(insideStatement.length - 1) === " ") {
insideStatement = insideStatement.slice(0, insideStatement.length - 1)
}
return `{{ all (${insideStatement}) }}`
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
* 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 will clean up
* 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 cleanup that has been deemed necessary for the system.
* 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 cleaned up handlebars statements as required.
* @returns {string} The string that was input with processed up handlebars statements as required.
*/
module.exports.cleanHandlebars = (string) => {
let cleaners = [swapToDotNotation, handleSpacesInProperties, finalise]
for (let cleaner of cleaners) {
module.exports.preprocess = (string) => {
let preprocessors = [swapToDotNotation, handleSpacesInProperties, finalise]
for (let processor of preprocessors) {
// re-run search each time incase previous cleaner update/removed a match
let regex = new RegExp(HBS_CLEANING_REGEX)
let regex = new RegExp(FIND_HBS_REGEX)
let matches = string.match(regex)
if (matches == null) {
continue
}
for (let match of matches) {
string = handleCleaner(string, match, cleaner)
string = handleProcessor(string, match, processor)
}
}
return string

View file

@ -1,6 +1,6 @@
const handlebars = require("handlebars")
const { registerAll } = require("./helpers/index")
const { cleanHandlebars } = require("./cleaning")
const { preprocess } = require("./custom/preprocessor")
const hbsInstance = handlebars.create()
registerAll(hbsInstance)
@ -84,8 +84,7 @@ module.exports.processStringSync = (string, context) => {
console.log(string)
console.log(context)
let template
string = cleanHandlebars(string)
console.log(string)
string = preprocess(string)
// this does not throw an error when template can't be fulfilled, have to try correct beforehand
template = hbsInstance.compile(string)
return template(context)

View file

@ -0,0 +1,15 @@
const ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
module.exports.FIND_HBS_REGEX = /{{[^}}]*}}/g
module.exports.isAlphaNumeric = (char) => {
return char.match(ALPHA_NUMERIC_REGEX)
}
module.exports.swapStrings = (string, start, length, swap) => {
return string.slice(0, start) + swap + string.slice(start + length)
}
module.exports.includesAny = (string, options) => {
return options.some(option => string.includes(option))
}

View file

@ -9,4 +9,5 @@ describe("test the custom helpers we have applied", () => {
})
expect(output).toBe("object is {\"a\":1}")
})
})