2021-01-20 07:44:29 +13:00
|
|
|
import { processString } from "@budibase/string-templates"
|
2020-12-03 02:49:24 +13:00
|
|
|
|
2021-01-20 07:44:29 +13:00
|
|
|
// Regex to test inputs with to see if they are likely candidates for template strings
|
|
|
|
const looksLikeTemplate = /{{.*}}/
|
2020-11-19 22:24:58 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enriches a given input with a row from the database.
|
|
|
|
*/
|
2020-11-20 22:50:10 +13:00
|
|
|
export const enrichDataBinding = (input, context) => {
|
2020-11-19 22:24:58 +13:00
|
|
|
// Only accept string inputs
|
|
|
|
if (!input || typeof input !== "string") {
|
|
|
|
return input
|
|
|
|
}
|
2021-01-20 07:44:29 +13:00
|
|
|
// Do a fast regex check if this looks like a template string
|
|
|
|
if (!looksLikeTemplate.test(input)) {
|
2020-11-19 22:24:58 +13:00
|
|
|
return input
|
|
|
|
}
|
2021-01-20 07:44:29 +13:00
|
|
|
return processString(input, context)
|
2020-11-19 22:24:58 +13:00
|
|
|
}
|
2020-11-25 22:50:51 +13: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
|
|
|
|
}
|