fix: 修复 fx 的 replace 方法可能死循环的问题 (#10539)

This commit is contained in:
liaoxuezhi 2024-06-27 19:32:01 +08:00 committed by GitHub
parent cd85d8c9ed
commit baaa1989f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 0 deletions

View File

@ -598,3 +598,12 @@ test('evalute:speical characters', () => {
expect(evaluate('${1 | 2}', {})).toBe(3);
expect(evaluate('${1 | abc}', {abc: 2})).toBe(3);
});
test('evalute:replace', () => {
const data = {};
expect(evaluate('${REPLACE("abcdefg", "abc", "cbd")}', data)).toBe('cbddefg');
expect(evaluate('${REPLACE("abcdefg", "efg", "efg")}', data)).toBe('abcdefg');
expect(evaluate('${REPLACE("abcdefg", "abc", "abcabc")}', data)).toBe(
'abcabcdefg'
);
});

View File

@ -1441,6 +1441,9 @@ export class Evaluator {
return result;
}
const shouldLoop = !(
typeof replace === 'string' && replace.includes(search)
);
while (true) {
const idx = result.indexOf(search);
@ -1452,6 +1455,10 @@ export class Evaluator {
result.substring(0, idx) +
replace +
result.substring(idx + search.length);
if (!shouldLoop) {
break;
}
}
return result;