2
0

feat(engine): 🚸 Always evaluate Set variable

This commit is contained in:
Baptiste Arnaud
2022-03-02 18:58:10 +01:00
parent 7045c02a15
commit a5a1fef597
3 changed files with 8 additions and 23 deletions

View File

@@ -9,7 +9,7 @@ export const SetVariableContent = ({ step }: { step: SetVariableStep }) => {
typebot?.variables.find(byId(step.options.variableId))?.name ?? '' typebot?.variables.find(byId(step.options.variableId))?.name ?? ''
const expression = step.options.expressionToEvaluate ?? '' const expression = step.options.expressionToEvaluate ?? ''
return ( return (
<Text color={'gray.500'}> <Text color={'gray.500'} isTruncated>
{variableName === '' && expression === '' {variableName === '' && expression === ''
? 'Click to edit...' ? 'Click to edit...'
: `${variableName} ${expression ? `= ${expression}` : ``}`} : `${variableName} ${expression ? `= ${expression}` : ``}`}

View File

@@ -11,7 +11,7 @@ import {
} from 'models' } from 'models'
import { isDefined, isNotDefined } from 'utils' import { isDefined, isNotDefined } from 'utils'
import { sanitizeUrl } from './utils' import { sanitizeUrl } from './utils'
import { isMathFormula, evaluateExpression, parseVariables } from './variable' import { evaluateExpression, parseVariables } from './variable'
type EdgeId = string type EdgeId = string
@@ -38,9 +38,9 @@ const executeSetVariable = (
if (!step.options?.variableId || !step.options.expressionToEvaluate) if (!step.options?.variableId || !step.options.expressionToEvaluate)
return step.outgoingEdgeId return step.outgoingEdgeId
const expression = step.options.expressionToEvaluate const expression = step.options.expressionToEvaluate
const evaluatedExpression = isMathFormula(expression) const evaluatedExpression = evaluateExpression(
? evaluateExpression(parseVariables(variables)(expression)) parseVariables(variables)(expression)
: expression )
updateVariableValue(step.options.variableId, evaluatedExpression) updateVariableValue(step.options.variableId, evaluatedExpression)
return step.outgoingEdgeId return step.outgoingEdgeId
} }

View File

@@ -20,30 +20,15 @@ export const parseVariables =
}) })
} }
export const isMathFormula = (str?: string) =>
['*', '/', '+', '-'].some((val) => str && str.includes(val))
export const evaluateExpression = (str: string) => { export const evaluateExpression = (str: string) => {
const result = replaceCommasWithDots(str)
try { try {
const evaluatedNumber = safeEval(result) as number const evaluatedResult = safeEval(str)
if (countDecimals(evaluatedNumber) > 2) { return evaluatedResult.toString()
return evaluatedNumber.toFixed(2)
}
return evaluatedNumber.toString()
} catch (err) { } 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 = ( export const parseVariablesInObject = (
object: { [key: string]: string | number }, object: { [key: string]: string | number },
variables: Variable[] variables: Variable[]