mirror of
https://github.com/frappe/erpnext.git
synced 2026-08-14 07:01:56 +00:00
fix(banking): use custom renderer for translated strings and parser for rules (#56643)
fix(banking): use custom renderer for translated strings and parser for formula evaluation
(cherry picked from commit 8447f551e7)
Co-authored-by: Nikhil Kothari <nik.kothari22@live.com>
27 lines
648 B
TypeScript
27 lines
648 B
TypeScript
import { Parser } from 'safe-expr-eval'
|
|
|
|
const parser = new Parser()
|
|
|
|
const PLAIN_NUMBER_PATTERN = /^-?\d+(\.\d+)?$/
|
|
|
|
export function evaluateAmountFormula(expression: string, transactionAmount: number): number {
|
|
const trimmed = expression.trim()
|
|
if (!trimmed) {
|
|
return 0
|
|
}
|
|
|
|
if (PLAIN_NUMBER_PATTERN.test(trimmed)) {
|
|
return Number(trimmed)
|
|
}
|
|
|
|
try {
|
|
const result = parser.parse(trimmed).evaluate({ transaction_amount: transactionAmount })
|
|
if (typeof result !== 'number' || !Number.isFinite(result)) {
|
|
return 0
|
|
}
|
|
return result
|
|
} catch {
|
|
return 0
|
|
}
|
|
}
|