From 9076a1d4e495b9343081236fe44eb5bef9384d3b Mon Sep 17 00:00:00 2001 From: Junyi Date: Fri, 2 Jun 2023 19:35:11 +0700 Subject: [PATCH] fix(evaluators): fix number lead key in variable path (#1976) --- packages/core/evaluators/src/server/__tests__/index.test.ts | 5 +++++ packages/core/evaluators/src/utils/index.ts | 2 +- 2 files changed, 6 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 8d47c9080..aa5cd2a18 100644 --- a/packages/core/evaluators/src/server/__tests__/index.test.ts +++ b/packages/core/evaluators/src/server/__tests__/index.test.ts @@ -54,4 +54,9 @@ describe('evaluate', () => { const result = mathEval('{{a.1}}', { a: { 1: 1 } }); expect(result).toBe(1); }); + + it('number lead string path to object member (formula.js)', () => { + const result = formulaEval('{{a.1a}}', { a: { '1a': 1 } }); + expect(result).toBe(1); + }); }); diff --git a/packages/core/evaluators/src/utils/index.ts b/packages/core/evaluators/src/utils/index.ts index 4727dcc2a..37665ba5f 100644 --- a/packages/core/evaluators/src/utils/index.ts +++ b/packages/core/evaluators/src/utils/index.ts @@ -23,7 +23,7 @@ function replaceNumberIndex(path: string, scope: Scope): string { for (let i = 0; i < segments.length; i++) { const p = segments[i]; - if (p.match(/^\d+$/)) { + if (p[0] && '0123456789'.indexOf(p[0]) > -1) { paths.push(Array.isArray(get(scope, segments.slice(0, i))) ? `[${p}]` : `["${p}"]`); } else { if (i) {