From 11611f117e9c5b7bef54726057bb1c6cf1c5c362 Mon Sep 17 00:00:00 2001 From: graphemecluster Date: Fri, 13 May 2022 06:33:15 +0800 Subject: [PATCH] `String.prototype.{replace, replaceAll}` --- build/replacement.ts | 2 + lib/lib.es2015.symbol.wellknown.d.ts | 119 ++++++++++++++++++++++++ lib/lib.es2021.string.d.ts | 31 ++++++- lib/lib.es5.d.ts | 132 +++++++++++++++++++++++++++ 4 files changed, 282 insertions(+), 2 deletions(-) create mode 100644 lib/lib.es2015.symbol.wellknown.d.ts diff --git a/build/replacement.ts b/build/replacement.ts index fb723d1..3c6dbf1 100644 --- a/build/replacement.ts +++ b/build/replacement.ts @@ -7,6 +7,7 @@ export const replacement = new Map([ "CallableFunction", "NewableFunction", "IArguments", + "String", "JSON", "ArrayConstructor", "ReadonlyArray", @@ -46,6 +47,7 @@ export const replacement = new Map([ ["es2015.promise.d.ts", new Set(["PromiseConstructor"])], ["es2015.proxy.d.ts", new Set(["ProxyHandler"])], ["es2015.reflect.d.ts", new Set(["Reflect"])], + ["es2015.symbol.wellknown.d.ts", new Set(["Array", "RegExp", "String"])], ["es2017.object.d.ts", new Set(["ObjectConstructor"])], ["es2018.asyncgenerator.d.ts", new Set(["AsyncGenerator"])], ["es2018.asynciterable.d.ts", new Set(["AsyncIterator"])], diff --git a/lib/lib.es2015.symbol.wellknown.d.ts b/lib/lib.es2015.symbol.wellknown.d.ts new file mode 100644 index 0000000..e24d288 --- /dev/null +++ b/lib/lib.es2015.symbol.wellknown.d.ts @@ -0,0 +1,119 @@ +/// + +interface Array { + /** + * Returns an object whose properties have the value 'true' + * when they will be absent when used in a 'with' statement. + */ + readonly [Symbol.unscopables]: { [key: PropertyKey]: boolean }; +} + +interface RegExp { + /** + * Matches a string with this regular expression, and returns an array containing the results of + * that search. + * @param string A string to search within. + */ + [Symbol.match](string: string): RegExpMatchArray | null; + + /** + * Replaces text in a string, using this regular expression. + * @param string A String object or string literal whose contents matching against + * this regular expression will be replaced + * @param replaceValue A String object or string literal containing the text to replace for every + * successful match of this regular expression. + */ + [Symbol.replace](string: string, replaceValue: string): string; + + /** + * Replaces text in a string, using this regular expression. + * @param string A String object or string literal whose contents matching against + * this regular expression will be replaced + * @param replacer A function that returns the replacement text. + */ + [Symbol.replace]( + string: string, + replacer: ( + substring: string, + // TODO: could be improved, but blocked by issue: + // https://github.com/microsoft/TypeScript/issues/45972 + ...rest: (string | number)[] + ) => string + ): string; + + /** + * Finds the position beginning first substring match in a regular expression search + * using this regular expression. + * + * @param string The string to search within. + */ + [Symbol.search](string: string): number; + + /** + * Returns an array of substrings that were delimited by strings in the original input that + * match against this regular expression. + * + * If the regular expression contains capturing parentheses, then each time this + * regular expression matches, the results (including any undefined results) of the + * capturing parentheses are spliced. + * + * @param string string value to split + * @param limit if not undefined, the output array is truncated so that it contains no more + * than 'limit' elements. + */ + [Symbol.split](string: string, limit?: number): string[]; +} + +interface String { + /** + * Matches a string or an object that supports being matched against, and returns an array + * containing the results of that search, or null if no matches are found. + * @param matcher An object that supports being matched against. + */ + match(matcher: { + [Symbol.match](string: string): RegExpMatchArray | null; + }): RegExpMatchArray | null; + + /** + * Replaces first match with string or all matches with RegExp. + * @param searchValue A object can search for and replace matches within a string. + * @param replaceValue A string containing the text to replace for match. + */ + replace( + searchValue: { + [Symbol.replace](string: string, replaceValue: string): string; + }, + replaceValue: string + ): string; + + /** + * Replaces text in a string, using an object that supports replacement within a string. + * @param searchValue A object can search for and replace matches within a string. + * @param replacer A function that returns the replacement text. + */ + replace( + searchValue: { + [Symbol.replace]( + string: string, + replacer: (substring: string, ...rest: (string | number)[]) => string + ): string; + }, + replacer: (substring: string, ...rest: (string | number)[]) => string + ): string; + + /** + * Finds the first substring match in a regular expression search. + * @param searcher An object which supports searching within a string. + */ + search(searcher: { [Symbol.search](string: string): number }): number; + + /** + * Split a string into substrings using the specified separator and return them as an array. + * @param splitter An object that can split a string. + * @param limit A value used to limit the number of elements returned in the array. + */ + split( + splitter: { [Symbol.split](string: string, limit?: number): string[] }, + limit?: number + ): string[]; +} diff --git a/lib/lib.es2021.string.d.ts b/lib/lib.es2021.string.d.ts index 781d928..0dc3989 100644 --- a/lib/lib.es2021.string.d.ts +++ b/lib/lib.es2021.string.d.ts @@ -3,14 +3,14 @@ interface String { /** * Replace all instances of a substring in a string, using a regular expression or search string. - * @param searchValue A string to search for. + * @param searchValue A string or RegExp search value. * @param replaceValue A string containing the text to replace for every successful match of searchValue in this string. */ replaceAll(searchValue: string | RegExp, replaceValue: string): string; /** * Replace all instances of a substring in a string, using a regular expression or search string. - * @param searchValue A string to search for. + * @param searchValue A string or RegExp search value. * @param replacer A function that returns the replacement text. */ replaceAll( @@ -22,4 +22,31 @@ interface String { ...rest: (string | number)[] ) => string ): string; + + /** + * Replace all instances of a substring in a string, using a regular expression or search string. + * @param searchValue A object can search for and replace matches within a string. + * @param replaceValue A string containing the text to replace for match. + */ + replaceAll( + searchValue: { + [Symbol.replace](string: string, replaceValue: string): string; + }, + replaceValue: string + ): string; + + /** + * Replace all instances of a substring in a string, using a regular expression or search string. + * @param searchValue A object can search for and replace matches within a string. + * @param replacer A function that returns the replacement text. + */ + replaceAll( + searchValue: { + [Symbol.replace]( + string: string, + replacer: (substring: string, ...rest: (string | number)[]) => string + ): string; + }, + replacer: (substring: string, ...rest: (string | number)[]) => string + ): string; } diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index de11c38..d4c1b58 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -321,6 +321,138 @@ interface IArguments { callee: Function; } +interface String { + /** Returns a string representation of a string. */ + toString(): string; + + /** + * Returns the character at the specified index. + * @param pos The zero-based index of the desired character. + */ + charAt(pos: number): string; + + /** + * Returns the Unicode value of the character at the specified location. + * @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned. + */ + charCodeAt(index: number): number; + + /** + * Returns a string that contains the concatenation of two or more strings. + * @param strings The strings to append to the end of the string. + */ + concat(...strings: string[]): string; + + /** + * Returns the position of the first occurrence of a substring. + * @param searchString The substring to search for in the string + * @param position The index at which to begin searching the String object. If omitted, search starts at the beginning of the string. + */ + indexOf(searchString: string, position?: number): number; + + /** + * Returns the last occurrence of a substring in the string. + * @param searchString The substring to search for. + * @param position The index at which to begin searching. If omitted, the search begins at the end of the string. + */ + lastIndexOf(searchString: string, position?: number): number; + + /** + * Determines whether two strings are equivalent in the current locale. + * @param that String to compare to target string + */ + localeCompare(that: string): number; + + /** + * Matches a string with a regular expression, and returns an array containing the results of that search. + * @param regexp A variable name or string literal containing the regular expression pattern and flags. + */ + match(regexp: string | RegExp): RegExpMatchArray | null; + + /** + * Replaces text in a string, using a regular expression or search string. + * @param searchValue A string or RegExp search value. + * @param replaceValue A string containing the text to replace for every successful match of searchValue in this string. + */ + replace(searchValue: string | RegExp, replaceValue: string): string; + + /** + * Replaces text in a string, using a regular expression or search string. + * @param searchValue A string or RegExp search value. + * @param replacer A function that returns the replacement text. + */ + replace( + searchValue: string | RegExp, + replacer: ( + substring: string, + // TODO: could be improved, but blocked by issue: + // https://github.com/microsoft/TypeScript/issues/45972 + ...rest: (string | number)[] + ) => string + ): string; + + /** + * Finds the first substring match in a regular expression search. + * @param regexp The regular expression pattern and applicable flags. + */ + search(regexp: string | RegExp): number; + + /** + * Returns a section of a string. + * @param start The index to the beginning of the specified portion of stringObj. + * @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. + * If this value is not specified, the substring continues to the end of stringObj. + */ + slice(start?: number, end?: number): string; + + /** + * Split a string into substrings using the specified separator and return them as an array. + * @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. + * @param limit A value used to limit the number of elements returned in the array. + */ + split(separator: string | RegExp, limit?: number): string[]; + + /** + * Returns the substring at the specified location within a String object. + * @param start The zero-based index number indicating the beginning of the substring. + * @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. + * If end is omitted, the characters from start through the end of the original string are returned. + */ + substring(start: number, end?: number): string; + + /** Converts all the alphabetic characters in a string to lowercase. */ + toLowerCase(): string; + + /** Converts all alphabetic characters to lowercase, taking into account the host environment's current locale. */ + toLocaleLowerCase(locales?: string | string[]): string; + + /** Converts all the alphabetic characters in a string to uppercase. */ + toUpperCase(): string; + + /** Returns a string where all alphabetic characters have been converted to uppercase, taking into account the host environment's current locale. */ + toLocaleUpperCase(locales?: string | string[]): string; + + /** Removes the leading and trailing white space and line terminator characters from a string. */ + trim(): string; + + /** Returns the length of a String object. */ + readonly length: number; + + // IE extensions + /** + * Gets a substring beginning at the specified location and having the specified length. + * @deprecated A legacy feature for browser compatibility + * @param from The starting position of the desired substring. The index of the first character in the string is zero. + * @param length The number of characters to include in the returned substring. + */ + substr(from: number, length?: number): string; + + /** Returns the primitive value of the specified object. */ + valueOf(): string; + + readonly [index: number]: string; +} + type JSONValue = | null | string