2
0

🐛 (chat) Make sure objects are deeply parsed with variables value

This commit is contained in:
Baptiste Arnaud
2022-12-24 10:42:38 +01:00
parent 449080b0ad
commit 431ad7c78c
5 changed files with 42 additions and 56 deletions

View File

@@ -86,20 +86,31 @@ const jsonParse = (str: string) =>
.replace(/"/g, `\\"`)
.replace(/\\[^n"]/g, `\\\\ `)
export const parseVariablesInObject = (
object: { [key: string]: string | number },
variables: Variable[]
) =>
Object.keys(object).reduce((newObj, key) => {
const currentValue = object[key]
return {
...newObj,
[key]:
typeof currentValue === 'string'
? parseVariables(variables)(currentValue)
: currentValue,
}
}, {})
export const deepParseVariable =
(variables: Variable[]) =>
<T extends Record<string, unknown>>(object: T): T =>
Object.keys(object).reduce<T>((newObj, key) => {
const currentValue = object[key]
if (typeof currentValue === 'string')
return { ...newObj, [key]: parseVariables(variables)(currentValue) }
if (currentValue instanceof Object && currentValue.constructor === Object)
return {
...newObj,
[key]: deepParseVariable(variables)(
currentValue as Record<string, unknown>
),
}
if (currentValue instanceof Array)
return {
...newObj,
[key]: currentValue.map(deepParseVariable(variables)),
}
return { ...newObj, [key]: currentValue }
}, {} as T)
export const parsePrefilledVariables = (
variables: Typebot['variables'],