code-block-writer
Advanced tools
Comparing version 2.0.5 to 3.0.0
@@ -23,3 +23,3 @@ var CodeBlockWriter = (function () { | ||
this.newLineIfLastCharNotNewLine(); | ||
this.write(str); | ||
this.writeIndentingNewLines(str); | ||
this.newLine(); | ||
@@ -37,3 +37,3 @@ return this; | ||
if (!willCreateAConsecutiveBlankLine && !this._isAtStartOfBlock && this._text.length !== 0) { | ||
this.write(this._newLine); | ||
this.baseWrite(this._newLine); | ||
} | ||
@@ -45,3 +45,3 @@ return this; | ||
if (lastChar != null && lastChar !== " " && !this.isLastCharANewLine()) { | ||
this.write(" "); | ||
this.baseWrite(" "); | ||
} | ||
@@ -51,2 +51,21 @@ return this; | ||
CodeBlockWriter.prototype.write = function (str) { | ||
this.writeIndentingNewLines(str); | ||
return this; | ||
}; | ||
CodeBlockWriter.prototype.getLength = function () { | ||
return this._text.length; | ||
}; | ||
CodeBlockWriter.prototype.toString = function () { | ||
return this.removeConsecutiveNewLineAtEndOfString(this._text); | ||
}; | ||
CodeBlockWriter.prototype.writeIndentingNewLines = function (str) { | ||
var _this = this; | ||
(str || "").split(/\r?\n/).forEach(function (s, i) { | ||
if (i > 0) { | ||
_this.newLine(); | ||
} | ||
_this.baseWrite(s); | ||
}); | ||
}; | ||
CodeBlockWriter.prototype.baseWrite = function (str) { | ||
this._isAtStartOfBlock = false; | ||
@@ -61,8 +80,2 @@ if (str != null && str.length > 0) { | ||
}; | ||
CodeBlockWriter.prototype.getLength = function () { | ||
return this._text.length; | ||
}; | ||
CodeBlockWriter.prototype.toString = function () { | ||
return this.removeConsecutiveNewLineAtEndOfString(this._text); | ||
}; | ||
CodeBlockWriter.prototype.removeConsecutiveNewLineAtEndOfString = function (text) { | ||
@@ -69,0 +82,0 @@ var consecutiveNewline = this._newLine + this._newLine; |
@@ -40,2 +40,10 @@ var assert = require("assert"); | ||
}); | ||
it("should indent if it's passed a newline character inside a block", function () { | ||
var expected = "test {\n inside\n inside\n}\n"; | ||
doTest(expected, function (writer) { | ||
writer.write("test ").block(function () { | ||
writer.write("inside" + opts.newLine + "inside"); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -165,2 +173,10 @@ describe("block()", function () { | ||
}); | ||
it("should indent if it's passed a newline character inside a block", function () { | ||
var expected = "test {\n inside\n inside\n}\n"; | ||
doTest(expected, function (writer) { | ||
writer.write("test ").block(function () { | ||
writer.writeLine("inside" + opts.newLine + "inside"); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -167,0 +183,0 @@ describe("spaceIfLastNotSpace()", function () { |
{ | ||
"name": "code-block-writer", | ||
"version": "2.0.5", | ||
"version": "3.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", |
@@ -27,3 +27,3 @@ export default class CodeBlockWriter { | ||
this.newLineIfLastCharNotNewLine(); | ||
this.write(str); | ||
this.writeIndentingNewLines(str); | ||
this.newLine(); | ||
@@ -46,3 +46,3 @@ | ||
if (!willCreateAConsecutiveBlankLine && !this._isAtStartOfBlock && this._text.length !== 0) { | ||
this.write(this._newLine); | ||
this.baseWrite(this._newLine); | ||
} | ||
@@ -57,3 +57,3 @@ | ||
if (lastChar != null && lastChar !== " " && !this.isLastCharANewLine()) { | ||
this.write(" "); | ||
this.baseWrite(" "); | ||
} | ||
@@ -65,2 +65,24 @@ | ||
write(str: string) { | ||
this.writeIndentingNewLines(str); | ||
return this; | ||
} | ||
getLength() { | ||
return this._text.length; | ||
} | ||
toString() { | ||
return this.removeConsecutiveNewLineAtEndOfString(this._text); | ||
} | ||
private writeIndentingNewLines(str: string) { | ||
(str || "").split(/\r?\n/).forEach((s, i) => { | ||
if (i > 0) { | ||
this.newLine(); | ||
} | ||
this.baseWrite(s); | ||
}); | ||
} | ||
private baseWrite(str: string) { | ||
this._isAtStartOfBlock = false; | ||
@@ -79,10 +101,2 @@ | ||
getLength() { | ||
return this._text.length; | ||
} | ||
toString() { | ||
return this.removeConsecutiveNewLineAtEndOfString(this._text); | ||
} | ||
private removeConsecutiveNewLineAtEndOfString(text: string) { | ||
@@ -89,0 +103,0 @@ const consecutiveNewline = this._newLine + this._newLine; |
@@ -49,2 +49,16 @@ import * as assert from "assert"; | ||
}); | ||
it("should indent if it's passed a newline character inside a block", () => { | ||
const expected = | ||
`test { | ||
inside | ||
inside | ||
} | ||
`; | ||
doTest(expected, writer => { | ||
writer.write("test ").block(() => { | ||
writer.write(`inside${opts.newLine}inside`); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -228,2 +242,16 @@ | ||
}); | ||
it("should indent if it's passed a newline character inside a block", () => { | ||
const expected = | ||
`test { | ||
inside | ||
inside | ||
} | ||
`; | ||
doTest(expected, writer => { | ||
writer.write("test ").block(() => { | ||
writer.writeLine(`inside${opts.newLine}inside`); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -230,0 +258,0 @@ |
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
36343
893