code-block-writer
Advanced tools
Comparing version 1.5.1 to 1.5.2
@@ -8,3 +8,3 @@ var CodeBlockWriter = (function () { | ||
this._isAtStartOfBlock = false; | ||
this._newline = (opts && opts.newLine) || "\n"; | ||
this._newLine = (opts && opts.newLine) || "\n"; | ||
} | ||
@@ -28,3 +28,3 @@ CodeBlockWriter.prototype.block = function (block) { | ||
CodeBlockWriter.prototype.newLineIfLastCharNotNewLine = function () { | ||
if (this.getLastChar() !== this._newline) { | ||
if (!this.isLastCharANewLine()) { | ||
this.newLine(); | ||
@@ -37,3 +37,3 @@ } | ||
if (!willCreateAConsecutiveBlankLine && !this._isAtStartOfBlock && this._text.length !== 0) { | ||
this.write(this._newline); | ||
this.write(this._newLine); | ||
} | ||
@@ -44,3 +44,3 @@ return this; | ||
var lastChar = this.getLastChar(); | ||
if (lastChar != null && lastChar !== " " && lastChar !== this._newline) { | ||
if (lastChar != null && lastChar !== " " && !this.isLastCharANewLine()) { | ||
this.write(" "); | ||
@@ -53,3 +53,3 @@ } | ||
if (str != null && str.length > 0) { | ||
if (str !== this._newline && this.getLastChar() === this._newline) { | ||
if (str !== this._newLine && this.isLastCharANewLine()) { | ||
this.writeIndentation(); | ||
@@ -71,8 +71,8 @@ } | ||
CodeBlockWriter.prototype.isLastLineBlankLine = function () { | ||
return this.getLastLine() === this._newline; | ||
return this.getLastLine() === this._newLine; | ||
}; | ||
CodeBlockWriter.prototype.getCurrentLine = function () { | ||
var lastNewLineIndex = this._text.lastIndexOf(this._newline); | ||
var lastNewLineIndex = this._text.lastIndexOf(this._newLine); | ||
if (lastNewLineIndex >= 0) { | ||
return this._text.substr(lastNewLineIndex + 1); | ||
return this._text.substr(lastNewLineIndex + this._newLine.length); | ||
} | ||
@@ -85,7 +85,7 @@ else { | ||
var lastLine; | ||
var lastNewLineIndex = this._text.lastIndexOf(this._newline); | ||
var lastNewLineIndex = this._text.lastIndexOf(this._newLine); | ||
if (lastNewLineIndex >= 0) { | ||
var secondLastNewLineIndex = this._text.lastIndexOf(this._newline, lastNewLineIndex - 1); | ||
var secondLastNewLineIndex = this._text.lastIndexOf(this._newLine, lastNewLineIndex - 1); | ||
if (secondLastNewLineIndex >= 0) { | ||
return this._text.substr(secondLastNewLineIndex, lastNewLineIndex - secondLastNewLineIndex); | ||
lastLine = this._text.substr(secondLastNewLineIndex, lastNewLineIndex - secondLastNewLineIndex); | ||
} | ||
@@ -95,2 +95,5 @@ } | ||
}; | ||
CodeBlockWriter.prototype.isLastCharANewLine = function () { | ||
return this._text.indexOf(this._newLine, this._text.length - this._newLine.length) !== -1; | ||
}; | ||
CodeBlockWriter.prototype.getLastChar = function () { | ||
@@ -97,0 +100,0 @@ var lastChar; |
var assert = require("assert"); | ||
var code_block_writer_1 = require("./../code-block-writer"); | ||
function getWriter() { | ||
return new code_block_writer_1.default(); | ||
} | ||
function doTest(expected, writerCallback) { | ||
var writer = getWriter(); | ||
writerCallback(writer); | ||
assert.equal(writer.toString(), expected); | ||
} | ||
describe("CodeBlockWriter", function () { | ||
describe("default opts", function () { | ||
it("should use a \n newline if none is specified", function () { | ||
var writer = new code_block_writer_1.default(); | ||
writer.writeLine("test"); | ||
assert.equal(writer.toString(), "test\n"); | ||
}); | ||
}); | ||
describe("tests for \n", function () { | ||
runTestsForNewLineChar({ newLine: "\n" }); | ||
}); | ||
describe("tests for \r\n", function () { | ||
runTestsForNewLineChar({ newLine: "\r\n" }); | ||
}); | ||
}); | ||
function runTestsForNewLineChar(opts) { | ||
function getWriter() { | ||
return new code_block_writer_1.default(opts); | ||
} | ||
function doTest(expected, writerCallback) { | ||
var writer = getWriter(); | ||
writerCallback(writer); | ||
assert.equal(writer.toString(), expected.replace(/\r?\n/g, opts.newLine)); | ||
} | ||
describe("write", function () { | ||
@@ -19,2 +34,8 @@ it("should write the text", function () { | ||
}); | ||
it("should do nothing if providing a null string", function () { | ||
var expected = ""; | ||
doTest(expected, function (writer) { | ||
writer.write(null); | ||
}); | ||
}); | ||
}); | ||
@@ -104,2 +125,8 @@ describe("block()", function () { | ||
}); | ||
it("should not do a newline at the start", function () { | ||
var expected = ""; | ||
doTest(expected, function (writer) { | ||
writer.newLine(); | ||
}); | ||
}); | ||
it("should not do a newline after doing a block", function () { | ||
@@ -153,4 +180,4 @@ var expected = "test {\n test\n}\n"; | ||
}); | ||
}); | ||
} | ||
//# sourceMappingURL=code-block-writer-tests.js.map |
var gulp = require("gulp"); | ||
var del = require("del"); | ||
var mocha = require("gulp-mocha"); | ||
var istanbul = require("gulp-istanbul"); | ||
var ts = require("gulp-typescript"); | ||
@@ -21,5 +22,13 @@ var tslint = require("gulp-tslint"); | ||
gulp.task("test", ["typescript"], function() { | ||
gulp.task("pre-test", ["typescript"], function () { | ||
return gulp.src(["dist/**/*.js", "!dist/tests/**/*.js"]) | ||
.pipe(istanbul()) | ||
.pipe(istanbul.hookRequire()); | ||
}); | ||
gulp.task("test", ["pre-test"], function() { | ||
return gulp.src("dist/tests/**/*.js") | ||
.pipe(mocha({ reporter: "progress" })); | ||
.pipe(mocha({ reporter: "progress" })) | ||
.pipe(istanbul.writeReports()) | ||
.pipe(istanbul.enforceThresholds({ thresholds: { global: 90 } })); | ||
}); | ||
@@ -26,0 +35,0 @@ |
{ | ||
"name": "code-block-writer", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "A simple code writer that assists with formatting and visualizing blocks of code.", | ||
"main": "dist/code-block-writer.js", | ||
"scripts": { | ||
"test": "gulp test", | ||
"test": "gulp test && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"build": "gulp typescript" | ||
@@ -28,8 +28,8 @@ }, | ||
}, | ||
"dependencies": { | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"typescript": "^1.6.2", | ||
"coveralls": "^2.11.6", | ||
"del": "^2.0.2", | ||
"gulp": "^3.9.0", | ||
"gulp-istanbul": "^0.10.3", | ||
"gulp-mocha": "^2.2.0", | ||
@@ -40,4 +40,5 @@ "gulp-sourcemaps": "^1.6.0", | ||
"mocha": "^2.3.3", | ||
"tslint": "^3.1.1" | ||
"tslint": "^3.1.1", | ||
"typescript": "^1.6.2" | ||
} | ||
} |
@@ -5,2 +5,3 @@ code-block-writer | ||
[![Build Status](https://travis-ci.org/dsherret/code-block-writer.svg)](https://travis-ci.org/dsherret/code-block-writer) | ||
[![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) | ||
[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) | ||
@@ -7,0 +8,0 @@ |
@@ -5,7 +5,7 @@ export default class CodeBlockWriter { | ||
private _numberSpaces = 4; | ||
private _newline: string; | ||
private _newLine: string; | ||
private _isAtStartOfBlock = false; | ||
constructor(opts: { newLine: string } = null) { | ||
this._newline = (opts && opts.newLine) || "\n"; | ||
this._newLine = (opts && opts.newLine) || "\n"; | ||
} | ||
@@ -34,3 +34,3 @@ | ||
newLineIfLastCharNotNewLine() { | ||
if (this.getLastChar() !== this._newline) { | ||
if (!this.isLastCharANewLine()) { | ||
this.newLine(); | ||
@@ -44,4 +44,5 @@ } | ||
const willCreateAConsecutiveBlankLine = this.isLastLineBlankLine() && this.isCurrentLineBlank(); | ||
if (!willCreateAConsecutiveBlankLine && !this._isAtStartOfBlock && this._text.length !== 0) { | ||
this.write(this._newline); | ||
this.write(this._newLine); | ||
} | ||
@@ -55,3 +56,3 @@ | ||
if (lastChar != null && lastChar !== " " && lastChar !== this._newline) { | ||
if (lastChar != null && lastChar !== " " && !this.isLastCharANewLine()) { | ||
this.write(" "); | ||
@@ -67,3 +68,3 @@ } | ||
if (str != null && str.length > 0) { | ||
if (str !== this._newline && this.getLastChar() === this._newline) { | ||
if (str !== this._newLine && this.isLastCharANewLine()) { | ||
this.writeIndentation(); | ||
@@ -91,10 +92,10 @@ } | ||
private isLastLineBlankLine() { | ||
return this.getLastLine() === this._newline; | ||
return this.getLastLine() === this._newLine; | ||
} | ||
private getCurrentLine() { | ||
const lastNewLineIndex = this._text.lastIndexOf(this._newline); | ||
const lastNewLineIndex = this._text.lastIndexOf(this._newLine); | ||
if (lastNewLineIndex >= 0) { | ||
return this._text.substr(lastNewLineIndex + 1); | ||
return this._text.substr(lastNewLineIndex + this._newLine.length); | ||
} | ||
@@ -108,9 +109,9 @@ else { | ||
let lastLine: string; | ||
const lastNewLineIndex = this._text.lastIndexOf(this._newline); | ||
const lastNewLineIndex = this._text.lastIndexOf(this._newLine); | ||
if (lastNewLineIndex >= 0) { | ||
const secondLastNewLineIndex = this._text.lastIndexOf(this._newline, lastNewLineIndex - 1); | ||
const secondLastNewLineIndex = this._text.lastIndexOf(this._newLine, lastNewLineIndex - 1); | ||
if (secondLastNewLineIndex >= 0) { | ||
return this._text.substr(secondLastNewLineIndex, lastNewLineIndex - secondLastNewLineIndex); | ||
lastLine = this._text.substr(secondLastNewLineIndex, lastNewLineIndex - secondLastNewLineIndex); | ||
} | ||
@@ -122,2 +123,6 @@ } | ||
private isLastCharANewLine() { | ||
return this._text.indexOf(this._newLine, this._text.length - this._newLine.length) !== -1; | ||
} | ||
private getLastChar() { | ||
@@ -124,0 +129,0 @@ let lastChar: string; |
import * as assert from "assert"; | ||
import CodeBlockWriter from "./../code-block-writer"; | ||
function getWriter() { | ||
return new CodeBlockWriter(); | ||
} | ||
describe("CodeBlockWriter", () => { | ||
describe("default opts", () => { | ||
it("should use a \n newline if none is specified", () => { | ||
const writer = new CodeBlockWriter(); | ||
writer.writeLine("test"); | ||
assert.equal(writer.toString(), "test\n"); | ||
}); | ||
}); | ||
function doTest(expected: string, writerCallback: (writer: CodeBlockWriter) => void) { | ||
const writer = getWriter(); | ||
writerCallback(writer); | ||
assert.equal(writer.toString(), expected); | ||
} | ||
describe("tests for \n", () => { | ||
runTestsForNewLineChar({ newLine: "\n" }); | ||
}); | ||
describe("CodeBlockWriter", () => { | ||
describe("tests for \r\n", () => { | ||
runTestsForNewLineChar({ newLine: "\r\n" }); | ||
}); | ||
}); | ||
function runTestsForNewLineChar(opts: { newLine: string }) { | ||
function getWriter() { | ||
return new CodeBlockWriter(opts); | ||
} | ||
function doTest(expected: string, writerCallback: (writer: CodeBlockWriter) => void) { | ||
const writer = getWriter(); | ||
writerCallback(writer); | ||
assert.equal(writer.toString(), expected.replace(/\r?\n/g, opts.newLine)); | ||
} | ||
describe("write", () => { | ||
@@ -23,2 +41,10 @@ it("should write the text", () => { | ||
}); | ||
it("should do nothing if providing a null string", () => { | ||
const expected = ""; | ||
doTest(expected, writer => { | ||
writer.write(null); | ||
}); | ||
}); | ||
}); | ||
@@ -148,2 +174,10 @@ | ||
it("should not do a newline at the start", () => { | ||
const expected = ``; | ||
doTest(expected, writer => { | ||
writer.newLine(); | ||
}); | ||
}); | ||
it("should not do a newline after doing a block", () => { | ||
@@ -209,2 +243,2 @@ const expected = `test {\n test\n}\n`; | ||
}); | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
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
126145
2917
37
11