Skip to content

Commit

Permalink
perf: use the efficient String#replaceAll when supported #95
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-layne committed Nov 17, 2024
1 parent 357d5b2 commit 6ad20af
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export function getCaptured(input: string, re: RegExp): string[] {
* A simplified version of `String.replaceAll` to address compatibility issues on Node 14
*/
export function replaceAll(source: string, searchValue: string, replaceValue: string): string {
if (typeof source.replaceAll === 'function')
return source.replaceAll(searchValue, replaceValue)
const escaped = escapeStringRegexp(searchValue)
const replaceRegExp = new RegExp(escaped, 'g')
return source.replace(replaceRegExp, replaceValue)
Expand Down

0 comments on commit 6ad20af

Please sign in to comment.