Skip to content

Commit 511928d

Browse files
committed
Add new Text API
1 parent d09563d commit 511928d

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎common/reviews/api/node-core-library.api.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,10 @@ class Path {
7474
static isUnder(childPath: string, parentFolderPath: string): boolean;
7575
}
7676

77+
// @public
78+
class Text {
79+
static convertToCrLf(input: string): string;
80+
static convertToLf(input: string): string;
81+
static replaceAll(input: string, searchValue: string, replaceValue: string): string;
82+
}
83+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
/**
5+
* Operations for working with strings that contain text.
6+
*
7+
* @remarks
8+
* The utilities provided by this class are intended to be simple, small, and very
9+
* broadly applicable.
10+
*
11+
* @public
12+
*/
13+
export class Text {
14+
private static readonly _newLineRegEx: RegExp = /\r\n|\n\r|\r|\n/g;
15+
16+
/**
17+
* Returns the same thing as targetString.replace(searchValue, replaceValue), except that
18+
* all matches are replaced, rather than just the first match.
19+
* @param input - The string to be modified
20+
* @param searchValue - The value to search for
21+
* @param replaceValue - The replacement text
22+
*/
23+
public static replaceAll(input: string, searchValue: string, replaceValue: string): string {
24+
return input.split(searchValue).join(replaceValue);
25+
}
26+
27+
/**
28+
* Converts all newlines in the provided string to use Windows-style CRLF end of line characters.
29+
*/
30+
public static convertToCrLf(input: string): string {
31+
return input.replace(Text._newLineRegEx, '\r\n');
32+
}
33+
34+
/**
35+
* Converts all newlines in the provided string to use Unix-style LF end of line characters.
36+
*/
37+
public static convertToLf(input: string): string {
38+
return input.replace(Text._newLineRegEx, '\n');
39+
}
40+
41+
}

‎libraries/node-core-library/src/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export { PackageJsonLookup } from './PackageJsonLookup';
2323
export {
2424
LockFile
2525
} from './LockFile';
26-
export { Path } from './Path';
26+
export { Path } from './Path';
27+
export { Text } from './Text';

0 commit comments

Comments
 (0)