Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const baseSupport =
//'allSettled' in Promise && // Polyfilled
'matchAll' in String.prototype &&
// ES2021
'replaceAll' in String.prototype &&
//'replaceAll' in String.prototype && // Polyfilled
// 'any' in Promise && // Polyfilled
// ES2022
// 'at' in String.prototype && // Polyfilled
Expand Down
38 changes: 38 additions & 0 deletions src/string-replaceall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export function stringReplaceAll(
this: string,
searchValue: RegExp | string,
replaceValue: ((substring: string, ...args: unknown[]) => string) | string
): string {
if (searchValue instanceof RegExp) return this.replace(searchValue, replaceValue as unknown as string)
Comment thread
keithamus marked this conversation as resolved.
let pos = -1
let endOfLastMatch = 0
let result = ''
if (typeof replaceValue === 'function') {
const unwrapped = replaceValue
replaceValue = () => unwrapped(searchValue, pos, this)
}
pos = this.indexOf(searchValue, pos + 1)
while (pos !== -1) {
result += this.substring(endOfLastMatch, pos)
result += searchValue.replace(searchValue, replaceValue as string)
endOfLastMatch = pos + searchValue.length
pos = this.indexOf(searchValue, pos + 1)
}
return result + this.substring(endOfLastMatch)
}

/*#__PURE__*/
export function isSupported(): boolean {
return 'replaceAll' in String.prototype && typeof String.prototype.replaceAll === 'function'
}

/*#__PURE__*/
export function isPolyfilled(): boolean {
return String.prototype.replaceAll === stringReplaceAll
}

export function apply(): void {
if (!isSupported()) {
String.prototype.replaceAll = stringReplaceAll
}
}
19 changes: 19 additions & 0 deletions test/string-replaceall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {apply, isPolyfilled, isSupported, stringReplaceAll} from '../lib/string-replaceall.js'

describe('String#replaceAll', () => {
it('has standard isSupported, isPolyfilled, apply API', () => {
expect(isSupported).to.be.a('function')
expect(isPolyfilled).to.be.a('function')
expect(apply).to.be.a('function')
expect(isSupported()).to.be.a('boolean')
expect(isPolyfilled()).to.equal(false)
})

it('replaces all ocurrences of a string', async () => {
expect(stringReplaceAll.call('aaa', 'a', 'bbb')).to.equal('bbbbbbbbb')
expect(stringReplaceAll.call('aaa', 'a', 'aba')).to.equal('abaabaaba')
expect(stringReplaceAll.call('_a_a_a_', 'a', 'b')).to.equal('_b_b_b_')
expect(stringReplaceAll.call('aaa', 'a', (match, i, whole) => match + i + whole)).to.equal('a0aaaa1aaaa2aaa')
expect(stringReplaceAll.call('aaa', /a/g, 'bbb')).to.equal('bbbbbbbbb')
})
})