1
0
Fork 0
mirror of synced 2024-08-31 17:51:11 +12:00
budibase/packages/builder/src/builderStore/replaceBindings.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-09-10 08:16:26 +12:00
export const CAPTURE_VAR_INSIDE_MUSTACHE = /{{([^}]+)}}/g
export function readableToRuntimeBinding(bindableProperties, textWithBindings) {
// Find all instances of mustasche
const boundValues = textWithBindings.match(CAPTURE_VAR_INSIDE_MUSTACHE)
2020-09-12 08:30:40 +12:00
let result = textWithBindings
2020-09-11 20:53:02 +12:00
// Replace readableBindings with runtimeBindings
2020-09-10 08:16:26 +12:00
boundValues &&
boundValues.forEach(boundValue => {
const binding = bindableProperties.find(({ readableBinding }) => {
return boundValue === `{{ ${readableBinding} }}`
})
if (binding) {
2020-09-12 08:30:40 +12:00
result = textWithBindings.replace(
2020-09-10 08:16:26 +12:00
boundValue,
`{{ ${binding.runtimeBinding} }}`
)
}
})
2020-09-12 08:30:40 +12:00
return result
2020-09-10 08:16:26 +12:00
}
export function runtimeToReadableBinding(bindableProperties, textWithBindings) {
let temp = textWithBindings
const boundValues =
(typeof textWithBindings === "string" &&
textWithBindings.match(CAPTURE_VAR_INSIDE_MUSTACHE)) ||
[]
2020-09-11 20:53:02 +12:00
// Replace runtimeBindings with readableBindings:
2020-09-10 08:16:26 +12:00
boundValues.forEach(v => {
const binding = bindableProperties.find(({ runtimeBinding }) => {
return v === `{{ ${runtimeBinding} }}`
})
if (binding) {
temp = temp.replace(v, `{{ ${binding.readableBinding} }}`)
}
})
return temp
}