2021-01-19 19:44:29 +01:00
|
|
|
import { processString } from "@budibase/string-templates"
|
2020-12-02 14:49:24 +01:00
|
|
|
|
2021-01-19 19:44:29 +01:00
|
|
|
// Regex to test inputs with to see if they are likely candidates for template strings
|
|
|
|
const looksLikeTemplate = /{{.*}}/
|
2020-11-19 10:24:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
2021-01-19 19:44:29 +01:00
|
|
|
// Do a fast regex check if this looks like a template string
|
|
|
|
if (!looksLikeTemplate.test(input)) {
|
2020-11-19 10:24:58 +01:00
|
|
|
return input
|
|
|
|
}
|
2021-01-19 19:44:29 +01:00
|
|
|
return processString(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
|
|
|
|
}
|