summaryrefslogtreecommitdiff
path: root/packages/webapi/src/lib/String.ts
blob: 49df3261c125a25bc64a24396f4a4009e55dc223 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import * as _ from './utils'

export const replaceAll = {
	replaceAll(this: string, searchValue: RegExp | string, replaceValue: string | ((substring: string, ...args: any[]) => string)) {
		return _.__object_isPrototypeOf(RegExp.prototype, searchValue)
			? this.replace(searchValue as RegExp, replaceValue as string)
		: this.replace(new RegExp(_.__string_escapeRegExp(searchValue as string), 'g'), replaceValue as string)
	}
}.replaceAll

export const initString = (target: any, exclude: Set<string>) => {
	if (exclude.has('String') || exclude.has('replaceAll')) return

	const Class = target.String || globalThis.String

	if (!Class.prototype.replaceAll) Object.defineProperty(Class.prototype, 'replaceAll', {
		value: replaceAll,
		writable: true,
		enumerable: false,
		configurable: true
	})
}