2
0

🚸 Automatically create variables when pasting groups to a new typebot

Closes #1587
This commit is contained in:
Baptiste Arnaud
2024-06-19 14:09:18 +02:00
parent 67f37c02a4
commit 4ab1803d39
7 changed files with 148 additions and 20 deletions

View File

@@ -0,0 +1,29 @@
import { Variable } from './types'
const variableNameRegex = /\{\{([^{}]+)\}\}/g
export const extractVariableIdReferencesInObject = (
obj: any,
existingVariables: Variable[]
): string[] =>
[...(JSON.stringify(obj).match(variableNameRegex) ?? [])].reduce<string[]>(
(acc, match) => {
const varName = match.slice(2, -2)
const id = existingVariables.find((v) => v.name === varName)?.id
if (!id || acc.find((accId) => accId === id)) return acc
return acc.concat(id)
},
[]
)
const variableIdRegex = /"\w*variableid":"([^"]+)"/gi
export const extractVariableIdsFromObject = (obj: any): string[] =>
[...(JSON.stringify(obj).match(variableIdRegex) ?? [])].reduce<string[]>(
(acc, match) => {
const id = variableIdRegex.exec(match)?.[1]
if (!id || acc.find((accId) => accId === id)) return acc
return acc.concat(id)
},
[]
)