-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
String.ts
30 lines (26 loc) · 836 Bytes
/
String.ts
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,
})
}