-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathNormalizeNewlinesTextRewriter.ts
110 lines (93 loc) · 3.19 KB
/
NormalizeNewlinesTextRewriter.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { Text, type NewlineKind } from '@rushstack/node-core-library';
import { TextRewriter, type TextRewriterState } from './TextRewriter';
interface INormalizeNewlinesTextRewriterState extends TextRewriterState {
characterToIgnore: string;
incompleteLine: boolean;
}
/**
* Constructor options for {@link NormalizeNewlinesTextRewriter}
*
* @public
*/
export interface INormalizeNewlinesTextRewriterOptions {
/**
* Specifies how newlines should be represented in the output stream.
*/
newlineKind: NewlineKind;
/**
* If `true`, then `NormalizeNewlinesTextRewriter.close()` will append a newline to
* the output if it ends with an incomplete line.
*
* @remarks
* If the output is an empty string, then a newline will NOT be appended,
* because writing an empty string does not produce an incomplete line.
*/
ensureNewlineAtEnd?: boolean;
}
/**
* For use with {@link TextRewriterTransform}, this rewriter converts all newlines to
* a standard format.
*
* @public
*/
export class NormalizeNewlinesTextRewriter extends TextRewriter {
/** {@inheritDoc INormalizeNewlinesTextRewriterOptions.newlineKind} */
public readonly newlineKind: NewlineKind;
/**
* The specific character sequence that will be used when appending newlines.
*/
public readonly newline: string;
/** {@inheritDoc INormalizeNewlinesTextRewriterOptions.ensureNewlineAtEnd} */
public readonly ensureNewlineAtEnd: boolean;
public constructor(options: INormalizeNewlinesTextRewriterOptions) {
super();
this.newlineKind = options.newlineKind;
this.newline = Text.getNewline(options.newlineKind);
this.ensureNewlineAtEnd = !!options.ensureNewlineAtEnd;
}
public initialize(): TextRewriterState {
return {
characterToIgnore: '',
incompleteLine: false
} as INormalizeNewlinesTextRewriterState;
}
public process(unknownState: TextRewriterState, text: string): string {
const state: INormalizeNewlinesTextRewriterState = unknownState as INormalizeNewlinesTextRewriterState;
let result: string = '';
if (text.length > 0) {
let i: number = 0;
do {
const c: string = text[i];
++i;
if (c === state.characterToIgnore) {
state.characterToIgnore = '';
} else if (c === '\r') {
result += this.newline;
state.characterToIgnore = '\n';
state.incompleteLine = false;
} else if (c === '\n') {
result += this.newline;
state.characterToIgnore = '\r';
state.incompleteLine = false;
} else {
result += c;
state.characterToIgnore = '';
state.incompleteLine = true;
}
} while (i < text.length);
}
return result;
}
public close(unknownState: TextRewriterState): string {
const state: INormalizeNewlinesTextRewriterState = unknownState as INormalizeNewlinesTextRewriterState;
state.characterToIgnore = '';
if (state.incompleteLine) {
state.incompleteLine = false;
return this.newline;
} else {
return '';
}
}
}