2020-12-02 13:46:36 +01:00
|
|
|
import handlebars from "handlebars"
|
2020-11-19 10:24:58 +01:00
|
|
|
|
|
|
|
// Regex to test inputs with to see if they are likely candidates for mustache
|
|
|
|
const looksLikeMustache = /{{.*}}/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enriches a given input with a row from the database.
|
|
|
|
*/
|
2020-11-20 10:50:10 +01:00
|
|
|
export const enrichDataBinding = (input, context) => {
|
2020-11-19 10:24:58 +01:00
|
|
|
// Only accept string inputs
|
|
|
|
if (!input || typeof input !== "string") {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
// Do a fast regex check if this looks like a mustache string
|
|
|
|
if (!looksLikeMustache.test(input)) {
|
|
|
|
return input
|
|
|
|
}
|
2020-12-02 13:46:36 +01:00
|
|
|
return handlebars.render(input, context)
|
2020-11-19 10:24:58 +01:00
|
|
|
}
|
2020-11-25 10:50:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enriches each prop in a props object
|
|
|
|
*/
|
|
|
|
export const enrichDataBindings = (props, context) => {
|
|
|
|
let enrichedProps = {}
|
|
|
|
Object.entries(props).forEach(([key, value]) => {
|
|
|
|
enrichedProps[key] = enrichDataBinding(value, context)
|
|
|
|
})
|
|
|
|
return enrichedProps
|
|
|
|
}
|