forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathstringUtils.ts
More file actions
46 lines (40 loc) · 1.49 KB
/
stringUtils.ts
File metadata and controls
46 lines (40 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export interface SplitLinesOptions {
trim?: boolean;
removeEmptyEntries?: boolean;
}
/**
* Split a string using the cr and lf characters and return them as an array.
* By default lines are trimmed and empty lines are removed.
* @param {SplitLinesOptions=} splitOptions - Options used for splitting the string.
*/
export function splitLines(
source: string,
splitOptions: SplitLinesOptions = { removeEmptyEntries: true, trim: true },
): string[] {
let lines = source.split(/\r?\n/g);
if (splitOptions?.trim) {
lines = lines.map((line) => line.trim());
}
if (splitOptions?.removeEmptyEntries) {
lines = lines.filter((line) => line.length > 0);
}
return lines;
}
/**
* Replaces all instances of a substring with a new substring.
*/
export function replaceAll(source: string, substr: string, newSubstr: string): string {
if (!source) {
return source;
}
/** 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 source.replace(new RegExp(escapeRegExp(substr), 'g'), newSubstr);
}