Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added string.replace() and .replaceAll() #698

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions devs/run-tests/05strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,22 @@ function testSplit() {
isEq(sq3[2], "c,d")
}

function testReplace() {
const q = "a,b,c,d"
const sq = q.replace(",", ":")
isEq(sq, "a:b,c,d")
}
function testReplaceAll() {
const q = "a,b,c,d"
const sq = q.replaceAll(",", ":")
isEq(sq, "a:b:c:d")
}

testStrings()
testStringOps()
consStringTest()

testSlice()
testSplit()
testReplace()
testReplaceAll()
22 changes: 17 additions & 5 deletions packages/core/src/corelib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,19 @@ interface String {
* If this value is not specified, the substring continues to the end of stringObj.
*/
slice(start?: number, end?: number): string
/**
* Replace the first instance of a substring in a string, using a search string.
* @param searchValue The substring to search for.
* @param replaceValue The string containing the text to replace the matched substring.
*/
replace(searchValue: string, replaceValue: string): string

/**
* Replace all instances of a substring in a string, using a search string.
* @param searchValue The substring to search for.
* @param replaceValue The string containing the text to replace all the matches found.
*/
replaceAll(searchValue: string, replaceValue: string): string
/** Returns the length of a String object. */
readonly length: number

Expand Down Expand Up @@ -440,7 +452,7 @@ interface Array<T> {
/**
* Returns an iterable of keys in the array
*/
keys(): IterableIterator<number>;
keys(): IterableIterator<number>
}

interface ArrayConstructor {
Expand Down Expand Up @@ -725,10 +737,10 @@ declare var Promise: PromiseConstructor
type Awaited<T> = T extends null | undefined
? T // special case for `null | undefined` when not in `--strictNullChecks` mode
: T extends object & { then(onfulfilled: infer F, ...args: infer _): any } // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
? F extends (value: infer V, ...args: infer _) => any // if the argument to `then` is callable, extracts the first argument
? Awaited<V> // recursively unwrap the value
: never // the argument to `then` was not callable
: T // non-object or non-thenable
? F extends (value: infer V, ...args: infer _) => any // if the argument to `then` is callable, extracts the first argument
? Awaited<V> // recursively unwrap the value
: never // the argument to `then` was not callable
: T // non-object or non-thenable

// utility types

Expand Down
30 changes: 30 additions & 0 deletions packages/core/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,36 @@ String.prototype.split = function (
return A
}

String.prototype.replace = function (
this: string,
searchValue: string,
replaceValue: string
): string {
const match = this.indexOf(searchValue)
if (match === -1) return this
return (
this.slice(0, match) +
replaceValue +
this.slice(match + searchValue.length)
)
}

String.prototype.replaceAll = function (
this: string,
searchValue: string,
replaceValue: string
): string {
let resultString = this
let match = this.indexOf(searchValue)
while (match !== -1) {
resultString =
resultString.slice(0, match) +
replaceValue +
resultString.slice(match + searchValue.length)
match = resultString.indexOf(searchValue)
}
return resultString
}
function splitMatch(S: string, q: number, R: string): number {
return S.indexOf(R, q, q + 1) === q ? q + R.length : -1
}
Loading