Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/code_block_writer/mod.ts>default

Code writer for JavaScript and TypeScript code.
Go to Latest
class default
import { default } from "https://deno.land/x/code_block_writer@11.0.0/mod.ts";

Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code.

Constructors

new
default(opts?: Partial<Options>)

Constructor.

Properties

private
_currentCommentChar: CommentChar | undefined
private
_currentIndentation: number
private
readonly
_indentationText: string
private
readonly
_indentNumberOfSpaces: number
private
_isInRegEx: boolean
private
_isOnFirstLineOfBlock: boolean
private
_length: number
private
readonly
_newLine: "\n" | "\r\n"
private
_newLineOnNextWrite: boolean
private
_queuedIndentation: number | undefined
private
_queuedOnlyIfNotBlock: true | undefined
private
readonly
_quoteChar: string
private
_stringCharStack: number[]
private
_texts: string[]
private
readonly
_useTabs: boolean

Methods

private
_getIndentationLevelFromArg(countOrText: string | number)
private
_getIndentationState(): IndentationLevelState
private
_indentBlockInternal(block?: () => void)
private
_internalWrite(text: string)
private
_setIndentationState(state: IndentationLevelState)
private
_updateInternalState(str: string)
private
_withResetIndentation(setStateAction: () => void, writeAction: () => void)
private
_writeIndentingNewLines(text: string)
blankLine(): this

Writes a blank line.

Writes a blank line if the last written text was not a blank line.

block(block?: () => void): this

Writes a block using braces.

closeComment(): this

Writes text to exit a comment if in a comment.

conditionalBlankLine(condition: boolean | undefined): this

Writes a blank line if the condition is true.

conditionalNewLine(condition: boolean | undefined): this

Writes a newline if the condition is true.

conditionalWrite(condition: boolean | undefined, textFunc: () => string): this

Conditionally writes text.

conditionalWrite(condition: boolean | undefined, text: string): this

Conditionally writes text.

conditionalWriteLine(condition: boolean | undefined, textFunc: () => string): this

Conditionally writes a line of text.

conditionalWriteLine(condition: boolean | undefined, text: string): this

Conditionally writes a line of text.

endsWith(text: string): boolean

Gets if the writer ends with the provided text.

Gets the current indentation level.

getLastChar(): string | undefined

Gets the last char written.

getLength(): number

Gets the length of the string in the writer.

Gets the options.

hangingIndent(action: () => void): this

Writes the text within the provided action with hanging indentation.

hangingIndentUnlessBlock(action: () => void): this

Writes the text within the provided action with hanging indentation unless writing a block.

indent(times?: number): this

Indents the code one level for the current line.

indent(block: () => void): this

Indents a block of code.

inlineBlock(block?: () => void): this

Writes an inline block with braces.

Gets if the writer is currently at the start of the first line of the text, block, or indentation block.

isInComment(): boolean

Gets if the writer is currently in a comment.

isInString(): boolean

Gets if the writer is currently in a string.

isLastBlankLine(): boolean

Gets if the last chars written were for a blank line.

isLastNewLine(): boolean

Gets if the last chars written were for a newline.

isLastSpace(): boolean

Gets if the last char written was a space.

isLastTab(): boolean

Gets if the last char written was a tab.

Gets if the writer is currently on the first line of the text, block, or indentation block.

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.

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.

newLine(): this

Writes a newline.

Writes a newline if the last line was not a newline.

queueIndentationLevel(indentationLevel: number): this

Queues the indentation level for the next lines written.

queueIndentationLevel(whitespaceText: string): this

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

queueIndentationLevel(countOrText: string | number): this
quote(): this

Writes a quote character.

quote(text: string): this

Writes text surrounded in quotes.

setIndentationLevel(indentationLevel: number): this

Sets the current indentation level.

setIndentationLevel(whitespaceText: string): this

Sets the current indentation using the provided indentation text.

setIndentationLevel(countOrText: string | number): this
space(times?): this

Writes a space.

Writes a space if the last character was not a space.

tab(times?): this

Writes a tab.

tabIfLastNot(): this

Writes a tab if the last character was not a tab.

toString(): string

Gets the writer's text.

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.

withIndentationLevel(indentationLevel: number, action: () => void): this

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

withIndentationLevel(whitespaceText: string, action: () => void): this

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

write(text: string): this

Writes the provided text.

writeLine(text: string): this

Writes a line of text.

Static Properties

private
readonly
_newLineRegEx: RegExp
private
readonly
_spacesOrTabsRegEx: RegExp