2022-01-14 07:49:24 +01:00
|
|
|
import { Table, Variable } from 'models'
|
|
|
|
import { isDefined } from 'utils'
|
|
|
|
|
|
|
|
const safeEval = eval
|
|
|
|
|
|
|
|
export const stringContainsVariable = (str: string): boolean =>
|
|
|
|
/\{\{(.*?)\}\}/g.test(str)
|
|
|
|
|
2022-01-19 14:25:15 +01:00
|
|
|
export const parseVariables = ({
|
|
|
|
text,
|
|
|
|
variables,
|
|
|
|
}: {
|
|
|
|
text?: string
|
2022-01-14 07:49:24 +01:00
|
|
|
variables: Table<Variable>
|
2022-01-19 14:25:15 +01:00
|
|
|
}): string => {
|
|
|
|
if (!text || text === '') return ''
|
2022-01-14 07:49:24 +01:00
|
|
|
return text.replace(/\{\{(.*?)\}\}/g, (_, fullVariableString) => {
|
|
|
|
const matchedVarName = fullVariableString.replace(/{{|}}/g, '')
|
|
|
|
const matchedVariableId = variables.allIds.find((variableId) => {
|
|
|
|
const variable = variables.byId[variableId]
|
|
|
|
return matchedVarName === variable.name && isDefined(variable.value)
|
|
|
|
})
|
|
|
|
return variables.byId[matchedVariableId ?? '']?.value ?? ''
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isMathFormula = (str?: string) =>
|
|
|
|
['*', '/', '+', '-'].some((val) => str && str.includes(val))
|
|
|
|
|
|
|
|
export const evaluateExpression = (str: string) => {
|
|
|
|
let result = replaceCommasWithDots(str)
|
|
|
|
try {
|
|
|
|
const evaluatedNumber = safeEval(result) as number
|
|
|
|
if (countDecimals(evaluatedNumber) > 2) {
|
|
|
|
return evaluatedNumber.toFixed(2)
|
|
|
|
}
|
|
|
|
return evaluatedNumber.toString()
|
|
|
|
} catch (err) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-01-19 14:25:15 +01:00
|
|
|
|
|
|
|
export const parseVariablesInObject = (
|
|
|
|
object: { [key: string]: string | number },
|
|
|
|
variables: Table<Variable>
|
|
|
|
) =>
|
|
|
|
Object.keys(object).reduce((newObj, key) => {
|
|
|
|
const currentValue = object[key]
|
|
|
|
return {
|
|
|
|
...newObj,
|
|
|
|
[key]:
|
|
|
|
typeof currentValue === 'string'
|
|
|
|
? parseVariables({ text: currentValue, variables })
|
|
|
|
: currentValue,
|
|
|
|
}
|
|
|
|
}, {})
|