fix: formula 的加法支持字符串拼接 (#5251)

This commit is contained in:
吴多益 2022-08-26 17:47:42 +08:00 committed by GitHub
parent c363e8ac2a
commit dce548c6b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

View File

@ -138,6 +138,8 @@ test('formula:math', () => {
expect(evalFormual('"3" - "3"')).toBe(0);
expect(evalFormual('AVG(4, "6", "10", 10, 10)')).toBe(8);
expect(evalFormual('MAX(4, "6", "10", 2, 3)')).toBe(10);
expect(evalFormual('"a" + "b"')).toBe('ab');
});
test('formula:text', () => {

View File

@ -230,6 +230,10 @@ export class Evaluator {
add(ast: {left: any; right: any}) {
const left = this.evalute(ast.left);
const right = this.evalute(ast.right);
// 如果有一个不是数字就变成字符串拼接
if (isNaN(left) || isNaN(right)) {
return left + right;
}
return stripNumber(this.formatNumber(left) + this.formatNumber(right));
}