1
0
Fork 0
mirror of synced 2024-09-12 15:37:31 +12:00
budibase/packages/client/src/utils/enrichDataBinding.js

31 lines
836 B
JavaScript
Raw Normal View History

import { processString } 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 = (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
}
/**
* 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
}