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) } /**