code-block-writer

  • Version 13.0.1
  • Published
  • 89.7 kB
  • No dependencies
  • MIT license

Install

npm i code-block-writer
yarn add code-block-writer
pnpm add code-block-writer

Overview

A simple code writer that assists with formatting and visualizing blocks of code.

Index

Classes

class CodeBlockWriter

class CodeBlockWriter {}
  • Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code.

constructor

constructor(opts?: Partial<Options>);
  • Constructor.

    Parameter opts

    Options for the writer.

method blankLine

blankLine: () => this;
  • Writes a blank line.

method blankLineIfLastNot

blankLineIfLastNot: () => this;
  • Writes a blank line if the last written text was not a blank line.

method block

block: (block?: () => void) => this;
  • Writes a block using braces.

    Parameter block

    Write using the writer within this block.

method closeComment

closeComment: () => this;
  • Writes text to exit a comment if in a comment.

method conditionalBlankLine

conditionalBlankLine: (condition: boolean | undefined) => this;
  • Writes a blank line if the condition is true.

    Parameter condition

    Condition to evaluate.

method conditionalNewLine

conditionalNewLine: (condition: boolean | undefined) => this;
  • Writes a newline if the condition is true.

    Parameter condition

    Condition to evaluate.

method conditionalWrite

conditionalWrite: {
(condition: boolean | undefined, textFunc: () => string): this;
(condition: boolean, text: string): this;
};
  • Conditionally writes text.

    Parameter condition

    Condition to evaluate.

    Parameter textFunc

    A function that returns a string to write if the condition is true.

  • Conditionally writes text.

    Parameter condition

    Condition to evaluate.

    Parameter text

    Text to write if the condition is true.

method conditionalWriteLine

conditionalWriteLine: {
(condition: boolean | undefined, textFunc: () => string): this;
(condition: boolean, text: string): this;
};
  • Conditionally writes a line of text.

    Parameter condition

    Condition to evaluate.

    Parameter textFunc

    A function that returns a string to write if the condition is true.

  • Conditionally writes a line of text.

    Parameter condition

    Condition to evaluate.

    Parameter text

    Text to write if the condition is true.

method endsWith

endsWith: (text: string) => boolean;
  • Gets if the writer ends with the provided text.

    Parameter text

    Text to check if the writer ends with the provided text.

method getIndentationLevel

getIndentationLevel: () => number;
  • Gets the current indentation level.

method getLastChar

getLastChar: () => string | undefined;
  • Gets the last char written.

method getLength

getLength: () => number;
  • Gets the length of the string in the writer.

method getOptions

getOptions: () => Options;
  • Gets the options.

method hangingIndent

hangingIndent: (action: () => void) => this;
  • Writes the text within the provided action with hanging indentation.

    Parameter action

    Action to perform with hanging indentation.

method hangingIndentUnlessBlock

hangingIndentUnlessBlock: (action: () => void) => this;
  • Writes the text within the provided action with hanging indentation unless writing a block.

    Parameter action

    Action to perform with hanging indentation unless a block is written.

method indent

indent: { (times?: number): this; (block: () => void): this };
  • Indents the code one level for the current line.

  • Indents a block of code.

    Parameter block

    Block to indent.

method inlineBlock

inlineBlock: (block?: () => void) => this;
  • Writes an inline block with braces.

    Parameter block

    Write using the writer within this block.

method isAtStartOfFirstLineOfBlock

isAtStartOfFirstLineOfBlock: () => boolean;
  • Gets if the writer is currently at the start of the first line of the text, block, or indentation block.

method isInComment

isInComment: () => boolean;
  • Gets if the writer is currently in a comment.

method isInString

isInString: () => boolean;
  • Gets if the writer is currently in a string.

method isLastBlankLine

isLastBlankLine: () => boolean;
  • Gets if the last chars written were for a blank line.

method isLastNewLine

isLastNewLine: () => boolean;
  • Gets if the last chars written were for a newline.

method isLastSpace

isLastSpace: () => boolean;
  • Gets if the last char written was a space.

method isLastTab

isLastTab: () => boolean;
  • Gets if the last char written was a tab.

method isOnFirstLineOfBlock

isOnFirstLineOfBlock: () => boolean;
  • Gets if the writer is currently on the first line of the text, block, or indentation block.

method iterateLastCharCodes

iterateLastCharCodes: <T>(
action: (charCode: number, index: number) => T | undefined
) => T | undefined;
  • Iterates over the writer character char codes in reverse order. The iteration stops when a non-null or undefined value is returned from the action. The returned value is then returned by the method.

    Remarks

    It is much more efficient to use this method rather than #toString() since #toString() will combine the internal array into a string. Additionally, this is slightly more efficient that iterateLastChars as this won't allocate a string per character.

method iterateLastChars

iterateLastChars: <T>(
action: (char: string, index: number) => T | undefined
) => T | undefined;
  • Iterates over the writer characters in reverse order. The iteration stops when a non-null or undefined value is returned from the action. The returned value is then returned by the method.

    Remarks

    It is much more efficient to use this method rather than #toString() since #toString() will combine the internal array into a string.

method newLine

newLine: () => this;
  • Writes a newline.

method newLineIfLastNot

newLineIfLastNot: () => this;
  • Writes a newline if the last line was not a newline.

method queueIndentationLevel

queueIndentationLevel: {
(indentationLevel: number): this;
(whitespaceText: string): this;
};
  • Queues the indentation level for the next lines written.

    Parameter indentationLevel

    Indentation level to queue.

  • Queues the indentation level for the next lines written using the provided indentation text.

    Parameter whitespaceText

    Gets the indentation level from the indentation text.

method quote

quote: { (): this; (text: string): this };
  • Writes a quote character.

  • Writes text surrounded in quotes.

    Parameter text

    Text to write.

method setIndentationLevel

setIndentationLevel: {
(indentationLevel: number): this;
(whitespaceText: string): this;
};
  • Sets the current indentation level.

    Parameter indentationLevel

    Indentation level to be at.

  • Sets the current indentation using the provided indentation text.

    Parameter whitespaceText

    Gets the indentation level from the indentation text.

method space

space: (times?: number) => this;
  • Writes a space.

    Parameter times

    Number of times to write a space.

method spaceIfLastNot

spaceIfLastNot: () => this;
  • Writes a space if the last character was not a space.

method tab

tab: (times?: number) => this;
  • Writes a tab.

    Parameter times

    Number of times to write a tab.

method tabIfLastNot

tabIfLastNot: () => this;
  • Writes a tab if the last character was not a tab.

method toString

toString: () => string;
  • Gets the writer's text.

method unsafeInsert

unsafeInsert: (pos: number, text: string) => this;
  • Inserts text at the provided position.

    This method is "unsafe" because it won't update the state of the writer unless inserting at the end position. It is biased towards being fast at inserting closer to the start or end, but slower to insert in the middle. Only use this if absolutely necessary.

    Parameter pos

    Position to insert at.

    Parameter text

    Text to insert.

method withIndentationLevel

withIndentationLevel: {
(indentationLevel: number, action: () => void): this;
(whitespaceText: string, action: () => void): this;
};
  • Sets the indentation level within the provided action and restores the writer's indentation state afterwards.

    Parameter indentationLevel

    Indentation level to set.

    Parameter action

    Action to perform with the indentation.

    Remarks

    Restores the writer's state after the action.

  • Sets the indentation level with the provided indentation text within the provided action and restores the writer's indentation state afterwards.

    Parameter whitespaceText

    Gets the indentation level from the indentation text.

    Parameter action

    Action to perform with the indentation.

method write

write: (text: string) => this;
  • Writes the provided text.

    Parameter text

    Text to write.

method writeLine

writeLine: (text: string) => this;
  • Writes a line of text.

    Parameter text

    String to write.

Interfaces

interface Options

interface Options {}
  • Options for the writer.

property indentNumberOfSpaces

indentNumberOfSpaces: number;
  • Number of spaces to indent when useTabs is false.

    Remarks

    Defaults to 4.

property newLine

newLine: '\n' | '\r\n';
  • Newline character.

    Remarks

    Defaults to \n.

property useSingleQuote

useSingleQuote: boolean;
  • Whether to use a single quote (true) or double quote (false).

    Remarks

    Defaults to false.

property useTabs

useTabs: boolean;
  • Whether to use tabs (true) or spaces (false).

    Remarks

    Defaults to false.

Package Files (1)

Dependencies (0)

No dependencies.

Dev Dependencies (5)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/code-block-writer.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/code-block-writer)
  • HTML
    <a href="https://www.jsdocs.io/package/code-block-writer"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>