Skip to content

Commit 439682b

Browse files
committed
fix safeReplace to replace all matches
1 parent eaa8c10 commit 439682b

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

common/src/util/__tests__/string.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'bun:test'
22

3-
import { pluralize } from '../string'
3+
import { pluralize, safeReplace } from '../string'
44

55
describe('pluralize', () => {
66
it('should handle singular and plural cases correctly', () => {
@@ -237,3 +237,16 @@ describe('pluralize', () => {
237237
})
238238
})
239239

240+
describe('safeReplace', () => {
241+
it('should replace every occurrence of the search string', () => {
242+
expect(safeReplace('alpha beta alpha', 'alpha', 'gamma')).toBe(
243+
'gamma beta gamma',
244+
)
245+
})
246+
247+
it('should treat dollar signs in replacement text literally', () => {
248+
expect(safeReplace('total: AMOUNT and AMOUNT', 'AMOUNT', '$& dollars')).toBe(
249+
'total: $& dollars and $& dollars',
250+
)
251+
})
252+
})

common/src/util/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export const safeReplace = (
292292
replaceStr: string,
293293
): string => {
294294
const escapedReplaceStr = replaceStr.replace(/\$/g, '$$$$')
295-
return content.replace(searchStr, escapedReplaceStr)
295+
return content.replaceAll(searchStr, escapedReplaceStr)
296296
}
297297

298298
/**

0 commit comments

Comments
 (0)