feat(engine): 🚸 Always evaluate Set variable
This commit is contained in:
@ -9,7 +9,7 @@ export const SetVariableContent = ({ step }: { step: SetVariableStep }) => {
|
||||
typebot?.variables.find(byId(step.options.variableId))?.name ?? ''
|
||||
const expression = step.options.expressionToEvaluate ?? ''
|
||||
return (
|
||||
<Text color={'gray.500'}>
|
||||
<Text color={'gray.500'} isTruncated>
|
||||
{variableName === '' && expression === ''
|
||||
? 'Click to edit...'
|
||||
: `${variableName} ${expression ? `= ${expression}` : ``}`}
|
||||
|
@ -11,7 +11,7 @@ import {
|
||||
} from 'models'
|
||||
import { isDefined, isNotDefined } from 'utils'
|
||||
import { sanitizeUrl } from './utils'
|
||||
import { isMathFormula, evaluateExpression, parseVariables } from './variable'
|
||||
import { evaluateExpression, parseVariables } from './variable'
|
||||
|
||||
type EdgeId = string
|
||||
|
||||
@ -38,9 +38,9 @@ const executeSetVariable = (
|
||||
if (!step.options?.variableId || !step.options.expressionToEvaluate)
|
||||
return step.outgoingEdgeId
|
||||
const expression = step.options.expressionToEvaluate
|
||||
const evaluatedExpression = isMathFormula(expression)
|
||||
? evaluateExpression(parseVariables(variables)(expression))
|
||||
: expression
|
||||
const evaluatedExpression = evaluateExpression(
|
||||
parseVariables(variables)(expression)
|
||||
)
|
||||
updateVariableValue(step.options.variableId, evaluatedExpression)
|
||||
return step.outgoingEdgeId
|
||||
}
|
||||
|
@ -20,30 +20,15 @@ export const parseVariables =
|
||||
})
|
||||
}
|
||||
|
||||
export const isMathFormula = (str?: string) =>
|
||||
['*', '/', '+', '-'].some((val) => str && str.includes(val))
|
||||
|
||||
export const evaluateExpression = (str: string) => {
|
||||
const result = replaceCommasWithDots(str)
|
||||
try {
|
||||
const evaluatedNumber = safeEval(result) as number
|
||||
if (countDecimals(evaluatedNumber) > 2) {
|
||||
return evaluatedNumber.toFixed(2)
|
||||
}
|
||||
return evaluatedNumber.toString()
|
||||
const evaluatedResult = safeEval(str)
|
||||
return evaluatedResult.toString()
|
||||
} catch (err) {
|
||||
return result
|
||||
return str
|
||||
}
|
||||
}
|
||||
|
||||
const replaceCommasWithDots = (str: string) =>
|
||||
str.replace(new RegExp(/(\d+)(,)(\d+)/, 'g'), '$1.$3')
|
||||
|
||||
const countDecimals = (value: number) => {
|
||||
if (value % 1 != 0) return value.toString().split('.')[1].length
|
||||
return 0
|
||||
}
|
||||
|
||||
export const parseVariablesInObject = (
|
||||
object: { [key: string]: string | number },
|
||||
variables: Variable[]
|
||||
|
Reference in New Issue
Block a user