-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathTextRewriter.ts
76 lines (71 loc) · 2.94 KB
/
TextRewriter.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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type { Brand } from '@rushstack/node-core-library';
/**
* Represents the internal state of a {@link TextRewriter} subclass.
*
* @remarks
* For example, suppose that {@link NormalizeNewlinesTextRewriter} will be used to rewrite
* the input `"line 1\r\nline 2\r\n"` to become `"line 1\nline 2\n"`. But suppose that the `"\r\n"`
* pair is split across two chunks:
*
* ```ts
* const rewriter: NormalizeNewlinesTextRewriter = new NormalizeNewlinesTextRewriter(NewlineKind.Lf);
* const state: TextRewriterState = rewriter.initialize();
* let output: string = rewriter.process(state, 'line 1\r');
* output += rewriter.process(state, '\nline 2\r\n');
* output += rewriter.close(state);
*
* // The final "output" value is: "line 1\nline 2\n"
* ```
*
* The `TextRewriterState` keeps track of this context, so that split `"\r"` and `"\n"` are
* interpreted as a single newline.
*
* @public
*/
export type TextRewriterState = Brand<unknown, 'TextRewriterState'>;
/**
* The abstract base class for operations that can be applied by {@link TextRewriterTransform}.
*
* @remarks
* The {@link TextRewriterTransform} applies one or more character rewriting operations to its
* chunk stream. Since these operations are applied separately to `stderr` and `stdout`, the
* state is stored in an opaque `TextRewriterState` object.
*
* Conceptually, a `TextRewriter` subclass is very similar to a regular expression, with the difference
* that `RegExp` operates on a text string, whereas `TextRewriter` operates on a stream of characters.
*
* The two most common subclasses are {@link NormalizeNewlinesTextRewriter} and {@link RemoveColorsTextRewriter}.
*
* A rewriting operation starts with `initialize()`, followed by any number of `process()` calls, and
* then finishes with `close()`. For example:
*
* ```ts
* const rewriter: NormalizeNewlinesTextRewriter = new NormalizeNewlinesTextRewriter(NewlineKind.Lf);
* const state: TextRewriterState = rewriter.initialize();
* let output: string = rewriter.process(state, 'line 1\r');
* output += rewriter.process(state, '\nline 2\r\n');
* output += rewriter.close(state);
*
* // The final "output" value is: "line 1\nline 2\n"
* ```
*
* After `close()` has been called, the `TextRewriterState` state should not be reused.
*
* @public
*/
export abstract class TextRewriter {
/**
* Create a new `TextRewriterState` object that can be used to process a stream of characters.
*/
public abstract initialize(): TextRewriterState;
/**
* Rewrite the next sequence of characters from the input stream, returning the modified output.
*/
public abstract process(state: TextRewriterState, input: string): string;
/**
* Close the `TextRewriterState` object and return any buffered output.
*/
public abstract close(state: TextRewriterState): string;
}