Skip to content

Commit bebd6da

Browse files
committed
Move NewlineKind to Text.ts, and add Text.ensureTrailingNewline()
1 parent 7117819 commit bebd6da

3 files changed

Lines changed: 33 additions & 22 deletions

File tree

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as pathUtilities from 'path';
55
import * as fs from 'fs';
66
import * 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
*

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
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
*
@@ -12,6 +31,7 @@
1231
*/
1332
export 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
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ export {
5252
} from './PackageJsonLookup';
5353
export { PackageName, IParsedPackageName, IParsedPackageNameOrError } from './PackageName';
5454
export { Path } from './Path';
55-
export { Text } from './Text';
55+
export { Text, NewlineKind } from './Text';
5656
export {
5757
FileSystem,
5858
IReadFolderOptions,
5959
IWriteFileOptions,
6060
IReadFileOptions,
6161
IFileSystemMoveOptions,
6262
IDeleteFileOptions,
63-
NewlineKind,
6463
PosixModeBits,
6564
IUpdateTimeParameters
6665
} from './FileSystem';

0 commit comments

Comments
 (0)