1
0
Fork 0
mirror of synced 2024-06-02 18:44:54 +12:00
budibase/packages/client/src/utils/enrichDataBinding.js
2022-01-31 19:02:59 +00:00

29 lines
933 B
JavaScript

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 = /{{.*}}/
/**
* Enriches a given input with a row from the database.
*/
export const enrichDataBinding = async (input, context) => {
// 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)) {
return input
}
return processString(input, context)
}
/**
* 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) => {
return processObjectSync(Helpers.cloneDeep(props), context, { cache: true })
}