Skip to content

Commit 4fb8c24

Browse files
IceJinx33Aliva Das
authored andcommitted
Added replaceAll for string prototype & unit tests (microsoft#17582)
* Added replaceAll for string prototype & unit tests * Create 15288.md * Bug Fix: use replaceAll for replacing separators. * Ran prettier on updated files. Co-authored-by: Aliva Das <[email protected]>
1 parent b5a3166 commit 4fb8c24

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

news/2 Fixes/15288.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
replaceAll for replacing separators. (thanks [Aliva Das](https://github.com/IceJinx33))

src/client/common/extensions.ts

+26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ declare interface String {
3737
* Removes leading and trailing quotes from a string
3838
*/
3939
trimQuotes(): string;
40+
41+
/**
42+
* String.replaceAll implementation
43+
* Replaces all instances of a substring with a new string
44+
*/
45+
replaceAll(substr: string, newSubstr: string): string;
4046
}
4147

4248
/**
@@ -92,6 +98,26 @@ String.prototype.trimQuotes = function (this: string): string {
9298
return this.replace(/(^['"])|(['"]$)/g, '');
9399
};
94100

101+
/**
102+
* String.replaceAll implementation
103+
* Replaces all instances of a substring with a new substring.
104+
*/
105+
String.prototype.replaceAll = function (this: string, substr: string, newSubstr: string): string {
106+
if (!this) {
107+
return this;
108+
}
109+
110+
/** Escaping function from the MDN web docs site
111+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
112+
* Escapes all the following special characters in a string . * + ? ^ $ { } ( ) | \ \\ */
113+
114+
function escapeRegExp(unescapedStr: string): string {
115+
return unescapedStr.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
116+
}
117+
118+
return this.replace(new RegExp(escapeRegExp(substr), 'g'), newSubstr);
119+
};
120+
95121
declare interface Promise<T> {
96122
/**
97123
* Catches task error and ignores them.

src/client/pythonEnvironments/base/info/interpreter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function getInterpreterInfo(python: PythonExecInfo): Promise<Interp
7373
const argv = [info.command, ...info.args];
7474

7575
// Concat these together to make a set of quoted strings
76-
const quoted = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replace('\\', '\\\\')}"`), '');
76+
const quoted = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replaceAll('\\', '\\\\')}"`), '');
7777

7878
// Try shell execing the command, followed by the arguments. This will make node kill the process if it
7979
// takes too long.

src/client/pythonEnvironments/info/interpreter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function getInterpreterInfo(
7373
const argv = [info.command, ...info.args];
7474

7575
// Concat these together to make a set of quoted strings
76-
const quoted = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replace('\\', '\\\\')}"`), '');
76+
const quoted = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replaceAll('\\', '\\\\')}"`), '');
7777

7878
// Try shell execing the command, followed by the arguments. This will make node kill the process if it
7979
// takes too long.

src/test/common/extensions.unit.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ suite('String Extensions', () => {
8484
expect(quotedString3.trimQuotes()).to.be.equal(expectedString);
8585
expect(quotedString4.trimQuotes()).to.be.equal(expectedString);
8686
});
87+
test('String should replace all substrings with new substring', () => {
88+
//tslint:disable:no-multiline-string
89+
const oldString = `foo \\ foo \\ foo`;
90+
const expectedString = `foo \\\\ foo \\\\ foo`;
91+
const oldString2 = `\\ foo \\ foo`;
92+
const expectedString2 = `\\\\ foo \\\\ foo`;
93+
const oldString3 = `\\ foo \\`;
94+
const expectedString3 = `\\\\ foo \\\\`;
95+
const oldString4 = `foo foo`;
96+
const expectedString4 = `foo foo`;
97+
expect(oldString.replaceAll('\\', '\\\\')).to.be.equal(expectedString);
98+
expect(oldString2.replaceAll('\\', '\\\\')).to.be.equal(expectedString2);
99+
expect(oldString3.replaceAll('\\', '\\\\')).to.be.equal(expectedString3);
100+
expect(oldString4.replaceAll('\\', '\\\\')).to.be.equal(expectedString4);
101+
});
87102
});
88103

89104
suite('Array extensions', () => {

0 commit comments

Comments
 (0)