From 92b28fa4118a1a3193f1f0bf43e340e7f806115b Mon Sep 17 00:00:00 2001 From: Junyi Date: Wed, 17 Apr 2024 18:40:26 +0800 Subject: [PATCH] fix(core): fix round bug in formula evaluator (#4070) --- .../core/evaluators/src/server/__tests__/index.test.ts | 10 ++++++++++ packages/core/evaluators/src/utils/formulajs.ts | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/core/evaluators/src/server/__tests__/index.test.ts b/packages/core/evaluators/src/server/__tests__/index.test.ts index d849c78b4..b53fdb58b 100644 --- a/packages/core/evaluators/src/server/__tests__/index.test.ts +++ b/packages/core/evaluators/src/server/__tests__/index.test.ts @@ -145,6 +145,16 @@ describe('evaluate', () => { const result = formulaEval('{{a.1a}}', { a: { '1a': 1 } }); expect(result).toBe(1); }); + + it('number greater than 32bit integer', () => { + const result = formulaEval('{{a}}', { a: 1609459200000 }); + expect(result).toBe(1609459200000); + }); + + it('ISO date string parsing by Date.parse', () => { + const result = formulaEval('Date.parse({{a}})', { a: '2021-01-01T00:00:00.000Z' }); + expect(result).toBe(1609459200000); + }); }); describe('string', () => { diff --git a/packages/core/evaluators/src/utils/formulajs.ts b/packages/core/evaluators/src/utils/formulajs.ts index 9f470824b..84838ed98 100644 --- a/packages/core/evaluators/src/utils/formulajs.ts +++ b/packages/core/evaluators/src/utils/formulajs.ts @@ -1,4 +1,5 @@ import * as functions from '@formulajs/formulajs'; +import { round } from 'mathjs'; import { evaluate } from '.'; @@ -12,7 +13,7 @@ export default evaluate.bind(function (expression: string, scope = {}) { if (Number.isNaN(result) || !Number.isFinite(result)) { return null; } - return functions.ROUND(result, 9); + return round(result, 9); } return result; }, {});