1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
- /**
5
- * @typedef {Object } SplitLinesOptions
6
- * @property {boolean } [trim=true] - Whether to trim the lines.
7
- * @property {boolean } [removeEmptyEntries=true] - Whether to remove empty entries.
8
- */
9
-
10
- // https://stackoverflow.com/questions/39877156/how-to-extend-string-prototype-and-use-it-next-in-typescript
11
-
12
4
declare interface String {
13
- /**
14
- * Split a string using the cr and lf characters and return them as an array.
15
- * By default lines are trimmed and empty lines are removed.
16
- * @param {SplitLinesOptions= } splitOptions - Options used for splitting the string.
17
- */
18
- splitLines ( splitOptions ?: { trim : boolean ; removeEmptyEntries ?: boolean } ) : string [ ] ;
19
5
/**
20
6
* Appropriately formats a string so it can be used as an argument for a command in a shell.
21
7
* E.g. if an argument contains a space, then it will be enclosed within double quotes.
@@ -37,33 +23,8 @@ declare interface String {
37
23
* Removes leading and trailing quotes from a string
38
24
*/
39
25
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 ;
46
26
}
47
27
48
- /**
49
- * Split a string using the cr and lf characters and return them as an array.
50
- * By default lines are trimmed and empty lines are removed.
51
- * @param {SplitLinesOptions= } splitOptions - Options used for splitting the string.
52
- */
53
- String . prototype . splitLines = function (
54
- this : string ,
55
- splitOptions : { trim : boolean ; removeEmptyEntries : boolean } = { removeEmptyEntries : true , trim : true } ,
56
- ) : string [ ] {
57
- let lines = this . split ( / \r ? \n / g) ;
58
- if ( splitOptions && splitOptions . trim ) {
59
- lines = lines . map ( ( line ) => line . trim ( ) ) ;
60
- }
61
- if ( splitOptions && splitOptions . removeEmptyEntries ) {
62
- lines = lines . filter ( ( line ) => line . length > 0 ) ;
63
- }
64
- return lines ;
65
- } ;
66
-
67
28
/**
68
29
* Appropriately formats a string so it can be used as an argument for a command in a shell.
69
30
* E.g. if an argument contains a space, then it will be enclosed within double quotes.
@@ -102,26 +63,6 @@ String.prototype.trimQuotes = function (this: string): string {
102
63
return this . replace ( / ( ^ [ ' " ] ) | ( [ ' " ] $ ) / g, '' ) ;
103
64
} ;
104
65
105
- /**
106
- * String.replaceAll implementation
107
- * Replaces all instances of a substring with a new substring.
108
- */
109
- String . prototype . replaceAll = function ( this : string , substr : string , newSubstr : string ) : string {
110
- if ( ! this ) {
111
- return this ;
112
- }
113
-
114
- /** Escaping function from the MDN web docs site
115
- * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
116
- * Escapes all the following special characters in a string . * + ? ^ $ { } ( ) | \ \\ */
117
-
118
- function escapeRegExp ( unescapedStr : string ) : string {
119
- return unescapedStr . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ; // $& means the whole matched string
120
- }
121
-
122
- return this . replace ( new RegExp ( escapeRegExp ( substr ) , 'g' ) , newSubstr ) ;
123
- } ;
124
-
125
66
declare interface Promise < T > {
126
67
/**
127
68
* Catches task error and ignores them.
0 commit comments