From 47535fcc8846785aa7b2ab82e4822c1380464749 Mon Sep 17 00:00:00 2001 From: liaoxuezhi <2698393+2betop@users.noreply.github.com> Date: Thu, 27 Jun 2024 19:32:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20fx=20=E7=9A=84=20re?= =?UTF-8?q?place=20=E6=96=B9=E6=B3=95=E5=8F=AF=E8=83=BD=E6=AD=BB=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E7=9A=84=E9=97=AE=E9=A2=98=20(#10539)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-formula/__tests__/evalute.test.ts | 9 +++++++++ packages/amis-formula/src/evalutor.ts | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/packages/amis-formula/__tests__/evalute.test.ts b/packages/amis-formula/__tests__/evalute.test.ts index 151d84a7a..233d54212 100644 --- a/packages/amis-formula/__tests__/evalute.test.ts +++ b/packages/amis-formula/__tests__/evalute.test.ts @@ -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' + ); +}); diff --git a/packages/amis-formula/src/evalutor.ts b/packages/amis-formula/src/evalutor.ts index a9a61ef89..416d40dfe 100644 --- a/packages/amis-formula/src/evalutor.ts +++ b/packages/amis-formula/src/evalutor.ts @@ -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;