From 439682b2571baa65baeaeee54fcee206e78f5e90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AE=B6=E5=90=8D?= Date: Wed, 27 May 2026 20:17:03 +0800 Subject: [PATCH] fix safeReplace to replace all matches --- common/src/util/__tests__/string.test.ts | 15 ++++++++++++++- common/src/util/string.ts | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/common/src/util/__tests__/string.test.ts b/common/src/util/__tests__/string.test.ts index 3a141ca6b6..4551eaaaeb 100644 --- a/common/src/util/__tests__/string.test.ts +++ b/common/src/util/__tests__/string.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'bun:test' -import { pluralize } from '../string' +import { pluralize, safeReplace } from '../string' describe('pluralize', () => { it('should handle singular and plural cases correctly', () => { @@ -237,3 +237,16 @@ describe('pluralize', () => { }) }) +describe('safeReplace', () => { + it('should replace every occurrence of the search string', () => { + expect(safeReplace('alpha beta alpha', 'alpha', 'gamma')).toBe( + 'gamma beta gamma', + ) + }) + + it('should treat dollar signs in replacement text literally', () => { + expect(safeReplace('total: AMOUNT and AMOUNT', 'AMOUNT', '$& dollars')).toBe( + 'total: $& dollars and $& dollars', + ) + }) +}) diff --git a/common/src/util/string.ts b/common/src/util/string.ts index 506de962fd..26e7fd64ee 100644 --- a/common/src/util/string.ts +++ b/common/src/util/string.ts @@ -292,7 +292,7 @@ export const safeReplace = ( replaceStr: string, ): string => { const escapedReplaceStr = replaceStr.replace(/\$/g, '$$$$') - return content.replace(searchStr, escapedReplaceStr) + return content.replaceAll(searchStr, escapedReplaceStr) } /**