File tree Expand file tree Collapse file tree
libraries/node-core-library/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import * as pathUtilities from 'path';
55import * as fs from 'fs' ;
66import * as fsx from 'fs-extra' ;
77
8- import { Text } from './Text' ;
8+ import { Text , NewlineKind } from './Text' ;
99
1010// The PosixModeBits are intended to be used with bitwise operations.
1111// tslint:disable:no-bitwise
@@ -18,25 +18,6 @@ export const enum Encoding {
1818 Utf8 = 'utf8'
1919}
2020
21- /**
22- * Enumeration controlling conversion of newline characters.
23- * @public
24- */
25- export const enum NewlineKind {
26- /**
27- * Windows-style newlines
28- */
29- CrLf = '\r\n' ,
30-
31- /**
32- * POSIX-style newlines
33- *
34- * @remarks
35- * POSIX is a registered trademark of the Institute of Electrical and Electronic Engineers, Inc.
36- */
37- Lf = '\n'
38- }
39-
4021/**
4122 * An integer value used to specify file permissions for POSIX-like operating systems.
4223 *
Original file line number Diff line number Diff line change 11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See LICENSE in the project root for license information.
33
4+ /**
5+ * Enumeration controlling conversion of newline characters.
6+ * @public
7+ */
8+ export const enum NewlineKind {
9+ /**
10+ * Windows-style newlines
11+ */
12+ CrLf = '\r\n' ,
13+
14+ /**
15+ * POSIX-style newlines
16+ *
17+ * @remarks
18+ * POSIX is a registered trademark of the Institute of Electrical and Electronic Engineers, Inc.
19+ */
20+ Lf = '\n'
21+ }
22+
423/**
524 * Operations for working with strings that contain text.
625 *
1231 */
1332export class Text {
1433 private static readonly _newLineRegEx : RegExp = / \r \n | \n \r | \r | \n / g;
34+ private static readonly _newLineAtEndRegEx : RegExp = / ( \r \n | \n \r | \r | \n ) $ / ;
1535
1636 /**
1737 * Returns the same thing as targetString.replace(searchValue, replaceValue), except that
@@ -76,4 +96,15 @@ export class Text {
7696
7797 return s . substring ( 0 , maximumLength - 3 ) + '...' ;
7898 }
99+
100+ /**
101+ * Returns the input string with a trailing '\n' character appended, if not already present.
102+ */
103+ public static ensureTrailingNewline ( s : string , newlineKind : NewlineKind = NewlineKind . Lf ) : string {
104+ // Is there already a newline?
105+ if ( Text . _newLineAtEndRegEx . test ( s ) ) {
106+ return s ; // yes, no change
107+ }
108+ return s + newlineKind ; // no, add it
109+ }
79110}
Original file line number Diff line number Diff line change @@ -52,15 +52,14 @@ export {
5252} from './PackageJsonLookup' ;
5353export { PackageName , IParsedPackageName , IParsedPackageNameOrError } from './PackageName' ;
5454export { Path } from './Path' ;
55- export { Text } from './Text' ;
55+ export { Text , NewlineKind } from './Text' ;
5656export {
5757 FileSystem ,
5858 IReadFolderOptions ,
5959 IWriteFileOptions ,
6060 IReadFileOptions ,
6161 IFileSystemMoveOptions ,
6262 IDeleteFileOptions ,
63- NewlineKind ,
6463 PosixModeBits ,
6564 IUpdateTimeParameters
6665} from './FileSystem' ;
You can’t perform that action at this time.
0 commit comments