1 | # code-block-writer
|
2 |
|
3 | [![npm version](https://badge.fury.io/js/code-block-writer.svg)](https://badge.fury.io/js/code-block-writer)
|
4 | [![CI](https://github.com/dsherret/code-block-writer/workflows/CI/badge.svg)](https://github.com/dsherret/code-block-writer/actions?query=workflow%3ACI)
|
5 | [![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/code_block_writer/mod.ts)
|
6 | [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)
|
7 |
|
8 | Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code.
|
9 |
|
10 | With Deno/Browser:
|
11 |
|
12 | ```ts
|
13 | import CodeBlockWriter from "https://deno.land/x/code_block_writer/mod.ts";
|
14 | ```
|
15 |
|
16 | Or with Node:
|
17 |
|
18 | ```
|
19 | npm install --save code-block-writer
|
20 | ```
|
21 |
|
22 | ## Example
|
23 |
|
24 |
|
25 |
|
26 | ```typescript
|
27 | import CodeBlockWriter from "https://deno.land/x/code_block_writer/mod.ts";
|
28 |
|
29 | const writer = new CodeBlockWriter({
|
30 | // optional options
|
31 | newLine: "\r\n", // default: "\n"
|
32 | indentNumberOfSpaces: 2, // default: 4
|
33 | useTabs: false, // default: false
|
34 | useSingleQuote: true // default: false
|
35 | });
|
36 | const className = "MyClass";
|
37 |
|
38 | writer.write(`class ${className} extends OtherClass`).block(() => {
|
39 | writer.writeLine(`@MyDecorator(1, 2)`);
|
40 | writer.write(`myMethod(myParam: any)`).block(() => {
|
41 | writer.write("return this.post(").quote("myArgument").write(");");
|
42 | });
|
43 | });
|
44 |
|
45 | console.log(writer.toString());
|
46 | ```
|
47 |
|
48 | Outputs (using "\r\n" for newlines):
|
49 |
|
50 | ```text
|
51 | class MyClass extends OtherClass {
|
52 | @MyDecorator(1, 2)
|
53 | myMethod(myParam: any) {
|
54 | return this.post('myArgument');
|
55 | }
|
56 | }
|
57 | ```
|
58 |
|
59 | ## Methods
|
60 |
|
61 | - `block(block?: () => void)` - Indents all the code written within and surrounds it in braces.
|
62 | - `inlineBlock(block?: () => void)` - Same as block, but doesn't add a space before the first brace and doesn't add a newline at the end.
|
63 | - `getLength()` - Get the current number of characters.
|
64 | - `writeLine(text: string)` - Writes some text and adds a newline.
|
65 | - `newLine()` - Writes a newline.
|
66 | - `newLineIfLastNot()` - Writes a newline if what was written last wasn't a newline.
|
67 | - `blankLine()` - Writes a blank line. Does not allow consecutive blank lines.
|
68 | - `blankLineIfLastNot()` - Writes a blank line if what was written last wasn't a blank line.
|
69 | - `quote()` - Writes a quote character.
|
70 | - `quote(text: string)` - Writes text surrounded in quotes.
|
71 | - `indent(times?: number)` - Indents the current line. Optionally indents multiple times when providing a number.
|
72 | - `indent(block?: () => void)` - Indents a block of code.
|
73 | - `space(times?: number)` - Writes a space. Optionally writes multiple spaces when providing a number.
|
74 | - `spaceIfLastNot()` - Writes a space if the last was not a space.
|
75 | - `tab(times?: number)` - Writes a tab. Optionally writes multiple tabs when providing a number.
|
76 | - `tabIfLastNot()` - Writes a tab if the last was not a tab.
|
77 | - `write(text: string)` - Writes some text.
|
78 | - `conditionalNewLine(condition: boolean)` - Writes a newline if the condition is matched.
|
79 | - `conditionalBlankLine(condition: boolean)` - Writes a blank line if the condition is matched.
|
80 | - `conditionalWrite(condition: boolean, text: string)` - Writes if the condition is matched.
|
81 | - `conditionalWrite(condition: boolean, textFunc: () => string)` - Writes if the condition is matched.
|
82 | - `conditionalWriteLine(condition: boolean, text: string)` - Writes some text and adds a newline if the condition is matched.
|
83 | - `conditionalWriteLine(condition: boolean, textFunc: () => string)` - Writes some text and adds a newline if the condition is matched.
|
84 | - `setIndentationLevel(indentationLevel: number)` - Sets the current indentation level.
|
85 | - `setIndentationLevel(whitespaceText: string)` - Sets the current indentation level based on the provided whitespace text.
|
86 | - `withIndentationLevel(indentationLevel: number, action: () => void)` - Sets the indentation level within the provided action.
|
87 | - `withIndentationLevel(whitespaceText: string, action: () => void)` - Sets the indentation level based on the provided whitespace text within the action.
|
88 | - `getIndentationLevel()` - Gets the current indentation level.
|
89 | - `queueIndentationLevel(indentationLevel: number)` - Queues an indentation level to be used once a new line is written.
|
90 | - `queueIndentationLevel(whitespaceText: string)` - Queues an indentation level to be used once a new line is written based on the provided whitespace text.
|
91 | - `hangingIndent(action: () => void)` - Writes the code within the action with hanging indentation.
|
92 | - `hangingIndentUnlessBlock(action: () => void)` - Writes the code within the action with hanging indentation unless a block is written going from the first line to the second.
|
93 | - `closeComment()` - Writes text to exit a comment if in a comment.
|
94 | - `unsafeInsert(pos: number, text: string)` - Inserts text into the writer. This will not update the writer's state. Read more in its jsdoc.
|
95 | - `isInComment()` - Gets if the writer is currently in a comment.
|
96 | - `isAtStartOfFirstLineOfBlock()` - Gets if the writer is currently at the start of the first line of the text, block, or indentation block.
|
97 | - `isOnFirstLineOfBlock()` - Gets if the writer is currently on the first line of the text, block, or indentation block.
|
98 | - `isInString()` - Gets if the writer is currently in a string.
|
99 | - `isLastNewLine()` - Gets if the writer last wrote a newline.
|
100 | - `isLastBlankLine()` - Gets if the writer last wrote a blank line.
|
101 | - `isLastSpace()` - Gets if the writer last wrote a space.
|
102 | - `isLastTab()` - Gets if the writer last wrote a tab.
|
103 | - `getLastChar()` - Gets the last character written.
|
104 | - `endsWith(text: string)` - Gets if the writer ends with the provided text.
|
105 | - `iterateLastChars<T>(action: (char: string, index: number) => T | undefined): T | undefined` - Iterates over the writer's characters in reverse order, stopping once a non-null or undefined value is returned and returns that value.
|
106 | - `iterateLastCharCodes<T>(action: (charCode: number, index: number) => T | undefined): T | undefined` - A slightly faster version of `iterateLastChars` that doesn't allocate a string per character.
|
107 | - `getOptions()` - Gets the writer options.
|
108 | - `toString()` - Gets the string.
|
109 |
|
110 | ## Other Features
|
111 |
|
112 | - Does not indent within strings.
|
113 | - Escapes newlines within double and single quotes created with `.quote(text)`.
|
114 |
|
115 | ## C# Version
|
116 |
|
117 | See [CodeBlockWriterSharp](https://github.com/dsherret/CodeBlockWriterSharp).
|