aboutsummaryrefslogtreecommitdiff
path: root/packages/webapi/src/lib/String.ts
blob: 92d39f39a16076b27240dac52998e030c860b9d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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,
		})
}