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 1.4.0 to 1.5.0

.travis.yml

3

code-block-writer.d.ts

@@ -9,4 +9,5 @@ declare module "code-block-writer" {

block(block: () => void): CodeBlockWriter;
getLength(): number;
writeLine(str: string): CodeBlockWriter;
newLineIfLastNotNewLine(): CodeBlockWriter;
newLineIfLastCharNotNewLine(): CodeBlockWriter;
newLine(): CodeBlockWriter;

@@ -13,0 +14,0 @@ spaceIfLastNotSpace(): CodeBlockWriter;

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

this._numberSpaces = 4;
this._lastWasNewLine = true;
this._isAtStartOfBlock = false;
this._newline = (opts && opts.newLine) || "\n";

@@ -15,2 +15,3 @@ }

this.newLine();
this._isAtStartOfBlock = true;
block();

@@ -22,3 +23,3 @@ this._currentIndentation--;

CodeBlockWriter.prototype.writeLine = function (str) {
this.newLineIfLastNotNewLine();
this.newLineIfLastCharNotNewLine();
this.write(str);

@@ -28,4 +29,4 @@ this.newLine();

};
CodeBlockWriter.prototype.newLineIfLastNotNewLine = function () {
if (!this._lastWasNewLine) {
CodeBlockWriter.prototype.newLineIfLastCharNotNewLine = function () {
if (this.getLastChar() !== this._newline) {
this.newLine();

@@ -36,6 +37,5 @@ }

CodeBlockWriter.prototype.newLine = function () {
if (this.isLastLineNotBlankNewLine()) {
if (this.isLastLineNotBlankNewLine() && !this._isAtStartOfBlock && this._text.length !== 0) {
this.write(this._newline);
}
this._lastWasNewLine = true;
return this;

@@ -51,11 +51,14 @@ };

CodeBlockWriter.prototype.write = function (str) {
if (this._lastWasNewLine) {
this._lastWasNewLine = false;
if (str !== this._newline) {
this._isAtStartOfBlock = false;
if (str != null && str.length > 0) {
if (str !== this._newline && this.getLastChar() === this._newline) {
this.writeIndentation();
}
this._text += str;
}
this._text += str;
return this;
};
CodeBlockWriter.prototype.getLength = function () {
return this._text.length;
};
CodeBlockWriter.prototype.toString = function () {

@@ -86,3 +89,3 @@ return this._text;

CodeBlockWriter.prototype.writeIndentation = function () {
this.write(Array(this._getCurrentIndentationNumberSpaces() + 1).join(" "));
this._text += Array(this._getCurrentIndentationNumberSpaces() + 1).join(" ");
};

@@ -89,0 +92,0 @@ CodeBlockWriter.prototype._getCurrentIndentationNumberSpaces = function () {

@@ -68,7 +68,7 @@ var assert = require("assert");

});
describe("newLineIfLastNotNewLine()", function () {
describe("newLineIfLastCharNotNewLine()", 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").newLineIfLastNotNewLine();
writer.write("test").newLineIfLastCharNotNewLine();
});

@@ -79,3 +79,3 @@ });

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

@@ -97,2 +97,10 @@ });

});
it("should not do a newline after doing a block", function () {
var expected = "test {\n test\n}\n";
doTest(expected, function (writer) {
writer.write("test").block(function () {
writer.newLine().writeLine("test");
});
});
});
it("should not do a newline if the last line was a blank line (no consecutive blank lines)", function () {

@@ -131,4 +139,11 @@ var expected = "test\n\n";

});
describe("getLength()", function () {
it("should return the length", function () {
var writer = getWriter();
writer.write("1234");
assert.equal(writer.getLength(), 4);
});
});
});
//# sourceMappingURL=code-block-writer-tests.js.map
{
"name": "code-block-writer",
"version": "1.4.0",
"version": "1.5.0",
"description": "A simple code writer that assists with formatting and visualizing blocks of code.",
"main": "dist/code-block-writer.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc"
"test": "gulp test",
"build": "gulp typescript"
},

@@ -10,0 +10,0 @@ "repository": {

@@ -5,4 +5,4 @@ export default class CodeBlockWriter {

private _numberSpaces = 4;
private _lastWasNewLine = true;
private _newline: string;
private _isAtStartOfBlock = false;

@@ -17,2 +17,3 @@ constructor(opts: { newLine: string } = null) {

this.newLine();
this._isAtStartOfBlock = true;
block();

@@ -26,3 +27,3 @@ this._currentIndentation--;

writeLine(str: string) {
this.newLineIfLastNotNewLine();
this.newLineIfLastCharNotNewLine();
this.write(str);

@@ -34,4 +35,4 @@ this.newLine();

newLineIfLastNotNewLine() {
if (!this._lastWasNewLine) {
newLineIfLastCharNotNewLine() {
if (this.getLastChar() !== this._newline) {
this.newLine();

@@ -44,8 +45,6 @@ }

newLine() {
if (this.isLastLineNotBlankNewLine()) {
if (this.isLastLineNotBlankNewLine() && !this._isAtStartOfBlock && this._text.length !== 0) {
this.write(this._newline);
}
this._lastWasNewLine = true;
return this;

@@ -65,15 +64,19 @@ }

write(str: string) {
if (this._lastWasNewLine) {
this._lastWasNewLine = false;
this._isAtStartOfBlock = false;
if (str !== this._newline) {
if (str != null && str.length > 0) {
if (str !== this._newline && this.getLastChar() === this._newline) {
this.writeIndentation();
}
this._text += str;
}
this._text += str;
return this;
}
getLength() {
return this._text.length;
}
toString() {

@@ -113,3 +116,3 @@ return this._text;

private writeIndentation() {
this.write(Array(this._getCurrentIndentationNumberSpaces() + 1).join(" "));
this._text += Array(this._getCurrentIndentationNumberSpaces() + 1).join(" ");
}

@@ -116,0 +119,0 @@

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

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

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

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

@@ -112,3 +112,3 @@ });

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

@@ -135,2 +135,12 @@ });

it("should not do a newline after doing a block", () => {
const expected = `test {\n test\n}\n`;
doTest(expected, writer => {
writer.write("test").block(() => {
writer.newLine().writeLine("test");
});
});
});
it("should not do a newline if the last line was a blank line (no consecutive blank lines)", () => {

@@ -178,2 +188,10 @@ const expected = `test\n\n`;

});
describe("getLength()", () => {
it("should return the length", () => {
const writer = getWriter();
writer.write("1234");
assert.equal(writer.getLength(), 4);
});
});
});
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