code-block-writer
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -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); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
121320
17
2802
0
0