Skip to content

Commit 41432ed

Browse files
String.prototype.{replace, replaceAll}
1 parent 3a608b7 commit 41432ed

File tree

4 files changed

+282
-2
lines changed

4 files changed

+282
-2
lines changed

build/replacement.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const replacement = new Map([
77
"CallableFunction",
88
"NewableFunction",
99
"IArguments",
10+
"String",
1011
"JSON",
1112
"ArrayConstructor",
1213
"ReadonlyArray",
@@ -46,6 +47,7 @@ export const replacement = new Map([
4647
["es2015.promise.d.ts", new Set(["PromiseConstructor"])],
4748
["es2015.proxy.d.ts", new Set(["ProxyHandler"])],
4849
["es2015.reflect.d.ts", new Set(["Reflect"])],
50+
["es2015.symbol.wellknown.d.ts", new Set(["Array", "RegExp", "String"])],
4951
["es2017.object.d.ts", new Set(["ObjectConstructor"])],
5052
["es2018.asyncgenerator.d.ts", new Set(["AsyncGenerator"])],
5153
["es2018.asynciterable.d.ts", new Set(["AsyncIterator"])],

lib/lib.es2015.symbol.wellknown.d.ts

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/// <reference no-default-lib="true"/>
2+
3+
interface Array<T> {
4+
/**
5+
* Returns an object whose properties have the value 'true'
6+
* when they will be absent when used in a 'with' statement.
7+
*/
8+
readonly [Symbol.unscopables]: { [key: PropertyKey]: boolean };
9+
}
10+
11+
interface RegExp {
12+
/**
13+
* Matches a string with this regular expression, and returns an array containing the results of
14+
* that search.
15+
* @param string A string to search within.
16+
*/
17+
[Symbol.match](string: string): RegExpMatchArray | null;
18+
19+
/**
20+
* Replaces text in a string, using this regular expression.
21+
* @param string A String object or string literal whose contents matching against
22+
* this regular expression will be replaced
23+
* @param replaceValue A String object or string literal containing the text to replace for every
24+
* successful match of this regular expression.
25+
*/
26+
[Symbol.replace](string: string, replaceValue: string): string;
27+
28+
/**
29+
* Replaces text in a string, using this regular expression.
30+
* @param string A String object or string literal whose contents matching against
31+
* this regular expression will be replaced
32+
* @param replacer A function that returns the replacement text.
33+
*/
34+
[Symbol.replace](
35+
string: string,
36+
replacer: (
37+
substring: string,
38+
// TODO: could be improved, but blocked by issue:
39+
// https://github.com/microsoft/TypeScript/issues/45972
40+
...rest: (string | number)[]
41+
) => string
42+
): string;
43+
44+
/**
45+
* Finds the position beginning first substring match in a regular expression search
46+
* using this regular expression.
47+
*
48+
* @param string The string to search within.
49+
*/
50+
[Symbol.search](string: string): number;
51+
52+
/**
53+
* Returns an array of substrings that were delimited by strings in the original input that
54+
* match against this regular expression.
55+
*
56+
* If the regular expression contains capturing parentheses, then each time this
57+
* regular expression matches, the results (including any undefined results) of the
58+
* capturing parentheses are spliced.
59+
*
60+
* @param string string value to split
61+
* @param limit if not undefined, the output array is truncated so that it contains no more
62+
* than 'limit' elements.
63+
*/
64+
[Symbol.split](string: string, limit?: number): string[];
65+
}
66+
67+
interface String {
68+
/**
69+
* Matches a string or an object that supports being matched against, and returns an array
70+
* containing the results of that search, or null if no matches are found.
71+
* @param matcher An object that supports being matched against.
72+
*/
73+
match(matcher: {
74+
[Symbol.match](string: string): RegExpMatchArray | null;
75+
}): RegExpMatchArray | null;
76+
77+
/**
78+
* Replaces first match with string or all matches with RegExp.
79+
* @param searchValue A object can search for and replace matches within a string.
80+
* @param replaceValue A string containing the text to replace for match.
81+
*/
82+
replace(
83+
searchValue: {
84+
[Symbol.replace](string: string, replaceValue: string): string;
85+
},
86+
replaceValue: string
87+
): string;
88+
89+
/**
90+
* Replaces text in a string, using an object that supports replacement within a string.
91+
* @param searchValue A object can search for and replace matches within a string.
92+
* @param replacer A function that returns the replacement text.
93+
*/
94+
replace(
95+
searchValue: {
96+
[Symbol.replace](
97+
string: string,
98+
replacer: (substring: string, ...rest: (string | number)[]) => string
99+
): string;
100+
},
101+
replacer: (substring: string, ...rest: (string | number)[]) => string
102+
): string;
103+
104+
/**
105+
* Finds the first substring match in a regular expression search.
106+
* @param searcher An object which supports searching within a string.
107+
*/
108+
search(searcher: { [Symbol.search](string: string): number }): number;
109+
110+
/**
111+
* Split a string into substrings using the specified separator and return them as an array.
112+
* @param splitter An object that can split a string.
113+
* @param limit A value used to limit the number of elements returned in the array.
114+
*/
115+
split(
116+
splitter: { [Symbol.split](string: string, limit?: number): string[] },
117+
limit?: number
118+
): string[];
119+
}

lib/lib.es2021.string.d.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
interface String {
44
/**
55
* Replace all instances of a substring in a string, using a regular expression or search string.
6-
* @param searchValue A string to search for.
6+
* @param searchValue A string or RegExp search value.
77
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
88
*/
99
replaceAll(searchValue: string | RegExp, replaceValue: string): string;
1010

1111
/**
1212
* Replace all instances of a substring in a string, using a regular expression or search string.
13-
* @param searchValue A string to search for.
13+
* @param searchValue A string or RegExp search value.
1414
* @param replacer A function that returns the replacement text.
1515
*/
1616
replaceAll(
@@ -22,4 +22,31 @@ interface String {
2222
...rest: (string | number)[]
2323
) => string
2424
): string;
25+
26+
/**
27+
* Replace all instances of a substring in a string, using a regular expression or search string.
28+
* @param searchValue A object can search for and replace matches within a string.
29+
* @param replaceValue A string containing the text to replace for match.
30+
*/
31+
replaceAll(
32+
searchValue: {
33+
[Symbol.replace](string: string, replaceValue: string): string;
34+
},
35+
replaceValue: string
36+
): string;
37+
38+
/**
39+
* Replace all instances of a substring in a string, using a regular expression or search string.
40+
* @param searchValue A object can search for and replace matches within a string.
41+
* @param replacer A function that returns the replacement text.
42+
*/
43+
replaceAll(
44+
searchValue: {
45+
[Symbol.replace](
46+
string: string,
47+
replacer: (substring: string, ...rest: (string | number)[]) => string
48+
): string;
49+
},
50+
replacer: (substring: string, ...rest: (string | number)[]) => string
51+
): string;
2552
}

lib/lib.es5.d.ts

+132
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,138 @@ interface IArguments {
321321
callee: Function;
322322
}
323323

324+
interface String {
325+
/** Returns a string representation of a string. */
326+
toString(): string;
327+
328+
/**
329+
* Returns the character at the specified index.
330+
* @param pos The zero-based index of the desired character.
331+
*/
332+
charAt(pos: number): string;
333+
334+
/**
335+
* Returns the Unicode value of the character at the specified location.
336+
* @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned.
337+
*/
338+
charCodeAt(index: number): number;
339+
340+
/**
341+
* Returns a string that contains the concatenation of two or more strings.
342+
* @param strings The strings to append to the end of the string.
343+
*/
344+
concat(...strings: string[]): string;
345+
346+
/**
347+
* Returns the position of the first occurrence of a substring.
348+
* @param searchString The substring to search for in the string
349+
* @param position The index at which to begin searching the String object. If omitted, search starts at the beginning of the string.
350+
*/
351+
indexOf(searchString: string, position?: number): number;
352+
353+
/**
354+
* Returns the last occurrence of a substring in the string.
355+
* @param searchString The substring to search for.
356+
* @param position The index at which to begin searching. If omitted, the search begins at the end of the string.
357+
*/
358+
lastIndexOf(searchString: string, position?: number): number;
359+
360+
/**
361+
* Determines whether two strings are equivalent in the current locale.
362+
* @param that String to compare to target string
363+
*/
364+
localeCompare(that: string): number;
365+
366+
/**
367+
* Matches a string with a regular expression, and returns an array containing the results of that search.
368+
* @param regexp A variable name or string literal containing the regular expression pattern and flags.
369+
*/
370+
match(regexp: string | RegExp): RegExpMatchArray | null;
371+
372+
/**
373+
* Replaces text in a string, using a regular expression or search string.
374+
* @param searchValue A string or RegExp search value.
375+
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
376+
*/
377+
replace(searchValue: string | RegExp, replaceValue: string): string;
378+
379+
/**
380+
* Replaces text in a string, using a regular expression or search string.
381+
* @param searchValue A string or RegExp search value.
382+
* @param replacer A function that returns the replacement text.
383+
*/
384+
replace(
385+
searchValue: string | RegExp,
386+
replacer: (
387+
substring: string,
388+
// TODO: could be improved, but blocked by issue:
389+
// https://github.com/microsoft/TypeScript/issues/45972
390+
...rest: (string | number)[]
391+
) => string
392+
): string;
393+
394+
/**
395+
* Finds the first substring match in a regular expression search.
396+
* @param regexp The regular expression pattern and applicable flags.
397+
*/
398+
search(regexp: string | RegExp): number;
399+
400+
/**
401+
* Returns a section of a string.
402+
* @param start The index to the beginning of the specified portion of stringObj.
403+
* @param end The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end.
404+
* If this value is not specified, the substring continues to the end of stringObj.
405+
*/
406+
slice(start?: number, end?: number): string;
407+
408+
/**
409+
* Split a string into substrings using the specified separator and return them as an array.
410+
* @param separator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.
411+
* @param limit A value used to limit the number of elements returned in the array.
412+
*/
413+
split(separator: string | RegExp, limit?: number): string[];
414+
415+
/**
416+
* Returns the substring at the specified location within a String object.
417+
* @param start The zero-based index number indicating the beginning of the substring.
418+
* @param end Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end.
419+
* If end is omitted, the characters from start through the end of the original string are returned.
420+
*/
421+
substring(start: number, end?: number): string;
422+
423+
/** Converts all the alphabetic characters in a string to lowercase. */
424+
toLowerCase(): string;
425+
426+
/** Converts all alphabetic characters to lowercase, taking into account the host environment's current locale. */
427+
toLocaleLowerCase(locales?: string | string[]): string;
428+
429+
/** Converts all the alphabetic characters in a string to uppercase. */
430+
toUpperCase(): string;
431+
432+
/** Returns a string where all alphabetic characters have been converted to uppercase, taking into account the host environment's current locale. */
433+
toLocaleUpperCase(locales?: string | string[]): string;
434+
435+
/** Removes the leading and trailing white space and line terminator characters from a string. */
436+
trim(): string;
437+
438+
/** Returns the length of a String object. */
439+
readonly length: number;
440+
441+
// IE extensions
442+
/**
443+
* Gets a substring beginning at the specified location and having the specified length.
444+
* @deprecated A legacy feature for browser compatibility
445+
* @param from The starting position of the desired substring. The index of the first character in the string is zero.
446+
* @param length The number of characters to include in the returned substring.
447+
*/
448+
substr(from: number, length?: number): string;
449+
450+
/** Returns the primitive value of the specified object. */
451+
valueOf(): string;
452+
453+
readonly [index: number]: string;
454+
}
455+
324456
type JSONValue =
325457
| null
326458
| string

0 commit comments

Comments
 (0)