Skip to content

Commit

Permalink
String.prototype.{replace, replaceAll}
Browse files Browse the repository at this point in the history
  • Loading branch information
graphemecluster committed May 12, 2022
1 parent 3a608b7 commit 41432ed
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 2 deletions.
2 changes: 2 additions & 0 deletions build/replacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const replacement = new Map([
"CallableFunction",
"NewableFunction",
"IArguments",
"String",
"JSON",
"ArrayConstructor",
"ReadonlyArray",
Expand Down Expand Up @@ -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"])],
Expand Down
119 changes: 119 additions & 0 deletions lib/lib.es2015.symbol.wellknown.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/// <reference no-default-lib="true"/>

interface Array<T> {
/**
* 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[];
}
31 changes: 29 additions & 2 deletions lib/lib.es2021.string.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
132 changes: 132 additions & 0 deletions lib/lib.es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 41432ed

Please sign in to comment.