code-block-writer
Advanced tools
Comparing version 4.0.0 to 4.1.0
export default class CodeBlockWriter { | ||
constructor(opts?: { newLine?: string; indentNumberOfSpaces?: number; useTabs?: boolean; }); | ||
blankLine(): CodeBlockWriter; | ||
block(block: () => void): CodeBlockWriter; | ||
@@ -4,0 +5,0 @@ inlineBlock(block: () => void): CodeBlockWriter; |
@@ -0,4 +1,4 @@ | ||
"use strict"; | ||
var CodeBlockWriter = (function () { | ||
function CodeBlockWriter(opts) { | ||
if (opts === void 0) { opts = null; } | ||
this._currentIndentation = 0; | ||
@@ -29,5 +29,4 @@ this._text = ""; | ||
CodeBlockWriter.prototype.conditionalWriteLine = function (condition, str) { | ||
if (condition) { | ||
if (condition) | ||
this.writeLine(str); | ||
} | ||
return this; | ||
@@ -42,11 +41,12 @@ }; | ||
CodeBlockWriter.prototype.newLineIfLastNotNewLine = function () { | ||
if (!this.isLastCharANewLine()) { | ||
if (!this.isLastCharANewLine()) | ||
this.newLine(); | ||
} | ||
return this; | ||
}; | ||
CodeBlockWriter.prototype.blankLine = function () { | ||
return this.newLine().newLine(); | ||
}; | ||
CodeBlockWriter.prototype.conditionalNewLine = function (condition) { | ||
if (condition) { | ||
if (condition) | ||
this.newLine(); | ||
} | ||
return this; | ||
@@ -56,5 +56,4 @@ }; | ||
var willCreateAConsecutiveBlankLine = this.isLastLineBlankLine() && this.isCurrentLineBlank(); | ||
if (!willCreateAConsecutiveBlankLine && !this._isAtStartOfBlock && this._text.length !== 0) { | ||
if (!willCreateAConsecutiveBlankLine && !this._isAtStartOfBlock && this._text.length !== 0) | ||
this.baseWrite(this._newLine); | ||
} | ||
return this; | ||
@@ -64,11 +63,9 @@ }; | ||
var lastChar = this.getLastChar(); | ||
if (lastChar != null && lastChar !== " " && !this.isLastCharANewLine()) { | ||
if (lastChar != null && lastChar !== " " && !this.isLastCharANewLine()) | ||
this.baseWrite(" "); | ||
} | ||
return this; | ||
}; | ||
CodeBlockWriter.prototype.conditionalWrite = function (condition, str) { | ||
if (condition) { | ||
if (condition) | ||
this.write(str); | ||
} | ||
return this; | ||
@@ -89,5 +86,4 @@ }; | ||
(str || "").split(/\r?\n/).forEach(function (s, i) { | ||
if (i > 0) { | ||
if (i > 0) | ||
_this.newLine(); | ||
} | ||
_this.baseWrite(s); | ||
@@ -98,8 +94,7 @@ }); | ||
this._isAtStartOfBlock = false; | ||
if (str != null && str.length > 0) { | ||
if (str !== this._newLine && this.isLastCharANewLine()) { | ||
this.writeIndentation(); | ||
} | ||
this._text += str; | ||
} | ||
if (str == null || str.length === 0) | ||
return this; | ||
if (str !== this._newLine && this.isLastCharANewLine()) | ||
this.writeIndentation(); | ||
this._text += str; | ||
return this; | ||
@@ -110,11 +105,9 @@ }; | ||
var lastIndexOfConsecutiveNewLines = text.lastIndexOf(consecutiveNewline); | ||
if (lastIndexOfConsecutiveNewLines >= 0 && lastIndexOfConsecutiveNewLines === text.length - consecutiveNewline.length) { | ||
if (lastIndexOfConsecutiveNewLines >= 0 && lastIndexOfConsecutiveNewLines === text.length - consecutiveNewline.length) | ||
text = text.substr(0, text.length - this._newLine.length); | ||
} | ||
return text; | ||
}; | ||
CodeBlockWriter.prototype.removeLastIfNewLine = function () { | ||
if (this.isLastLineBlankLine() && this.isCurrentLineBlank()) { | ||
if (this.isLastLineBlankLine() && this.isCurrentLineBlank()) | ||
this._text = this._text.substr(0, this._text.length - this._newLine.length); | ||
} | ||
}; | ||
@@ -129,19 +122,15 @@ CodeBlockWriter.prototype.isCurrentLineBlank = function () { | ||
var lastNewLineIndex = this._text.lastIndexOf(this._newLine); | ||
if (lastNewLineIndex >= 0) { | ||
if (lastNewLineIndex >= 0) | ||
return this._text.substr(lastNewLineIndex + this._newLine.length); | ||
} | ||
else { | ||
else | ||
return ""; | ||
} | ||
}; | ||
CodeBlockWriter.prototype.getLastLine = function () { | ||
var lastLine; | ||
var lastNewLineIndex = this._text.lastIndexOf(this._newLine); | ||
if (lastNewLineIndex >= 0) { | ||
var secondLastNewLineIndex = this._text.lastIndexOf(this._newLine, lastNewLineIndex - 1); | ||
if (secondLastNewLineIndex >= 0) { | ||
lastLine = this._text.substr(secondLastNewLineIndex, lastNewLineIndex - secondLastNewLineIndex); | ||
} | ||
} | ||
return lastLine; | ||
if (lastNewLineIndex < 0) | ||
return null; | ||
var secondLastNewLineIndex = this._text.lastIndexOf(this._newLine, lastNewLineIndex - 1); | ||
if (secondLastNewLineIndex === -1) | ||
secondLastNewLineIndex = 0; | ||
return this._text.substr(secondLastNewLineIndex, lastNewLineIndex - secondLastNewLineIndex); | ||
}; | ||
@@ -152,15 +141,12 @@ CodeBlockWriter.prototype.isLastCharANewLine = function () { | ||
CodeBlockWriter.prototype.getLastChar = function () { | ||
var lastChar; | ||
if (this._text.length > 0) { | ||
var lastChar = null; | ||
if (this._text.length > 0) | ||
lastChar = this._text[this._text.length - 1]; | ||
} | ||
return lastChar; | ||
}; | ||
CodeBlockWriter.prototype.writeIndentation = function () { | ||
if (this._useTabs) { | ||
if (this._useTabs) | ||
this._text += Array(this._currentIndentation + 1).join("\t"); | ||
} | ||
else { | ||
else | ||
this._text += Array(this._getCurrentIndentationNumberSpaces() + 1).join(" "); | ||
} | ||
}; | ||
@@ -171,3 +157,3 @@ CodeBlockWriter.prototype._getCurrentIndentationNumberSpaces = function () { | ||
return CodeBlockWriter; | ||
})(); | ||
}()); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -174,0 +160,0 @@ exports.default = CodeBlockWriter; |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var assert = require("assert"); | ||
@@ -129,2 +130,22 @@ var code_block_writer_1 = require("./../code-block-writer"); | ||
}); | ||
describe("#blankLine()", function () { | ||
it("should do a blank line if the last text was not a new line", function () { | ||
var expected = "test\n\ntest"; | ||
doTest(expected, function (writer) { | ||
writer.write("test").blankLine().write("test"); | ||
}); | ||
}); | ||
it("should do a blank line if the last text was a newline", function () { | ||
var expected = "test\n\ntest"; | ||
doTest(expected, function (writer) { | ||
writer.writeLine("test").blankLine().write("test"); | ||
}); | ||
}); | ||
it("should not do a blank line if the last was a blank line", function () { | ||
var expected = "test\n\ntest"; | ||
doTest(expected, function (writer) { | ||
writer.writeLine("test").newLine().blankLine().write("test"); | ||
}); | ||
}); | ||
}); | ||
describe("#newLineIfLastNotNewLine()", function () { | ||
@@ -131,0 +152,0 @@ it("should do a newline if the last text was not a newline", function () { |
{ | ||
"name": "code-block-writer", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "A simple code writer that assists with formatting and visualizing blocks of code.", | ||
@@ -41,4 +41,4 @@ "main": "dist/code-block-writer.js", | ||
"tslint": "^3.1.1", | ||
"typescript": "^1.6.2" | ||
"typescript": "^2.1.6" | ||
} | ||
} |
@@ -20,3 +20,8 @@ code-block-writer | ||
const writer = new CodeBlockWriter({ newLine: "\r\n" }); // optional options (newLine defaults to "\n") | ||
const writer = new CodeBlockWriter({ | ||
// optional options | ||
newLine: "\r\n", // default: "\n" | ||
indentNumberOfSpaces: 2, // default: 4 | ||
useTabs: false // default: false | ||
}); | ||
const className = "MyClass"; | ||
@@ -23,0 +28,0 @@ |
@@ -9,3 +9,3 @@ export default class CodeBlockWriter { | ||
constructor(opts: { newLine?: string; indentNumberOfSpaces?: number; useTabs?: boolean; } = null) { | ||
constructor(opts?: { newLine?: string; indentNumberOfSpaces?: number; useTabs?: boolean; }) { | ||
this._newLine = (opts && opts.newLine) || "\n"; | ||
@@ -38,5 +38,4 @@ this._numberSpaces = (opts && opts.indentNumberOfSpaces) || 4; | ||
conditionalWriteLine(condition: boolean, str: string) { | ||
if (condition) { | ||
if (condition) | ||
this.writeLine(str); | ||
} | ||
@@ -55,5 +54,4 @@ return this; | ||
newLineIfLastNotNewLine() { | ||
if (!this.isLastCharANewLine()) { | ||
if (!this.isLastCharANewLine()) | ||
this.newLine(); | ||
} | ||
@@ -63,6 +61,9 @@ return this; | ||
blankLine() { | ||
return this.newLine().newLine(); | ||
} | ||
conditionalNewLine(condition: boolean) { | ||
if (condition) { | ||
if (condition) | ||
this.newLine(); | ||
} | ||
@@ -75,5 +76,4 @@ return this; | ||
if (!willCreateAConsecutiveBlankLine && !this._isAtStartOfBlock && this._text.length !== 0) { | ||
if (!willCreateAConsecutiveBlankLine && !this._isAtStartOfBlock && this._text.length !== 0) | ||
this.baseWrite(this._newLine); | ||
} | ||
@@ -86,5 +86,4 @@ return this; | ||
if (lastChar != null && lastChar !== " " && !this.isLastCharANewLine()) { | ||
if (lastChar != null && lastChar !== " " && !this.isLastCharANewLine()) | ||
this.baseWrite(" "); | ||
} | ||
@@ -95,5 +94,5 @@ return this; | ||
conditionalWrite(condition: boolean, str: string) { | ||
if (condition) { | ||
if (condition) | ||
this.write(str); | ||
} | ||
return this; | ||
@@ -117,5 +116,5 @@ } | ||
(str || "").split(/\r?\n/).forEach((s, i) => { | ||
if (i > 0) { | ||
if (i > 0) | ||
this.newLine(); | ||
} | ||
this.baseWrite(s); | ||
@@ -128,10 +127,10 @@ }); | ||
if (str != null && str.length > 0) { | ||
if (str !== this._newLine && this.isLastCharANewLine()) { | ||
this.writeIndentation(); | ||
} | ||
if (str == null || str.length === 0) | ||
return this; | ||
this._text += str; | ||
} | ||
if (str !== this._newLine && this.isLastCharANewLine()) | ||
this.writeIndentation(); | ||
this._text += str; | ||
return this; | ||
@@ -144,5 +143,4 @@ } | ||
if (lastIndexOfConsecutiveNewLines >= 0 && lastIndexOfConsecutiveNewLines === text.length - consecutiveNewline.length) { | ||
if (lastIndexOfConsecutiveNewLines >= 0 && lastIndexOfConsecutiveNewLines === text.length - consecutiveNewline.length) | ||
text = text.substr(0, text.length - this._newLine.length); | ||
} | ||
@@ -153,5 +151,4 @@ return text; | ||
private removeLastIfNewLine() { | ||
if (this.isLastLineBlankLine() && this.isCurrentLineBlank()) { | ||
if (this.isLastLineBlankLine() && this.isCurrentLineBlank()) | ||
this._text = this._text.substr(0, this._text.length - this._newLine.length); | ||
} | ||
} | ||
@@ -170,23 +167,20 @@ | ||
if (lastNewLineIndex >= 0) { | ||
if (lastNewLineIndex >= 0) | ||
return this._text.substr(lastNewLineIndex + this._newLine.length); | ||
} | ||
else { | ||
else | ||
return ""; | ||
} | ||
} | ||
private getLastLine() { | ||
let lastLine: string; | ||
const lastNewLineIndex = this._text.lastIndexOf(this._newLine); | ||
if (lastNewLineIndex >= 0) { | ||
const secondLastNewLineIndex = this._text.lastIndexOf(this._newLine, lastNewLineIndex - 1); | ||
if (lastNewLineIndex < 0) | ||
return null; | ||
if (secondLastNewLineIndex >= 0) { | ||
lastLine = this._text.substr(secondLastNewLineIndex, lastNewLineIndex - secondLastNewLineIndex); | ||
} | ||
} | ||
let secondLastNewLineIndex = this._text.lastIndexOf(this._newLine, lastNewLineIndex - 1); | ||
return lastLine; | ||
if (secondLastNewLineIndex === -1) | ||
secondLastNewLineIndex = 0; | ||
return this._text.substr(secondLastNewLineIndex, lastNewLineIndex - secondLastNewLineIndex); | ||
} | ||
@@ -199,7 +193,6 @@ | ||
private getLastChar() { | ||
let lastChar: string; | ||
let lastChar: string | null = null; | ||
if (this._text.length > 0) { | ||
if (this._text.length > 0) | ||
lastChar = this._text[this._text.length - 1]; | ||
} | ||
@@ -210,8 +203,6 @@ return lastChar; | ||
private writeIndentation() { | ||
if (this._useTabs) { | ||
if (this._useTabs) | ||
this._text += Array(this._currentIndentation + 1).join("\t"); | ||
} | ||
else { | ||
else | ||
this._text += Array(this._getCurrentIndentationNumberSpaces() + 1).join(" "); | ||
} | ||
} | ||
@@ -218,0 +209,0 @@ |
@@ -54,3 +54,3 @@ import * as assert from "assert"; | ||
doTest(expected, writer => { | ||
writer.write(null); | ||
writer.write(null as any as string); | ||
}); | ||
@@ -186,2 +186,28 @@ }); | ||
describe("#blankLine()", () => { | ||
it("should do a blank line if the last text was not a new line", () => { | ||
const expected = `test\n\ntest`; | ||
doTest(expected, writer => { | ||
writer.write("test").blankLine().write("test"); | ||
}); | ||
}); | ||
it("should do a blank line if the last text was a newline", () => { | ||
const expected = `test\n\ntest`; | ||
doTest(expected, writer => { | ||
writer.writeLine("test").blankLine().write("test"); | ||
}); | ||
}); | ||
it("should not do a blank line if the last was a blank line", () => { | ||
const expected = `test\n\ntest`; | ||
doTest(expected, writer => { | ||
writer.writeLine("test").newLine().blankLine().write("test"); | ||
}); | ||
}); | ||
}); | ||
describe("#newLineIfLastNotNewLine()", () => { | ||
@@ -188,0 +214,0 @@ it("should do a newline if the last text was not a newline", () => { |
@@ -11,4 +11,5 @@ { | ||
"outDir": "dist", | ||
"rootDir": "src" | ||
"rootDir": "src", | ||
"strictNullChecks": true | ||
} | ||
} |
@@ -16,3 +16,3 @@ { | ||
], | ||
"curly": true, | ||
"curly": false, | ||
"eofline": true, | ||
@@ -19,0 +19,0 @@ "forin": true, |
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
142173
3282
63