2022-01-31 10:32:06 +01:00
|
|
|
import { cloneDeep } from "@budibase/bbui"
|
2021-06-01 15:59:42 +02:00
|
|
|
import { processString, processObjectSync } 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.
|
|
|
|
*/
|
2021-01-20 14:32:15 +01:00
|
|
|
export const enrichDataBinding = async (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
|
|
|
|
|
|
|
/**
|
2021-01-21 11:42:14 +01:00
|
|
|
* Recursively enriches all props in a props object and returns the new props.
|
|
|
|
* Props are deeply cloned so that no mutation is done to the source object.
|
2020-11-25 10:50:51 +01:00
|
|
|
*/
|
2021-06-01 15:59:42 +02:00
|
|
|
export const enrichDataBindings = (props, context) => {
|
2022-01-29 19:53:21 +01:00
|
|
|
return processObjectSync(cloneDeep(props), context, { cache: true })
|
2021-01-21 11:42:14 +01:00
|
|
|
}
|