Socket
Socket
Sign inDemoInstall

code-block-writer

Package Overview
Dependencies
Maintainers
1
Versions
85
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

code-block-writer - npm Package Compare versions

Comparing version 3.2.0 to 4.0.0

6

code-block-writer.d.ts
export default class CodeBlockWriter {
constructor(opts?: { newLine: string });
constructor(opts?: { newLine?: string; indentNumberOfSpaces?: number; useTabs?: boolean; });
block(block: () => void): CodeBlockWriter;
inlineBlock(block: () => void): CodeBlockWriter;
conditionalNewLine(condition: boolean): CodeBlockWriter;
conditionalWrite(condition: boolean, str: string): CodeBlockWriter;
conditionalWriteLine(condition: boolean, str: string): CodeBlockWriter;
getLength(): number;
writeLine(str: string): CodeBlockWriter;
newLineIfLastCharNotNewLine(): CodeBlockWriter;
newLineIfLastNotNewLine(): CodeBlockWriter;
newLine(): CodeBlockWriter;

@@ -10,0 +12,0 @@ spaceIfLastNotSpace(): CodeBlockWriter;

@@ -9,2 +9,3 @@ var CodeBlockWriter = (function () {

this._numberSpaces = (opts && opts.indentNumberOfSpaces) || 4;
this._useTabs = (opts && opts.useTabs) || false;
}

@@ -25,7 +26,13 @@ CodeBlockWriter.prototype.block = function (block) {

this._currentIndentation--;
this.newLineIfLastCharNotNewLine().write("}");
this.newLineIfLastNotNewLine().write("}");
return this;
};
CodeBlockWriter.prototype.conditionalWriteLine = function (condition, str) {
if (condition) {
this.writeLine(str);
}
return this;
};
CodeBlockWriter.prototype.writeLine = function (str) {
this.newLineIfLastCharNotNewLine();
this.newLineIfLastNotNewLine();
this.writeIndentingNewLines(str);

@@ -35,3 +42,3 @@ this.newLine();

};
CodeBlockWriter.prototype.newLineIfLastCharNotNewLine = function () {
CodeBlockWriter.prototype.newLineIfLastNotNewLine = function () {
if (!this.isLastCharANewLine()) {

@@ -42,2 +49,8 @@ this.newLine();

};
CodeBlockWriter.prototype.conditionalNewLine = function (condition) {
if (condition) {
this.newLine();
}
return this;
};
CodeBlockWriter.prototype.newLine = function () {

@@ -94,3 +107,4 @@ var willCreateAConsecutiveBlankLine = this.isLastLineBlankLine() && this.isCurrentLineBlank();

var consecutiveNewline = this._newLine + this._newLine;
if (text.lastIndexOf(consecutiveNewline) === text.length - consecutiveNewline.length) {
var lastIndexOfConsecutiveNewLines = text.lastIndexOf(consecutiveNewline);
if (lastIndexOfConsecutiveNewLines >= 0 && lastIndexOfConsecutiveNewLines === text.length - consecutiveNewline.length) {
text = text.substr(0, text.length - this._newLine.length);

@@ -142,3 +156,8 @@ }

CodeBlockWriter.prototype.writeIndentation = function () {
this._text += Array(this._getCurrentIndentationNumberSpaces() + 1).join(" ");
if (this._useTabs) {
this._text += Array(this._currentIndentation + 1).join("\t");
}
else {
this._text += Array(this._getCurrentIndentationNumberSpaces() + 1).join(" ");
}
};

@@ -145,0 +164,0 @@ CodeBlockWriter.prototype._getCurrentIndentationNumberSpaces = function () {

@@ -27,3 +27,9 @@ var assert = require("assert");

}
describe("write", function () {
describe("#write()", function () {
it("should write a single letter", function () {
var expected = "a";
doTest(expected, function (writer) {
writer.write("a");
});
});
it("should write the text", function () {

@@ -50,3 +56,3 @@ var expected = "test";

});
describe("block()", function () {
describe("#block()", function () {
it("should write text inside a block", function () {

@@ -95,3 +101,3 @@ var expected = "test {\n inside\n}\n";

});
describe("inlineBlock()", function () {
describe("#inlineBlock()", function () {
it("should do an inline block correctly", function () {

@@ -106,3 +112,3 @@ var expected = "someCall({\n console.log();\n});";

});
describe("writeLine()", function () {
describe("#writeLine()", function () {
it("should write some text on a line", function () {

@@ -127,7 +133,7 @@ var expected = "test\n";

});
describe("newLineIfLastCharNotNewLine()", function () {
describe("#newLineIfLastNotNewLine()", function () {
it("should do a newline if the last text was not a newline", function () {
var expected = "test\n";
doTest(expected, function (writer) {
writer.write("test").newLineIfLastCharNotNewLine();
writer.write("test").newLineIfLastNotNewLine();
});

@@ -138,7 +144,7 @@ });

doTest(expected, function (writer) {
writer.writeLine("test").newLineIfLastCharNotNewLine();
writer.writeLine("test").newLineIfLastNotNewLine();
});
});
});
describe("newLine()", function () {
describe("#newLine()", function () {
it("should do a newline when writing", function () {

@@ -197,3 +203,3 @@ var expected = "test\n";

});
describe("spaceIfLastNotSpace()", function () {
describe("#spaceIfLastNotSpace()", function () {
it("should do a space if the last character wasn't a space", function () {

@@ -218,3 +224,3 @@ var expected = "test ";

});
describe("getLength()", function () {
describe("#getLength()", function () {
it("should return the length", function () {

@@ -226,4 +232,16 @@ var writer = getWriter();

});
describe("conditionalWrite()", function () {
describe("#conditionalNewLine()", function () {
it("should write when the condition is true", function () {
doTest("t\n", function (writer) {
writer.write("t").conditionalNewLine(true);
});
});
it("should not write when the condition is false", function () {
doTest("t", function (writer) {
writer.write("t").conditionalNewLine(false);
});
});
});
describe("#conditionalWrite()", function () {
it("should write when the condition is true", function () {
doTest("test", function (writer) {

@@ -239,4 +257,16 @@ writer.conditionalWrite(true, "test");

});
describe("#conditionalWriteLine()", function () {
it("should write when the condition is true", function () {
doTest("test\n", function (writer) {
writer.conditionalWriteLine(true, "test");
});
});
it("should not write when the condition is false", function () {
doTest("", function (writer) {
writer.conditionalWriteLine(false, "test");
});
});
});
}
describe("numberSpaces", function () {
describe("indentNumberOfSpaces", function () {
var writer = new code_block_writer_1.default({ indentNumberOfSpaces: 2 });

@@ -251,3 +281,15 @@ writer.write("do").block(function () {

});
describe("useTabs", function () {
var writer = new code_block_writer_1.default({ useTabs: true });
writer.write("do").block(function () {
writer.write("do").block(function () {
writer.write("something");
});
});
var expected = "do {\n\tdo {\n\t\tsomething\n\t}\n}\n";
it("should use tabs", function () {
assert.equal(writer.toString(), expected);
});
});
//# sourceMappingURL=code-block-writer-tests.js.map
{
"name": "code-block-writer",
"version": "3.2.0",
"version": "4.0.0",
"description": "A simple code writer that assists with formatting and visualizing blocks of code.",

@@ -5,0 +5,0 @@ "main": "dist/code-block-writer.js",

code-block-writer
=================
[![npm version](https://badge.fury.io/js/code-block-writer.svg)](https://badge.fury.io/js/code-block-writer)
[![Build Status](https://travis-ci.org/dsherret/code-block-writer.svg)](https://travis-ci.org/dsherret/code-block-writer)

@@ -42,1 +43,16 @@ [![Coverage Status](https://coveralls.io/repos/dsherret/code-block-writer/badge.svg?branch=master&service=github)](https://coveralls.io/github/dsherret/code-block-writer?branch=master)

```
## 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
* `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

@@ -5,8 +5,10 @@ export default class CodeBlockWriter {

private _numberSpaces: number;
private _useTabs: boolean;
private _newLine: string;
private _isAtStartOfBlock = false;
constructor(opts: { newLine?: string; indentNumberOfSpaces?: number; } = null) {
constructor(opts: { newLine?: string; indentNumberOfSpaces?: number; useTabs?: boolean; } = null) {
this._newLine = (opts && opts.newLine) || "\n";
this._numberSpaces = (opts && opts.indentNumberOfSpaces) || 4;
this._useTabs = (opts && opts.useTabs) || false;
}

@@ -30,3 +32,3 @@

this._currentIndentation--;
this.newLineIfLastCharNotNewLine().write("}");
this.newLineIfLastNotNewLine().write("}");

@@ -36,4 +38,12 @@ return this;

conditionalWriteLine(condition: boolean, str: string) {
if (condition) {
this.writeLine(str);
}
return this;
}
writeLine(str: string) {
this.newLineIfLastCharNotNewLine();
this.newLineIfLastNotNewLine();
this.writeIndentingNewLines(str);

@@ -45,3 +55,3 @@ this.newLine();

newLineIfLastCharNotNewLine() {
newLineIfLastNotNewLine() {
if (!this.isLastCharANewLine()) {

@@ -54,2 +64,10 @@ this.newLine();

conditionalNewLine(condition: boolean) {
if (condition) {
this.newLine();
}
return this;
}
newLine() {

@@ -120,4 +138,5 @@ const willCreateAConsecutiveBlankLine = this.isLastLineBlankLine() && this.isCurrentLineBlank();

const consecutiveNewline = this._newLine + this._newLine;
const lastIndexOfConsecutiveNewLines = text.lastIndexOf(consecutiveNewline);
if (text.lastIndexOf(consecutiveNewline) === text.length - consecutiveNewline.length) {
if (lastIndexOfConsecutiveNewLines >= 0 && lastIndexOfConsecutiveNewLines === text.length - consecutiveNewline.length) {
text = text.substr(0, text.length - this._newLine.length);

@@ -184,3 +203,8 @@ }

private writeIndentation() {
this._text += Array(this._getCurrentIndentationNumberSpaces() + 1).join(" ");
if (this._useTabs) {
this._text += Array(this._currentIndentation + 1).join("\t");
}
else {
this._text += Array(this._getCurrentIndentationNumberSpaces() + 1).join(" ");
}
}

@@ -187,0 +211,0 @@

@@ -33,3 +33,11 @@ import * as assert from "assert";

describe("write", () => {
describe("#write()", () => {
it("should write a single letter", () => {
const expected = `a`;
doTest(expected, writer => {
writer.write("a");
});
});
it("should write the text", () => {

@@ -66,3 +74,3 @@ const expected = `test`;

describe("block()", () => {
describe("#block()", () => {
it("should write text inside a block", () => {

@@ -141,3 +149,3 @@ const expected =

describe("inlineBlock()", () => {
describe("#inlineBlock()", () => {
it("should do an inline block correctly", () => {

@@ -154,3 +162,3 @@ const expected = `someCall({\n console.log();\n});`;

describe("writeLine()", () => {
describe("#writeLine()", () => {
it("should write some text on a line", () => {

@@ -181,3 +189,3 @@ const expected = `test\n`;

describe("newLineIfLastCharNotNewLine()", () => {
describe("#newLineIfLastNotNewLine()", () => {
it("should do a newline if the last text was not a newline", () => {

@@ -187,3 +195,3 @@ const expected = `test\n`;

doTest(expected, writer => {
writer.write("test").newLineIfLastCharNotNewLine();
writer.write("test").newLineIfLastNotNewLine();
});

@@ -196,3 +204,3 @@ });

doTest(expected, writer => {
writer.writeLine("test").newLineIfLastCharNotNewLine();
writer.writeLine("test").newLineIfLastNotNewLine();
});

@@ -202,3 +210,3 @@ });

describe("newLine()", () => {
describe("#newLine()", () => {
it("should do a newline when writing", () => {

@@ -277,3 +285,3 @@ const expected = `test\n`;

describe("spaceIfLastNotSpace()", () => {
describe("#spaceIfLastNotSpace()", () => {
it("should do a space if the last character wasn't a space", () => {

@@ -304,3 +312,3 @@ const expected = `test `;

describe("getLength()", () => {
describe("#getLength()", () => {
it("should return the length", () => {

@@ -313,4 +321,18 @@ const writer = getWriter();

describe("conditionalWrite()", () => {
describe("#conditionalNewLine()", () => {
it("should write when the condition is true", () => {
doTest("t\n", writer => {
writer.write("t").conditionalNewLine(true);
});
});
it("should not write when the condition is false", () => {
doTest("t", writer => {
writer.write("t").conditionalNewLine(false);
});
});
});
describe("#conditionalWrite()", () => {
it("should write when the condition is true", () => {
doTest("test", writer => {

@@ -327,5 +349,19 @@ writer.conditionalWrite(true, "test");

});
describe("#conditionalWriteLine()", () => {
it("should write when the condition is true", () => {
doTest("test\n", writer => {
writer.conditionalWriteLine(true, "test");
});
});
it("should not write when the condition is false", () => {
doTest("", writer => {
writer.conditionalWriteLine(false, "test");
});
});
});
}
describe("numberSpaces", () => {
describe("indentNumberOfSpaces", () => {
const writer = new CodeBlockWriter({ indentNumberOfSpaces: 2 });

@@ -346,1 +382,22 @@ writer.write("do").block(() => {

});
describe("useTabs", () => {
const writer = new CodeBlockWriter({ useTabs: true });
writer.write("do").block(() => {
writer.write("do").block(() => {
writer.write("something");
});
});
const expected =
`do {
\tdo {
\t\tsomething
\t}
}
`;
it("should use tabs", () => {
assert.equal(writer.toString(), expected);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc