Skip to content

Commit

Permalink
feat(engine): 🚸 Always evaluate Set variable
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Mar 2, 2022
1 parent 7045c02 commit a5a1fef
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}` : ``}`}
Expand Down
8 changes: 4 additions & 4 deletions packages/bot-engine/src/services/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
Expand Down
21 changes: 3 additions & 18 deletions packages/bot-engine/src/services/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down

2 comments on commit a5a1fef

@vercel
Copy link

@vercel vercel bot commented on a5a1fef Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on a5a1fef Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

Please sign in to comment.