1
0
Fork 0
mirror of synced 2024-08-19 20:11:42 +12:00
budibase/packages/client/src/utils/enrichDataBinding.js

29 lines
933 B
JavaScript
Raw Normal View History

2022-02-01 08:02:59 +13:00
import { Helpers } from "@budibase/bbui"
import { processString, processObjectSync } from "@budibase/string-templates"
// 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.
*/
export const enrichDataBinding = async (input, context) => {
2020-11-19 22:24:58 +13:00
// Only accept string inputs
if (!input || typeof input !== "string") {
return input
}
// 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
}
return processString(input, context)
2020-11-19 22:24:58 +13: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.
*/
export const enrichDataBindings = (props, context) => {
2022-02-01 08:02:59 +13:00
return processObjectSync(Helpers.cloneDeep(props), context, { cache: true })
}