JSPM

  • Created
  • Published
  • Downloads 4232539
  • Score
    100M100P100Q210406F
  • License MIT

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

Package Exports

  • code-block-writer

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (code-block-writer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

code-block-writer

npm version Build Status Coverage Status stable

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

npm install --save code-block-writer

Example

import CodeBlockWriter from "code-block-writer";

const writer = new CodeBlockWriter({
    // optional options
    newLine: "\r\n",         // default: "\n"
    indentNumberOfSpaces: 2, // default: 4
    useTabs: false           // default: false
});
const className = "MyClass";

writer.write(`class ${className} extends OtherClass`).block(() => {
    writer.writeLine(`@MyDecorator("myArgument1", "myArgument2")`);
    writer.write(`myMethod(myParam: any)`).block(() => {
        writer.write(`return this.post("myArgument");`);
    });
});

console.log(writer.toString());

Outputs:

class MyClass extends OtherClass {
   @MyDecorator("myArgument1", "myArgument2")
   myMethod(myParam: any) {
       return this.post("myArgument");
   }
}

Methods

  • block(block: () => void) - Indents all the code written within and surrounds it in braces.
  • 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.
  • getLength() - Get the current number of characters.
  • writeLine(str: string) - Writes some text and adds a newline.
  • newLineIfLastNotNewLine() - Writes a newline if what was written last wasn't a newline.
  • newLine() - Writes a newline.
  • blankLine() - Writes a blank line. Does not allow consecutive blank lines.
  • indent() - Indents the current line.
  • spaceIfLastNotSpace() - Writes a space if the last was not a space.
  • write(str: string) - Writes some text.
  • conditionalNewLine(condition: boolean) - Writes a newline if the condition is matched.
  • conditionalWrite(condition: boolean, str: string) - Writes if the condition is matched.
  • conditionalWriteLine(condition: boolean, str: string) - Writes some text and adds a newline if the condition is matched.
  • toString() - Gets the string.