Skip to content

Added replaceAll for string prototype & unit tests #17582

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

Merged
merged 4 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions news/2 Fixes/15288.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
replaceAll for replacing separators. (thanks [Aliva Das](https://github.com/IceJinx33))
26 changes: 26 additions & 0 deletions src/client/common/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ declare interface String {
* Removes leading and trailing quotes from a string
*/
trimQuotes(): string;

/**
* String.replaceAll implementation
* Replaces all instances of a substring with a new string
*/
replaceAll(substr: string, newSubstr: string): string;
}

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

/**
* String.replaceAll implementation
* Replaces all instances of a substring with a new substring.
*/
String.prototype.replaceAll = function (this: string, substr: string, newSubstr: string): string {
if (!this) {
return this;
}

/** Escaping function from the MDN web docs site
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
* Escapes all the following special characters in a string . * + ? ^ $ { } ( ) | \ \\ */

function escapeRegExp(unescapedStr: string): string {
return unescapedStr.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

return this.replace(new RegExp(escapeRegExp(substr), 'g'), newSubstr);
};

declare interface Promise<T> {
/**
* Catches task error and ignores them.
Expand Down
2 changes: 1 addition & 1 deletion src/client/pythonEnvironments/base/info/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function getInterpreterInfo(python: PythonExecInfo): Promise<Interp
const argv = [info.command, ...info.args];

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

// Try shell execing the command, followed by the arguments. This will make node kill the process if it
// takes too long.
Expand Down
2 changes: 1 addition & 1 deletion src/client/pythonEnvironments/info/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function getInterpreterInfo(
const argv = [info.command, ...info.args];

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

// Try shell execing the command, followed by the arguments. This will make node kill the process if it
// takes too long.
Expand Down
15 changes: 15 additions & 0 deletions src/test/common/extensions.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ suite('String Extensions', () => {
expect(quotedString3.trimQuotes()).to.be.equal(expectedString);
expect(quotedString4.trimQuotes()).to.be.equal(expectedString);
});
test('String should replace all substrings with new substring', () => {
//tslint:disable:no-multiline-string
const oldString = `foo \\ foo \\ foo`;
const expectedString = `foo \\\\ foo \\\\ foo`;
const oldString2 = `\\ foo \\ foo`;
const expectedString2 = `\\\\ foo \\\\ foo`;
const oldString3 = `\\ foo \\`;
const expectedString3 = `\\\\ foo \\\\`;
const oldString4 = `foo foo`;
const expectedString4 = `foo foo`;
expect(oldString.replaceAll('\\', '\\\\')).to.be.equal(expectedString);
expect(oldString2.replaceAll('\\', '\\\\')).to.be.equal(expectedString2);
expect(oldString3.replaceAll('\\', '\\\\')).to.be.equal(expectedString3);
expect(oldString4.replaceAll('\\', '\\\\')).to.be.equal(expectedString4);
});
});

suite('Array extensions', () => {
Expand Down