Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion common/src/util/__tests__/string.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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',
)
})
})
2 changes: 1 addition & 1 deletion common/src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
Loading