fix(evaluators): fix number lead key in variable path (#1976)

This commit is contained in:
Junyi 2023-06-02 19:35:11 +07:00 committed by GitHub
parent 295faf569d
commit 9076a1d4e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -54,4 +54,9 @@ describe('evaluate', () => {
const result = mathEval('{{a.1}}', { a: { 1: 1 } }); const result = mathEval('{{a.1}}', { a: { 1: 1 } });
expect(result).toBe(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);
});
}); });

View File

@ -23,7 +23,7 @@ function replaceNumberIndex(path: string, scope: Scope): string {
for (let i = 0; i < segments.length; i++) { for (let i = 0; i < segments.length; i++) {
const p = segments[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}"]`); paths.push(Array.isArray(get(scope, segments.slice(0, i))) ? `[${p}]` : `["${p}"]`);
} else { } else {
if (i) { if (i) {