{"id":15631363,"url":"https://github.com/dsherret/code-block-writer","last_synced_at":"2025-04-13T11:46:54.101Z","repository":{"id":41902088,"uuid":"44211976","full_name":"dsherret/code-block-writer","owner":"dsherret","description":"Code writer for JavaScript and TypeScript code.","archived":false,"fork":false,"pushed_at":"2024-10-05T13:29:59.000Z","size":448,"stargazers_count":204,"open_issues_count":4,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-06T08:08:19.228Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dsherret.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-10-13T23:42:29.000Z","updated_at":"2025-04-03T05:32:35.000Z","dependencies_parsed_at":"2024-06-18T13:49:28.946Z","dependency_job_id":"eefa974e-948b-4117-9231-a47655605ac2","html_url":"https://github.com/dsherret/code-block-writer","commit_stats":{"total_commits":250,"total_committers":4,"mean_commits":62.5,"dds":"0.016000000000000014","last_synced_commit":"96de18325769606589307281ce355f18eca830b2"},"previous_names":[],"tags_count":74,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsherret%2Fcode-block-writer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsherret%2Fcode-block-writer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsherret%2Fcode-block-writer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsherret%2Fcode-block-writer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dsherret","download_url":"https://codeload.github.com/dsherret/code-block-writer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710408,"owners_count":21149185,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T10:40:03.867Z","updated_at":"2025-04-13T11:46:54.078Z","avatar_url":"https://github.com/dsherret.png","language":"TypeScript","readme":"# code-block-writer\n\n[![npm version](https://badge.fury.io/js/code-block-writer.svg)](https://badge.fury.io/js/code-block-writer)\n[![CI](https://github.com/dsherret/code-block-writer/workflows/CI/badge.svg)](https://github.com/dsherret/code-block-writer/actions?query=workflow%3ACI)\n[![JSR](https://jsr.io/badges/@david/code-block-writer)](https://jsr.io/@david/code-block-writer)\n[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)\n\nCode writer for JavaScript and TypeScript code.\n\nWith Deno:\n\n```ts\ndeno add jsr:@david/code-block-writer\n```\n\nOr with Node:\n\n```\nnpm install --save code-block-writer\n```\n\n## Example\n\n\u003c!-- dprint-ignore --\u003e\n\n```typescript\n// import CodeBlockWriter from \"code-block-writer\"; // for npm\nimport CodeBlockWriter from \"@david/code-block-writer\";\n\nconst writer = new CodeBlockWriter({\n  // optional options\n  newLine: \"\\r\\n\",         // default: \"\\n\"\n  indentNumberOfSpaces: 2, // default: 4\n  useTabs: false,          // default: false\n  useSingleQuote: true     // default: false\n});\n\nwriter.write(\"class MyClass extends OtherClass\").block(() =\u003e {\n  writer.writeLine(`@MyDecorator(1, 2)`);\n  writer.write(`myMethod(myParam: any)`).block(() =\u003e {\n    writer.write(\"return this.post(\").quote(\"myArgument\").write(\");\");\n  });\n});\n\nconsole.log(writer.toString());\n```\n\nOutputs (using \"\\r\\n\" for newlines):\n\n\u003c!-- dprint-ignore --\u003e\n\n```js\nclass MyClass extends OtherClass {\n  @MyDecorator(1, 2)\n  myMethod(myParam: any) {\n    return this.post('myArgument');\n  }\n}\n```\n\n## Methods\n\n- `block(block?: () =\u003e void)` - Indents all the code written within and surrounds it in braces.\n- `inlineBlock(block?: () =\u003e void)` - Same as block, but doesn't add a space before the first brace and doesn't add a newline at the end.\n- `getLength()` - Get the current number of characters.\n- `writeLine(text: string)` - Writes some text and adds a newline.\n- `newLine()` - Writes a newline.\n- `newLineIfLastNot()` - Writes a newline if what was written last wasn't a newline.\n- `blankLine()` - Writes a blank line. Does not allow consecutive blank lines.\n- `blankLineIfLastNot()` - Writes a blank line if what was written last wasn't a blank line.\n- `quote()` - Writes a quote character.\n- `quote(text: string)` - Writes text surrounded in quotes.\n- `indent(times?: number)` - Indents the current line. Optionally indents multiple times when providing a number.\n- `indent(block?: () =\u003e void)` - Indents a block of code.\n- `space(times?: number)` - Writes a space. Optionally writes multiple spaces when providing a number.\n- `spaceIfLastNot()` - Writes a space if the last was not a space.\n- `tab(times?: number)` - Writes a tab. Optionally writes multiple tabs when providing a number.\n- `tabIfLastNot()` - Writes a tab if the last was not a tab.\n- `write(text: string)` - Writes some text.\n- `conditionalNewLine(condition: boolean)` - Writes a newline if the condition is matched.\n- `conditionalBlankLine(condition: boolean)` - Writes a blank line if the condition is matched.\n- `conditionalWrite(condition: boolean, text: string)` - Writes if the condition is matched.\n- `conditionalWrite(condition: boolean, textFunc: () =\u003e string)` - Writes if the condition is matched.\n- `conditionalWriteLine(condition: boolean, text: string)` - Writes some text and adds a newline if the condition is matched.\n- `conditionalWriteLine(condition: boolean, textFunc: () =\u003e string)` - Writes some text and adds a newline if the condition is matched.\n- `setIndentationLevel(indentationLevel: number)` - Sets the current indentation level.\n- `setIndentationLevel(whitespaceText: string)` - Sets the current indentation level based on the provided whitespace text.\n- `withIndentationLevel(indentationLevel: number, action: () =\u003e void)` - Sets the indentation level within the provided action.\n- `withIndentationLevel(whitespaceText: string, action: () =\u003e void)` - Sets the indentation level based on the provided whitespace text within the action.\n- `getIndentationLevel()` - Gets the current indentation level.\n- `queueIndentationLevel(indentationLevel: number)` - Queues an indentation level to be used once a new line is written.\n- `queueIndentationLevel(whitespaceText: string)` - Queues an indentation level to be used once a new line is written based on the provided whitespace text.\n- `hangingIndent(action: () =\u003e void)` - Writes the code within the action with hanging indentation.\n- `hangingIndentUnlessBlock(action: () =\u003e void)` - Writes the code within the action with hanging indentation unless a block is written going from the first line to the second.\n- `closeComment()` - Writes text to exit a comment if in a comment.\n- `unsafeInsert(pos: number, text: string)` - Inserts text into the writer. This will not update the writer's state. Read more in its jsdoc.\n- `isInComment()` - Gets if the writer is currently in a comment.\n- `isAtStartOfFirstLineOfBlock()` - Gets if the writer is currently at the start of the first line of the text, block, or indentation block.\n- `isOnFirstLineOfBlock()` - Gets if the writer is currently on the first line of the text, block, or indentation block.\n- `isInString()` - Gets if the writer is currently in a string.\n- `isLastNewLine()` - Gets if the writer last wrote a newline.\n- `isLastBlankLine()` - Gets if the writer last wrote a blank line.\n- `isLastSpace()` - Gets if the writer last wrote a space.\n- `isLastTab()` - Gets if the writer last wrote a tab.\n- `getLastChar()` - Gets the last character written.\n- `endsWith(text: string)` - Gets if the writer ends with the provided text.\n- `iterateLastChars\u003cT\u003e(action: (char: string, index: number) =\u003e 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.\n- `iterateLastCharCodes\u003cT\u003e(action: (charCode: number, index: number) =\u003e T | undefined): T | undefined` - A slightly faster version of `iterateLastChars` that doesn't allocate a string per character.\n- `getOptions()` - Gets the writer options.\n- `toString()` - Gets the string.\n\n## Other Features\n\n- Does not indent within strings.\n- Escapes newlines within double and single quotes created with `.quote(text)`.\n\n## C# Version\n\nSee [CodeBlockWriterSharp](https://github.com/dsherret/CodeBlockWriterSharp).\n","funding_links":[],"categories":["Packages"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsherret%2Fcode-block-writer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdsherret%2Fcode-block-writer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsherret%2Fcode-block-writer/lists"}