Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/terminal > TextRewriterState

TextRewriterState type

Represents the internal state of a TextRewriter subclass.

Signature:

export type TextRewriterState = Brand<unknown, 'TextRewriterState'>;

References: Brand

Remarks

For example, suppose that 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:

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.