code-block-writer
Advanced tools
Comparing version 6.1.1 to 6.2.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="6.2.0"></a> | ||
# [6.2.0](https://github.com/dsherret/code-block-writer/compare/v6.1.1...v6.2.0) (2017-12-05) | ||
### Features | ||
* [#14](https://github.com/dsherret/code-block-writer/issues/14) - Add .quote() and .quote(text: string) ([efe2280](https://github.com/dsherret/code-block-writer/commit/efe2280)) | ||
<a name="6.1.1"></a> | ||
@@ -7,0 +17,0 @@ ## [6.1.1](https://github.com/dsherret/code-block-writer/compare/v6.1.0...v6.1.1) (2017-12-02) |
@@ -5,2 +5,3 @@ export default class CodeBlockWriter { | ||
private readonly _useTabs; | ||
private readonly _quoteChar; | ||
private readonly _indentNumberOfSpaces; | ||
@@ -16,2 +17,3 @@ private _currentIndentation; | ||
useTabs?: boolean; | ||
useSingleQuote?: boolean; | ||
}); | ||
@@ -29,5 +31,7 @@ setIndentationLevel(indentationLevel: number): this; | ||
newLine(): this; | ||
quote(): this; | ||
quote(text: string): this; | ||
spaceIfLastNotSpace(): this; | ||
conditionalWrite(condition: boolean | undefined, str: string): this; | ||
write(str: string): this; | ||
conditionalWrite(condition: boolean | undefined, text: string): this; | ||
write(text: string): this; | ||
getLength(): number; | ||
@@ -37,3 +41,3 @@ isInComment(): boolean; | ||
toString(): string; | ||
private writeIndentingNewLines(str); | ||
private writeIndentingNewLines(text); | ||
private baseWriteNewline(); | ||
@@ -40,0 +44,0 @@ private updateStringStack(str); |
@@ -15,2 +15,3 @@ "use strict"; | ||
this._indentationText = getIndentationText(this._useTabs, this._indentNumberOfSpaces); | ||
this._quoteChar = (opts && opts.useSingleQuote) ? "'" : "\""; | ||
} | ||
@@ -90,2 +91,7 @@ CodeBlockWriter.prototype.setIndentationLevel = function (countOrText) { | ||
}; | ||
CodeBlockWriter.prototype.quote = function (text) { | ||
this.newLineIfNewLineOnNextWrite(); | ||
this.writeIndentingNewLines(text == null ? this._quoteChar : this._quoteChar + text + this._quoteChar); | ||
return this; | ||
}; | ||
CodeBlockWriter.prototype.spaceIfLastNotSpace = function () { | ||
@@ -98,10 +104,10 @@ this.newLineIfNewLineOnNextWrite(); | ||
}; | ||
CodeBlockWriter.prototype.conditionalWrite = function (condition, str) { | ||
CodeBlockWriter.prototype.conditionalWrite = function (condition, text) { | ||
if (condition) | ||
this.write(str); | ||
this.write(text); | ||
return this; | ||
}; | ||
CodeBlockWriter.prototype.write = function (str) { | ||
CodeBlockWriter.prototype.write = function (text) { | ||
this.newLineIfNewLineOnNextWrite(); | ||
this.writeIndentingNewLines(str); | ||
this.writeIndentingNewLines(text); | ||
return this; | ||
@@ -121,5 +127,5 @@ }; | ||
}; | ||
CodeBlockWriter.prototype.writeIndentingNewLines = function (str) { | ||
CodeBlockWriter.prototype.writeIndentingNewLines = function (text) { | ||
var _this = this; | ||
var items = (str || "").split(/\r?\n/); | ||
var items = (text || "").split(/\r?\n/); | ||
items.forEach(function (s, i) { | ||
@@ -126,0 +132,0 @@ if (i > 0) |
@@ -21,7 +21,12 @@ "use strict"; | ||
function runTestsForNewLineChar(opts) { | ||
function getWriter() { | ||
return new code_block_writer_1.default(opts); | ||
function getWriter(additionalOpts) { | ||
if (additionalOpts === void 0) { additionalOpts = {}; } | ||
return new code_block_writer_1.default({ | ||
newLine: opts.newLine, | ||
useSingleQuote: additionalOpts.useSingleQuote | ||
}); | ||
} | ||
function doTest(expected, writerCallback) { | ||
var writer = getWriter(); | ||
function doTest(expected, writerCallback, additionalOpts) { | ||
if (additionalOpts === void 0) { additionalOpts = {}; } | ||
var writer = getWriter(additionalOpts); | ||
writerCallback(writer); | ||
@@ -281,2 +286,22 @@ assert.equal(writer.toString(), expected.replace(/\r?\n/g, opts.newLine)); | ||
}); | ||
describe("#quote()", function () { | ||
it("should write out a double quote character", function () { | ||
var expected = "\""; | ||
doTest(expected, function (writer) { | ||
writer.quote(); | ||
}); | ||
}); | ||
it("should write out a single quote character", function () { | ||
var expected = "'"; | ||
doTest(expected, function (writer) { | ||
writer.quote(); | ||
}, { useSingleQuote: true }); | ||
}); | ||
it("should write out text surrounded by quotes", function () { | ||
var expected = "\"test\""; | ||
doTest(expected, function (writer) { | ||
writer.quote("test"); | ||
}); | ||
}); | ||
}); | ||
describe("#spaceIfLastNotSpace()", function () { | ||
@@ -283,0 +308,0 @@ it("should do a space at the beginning of the file", function () { |
{ | ||
"name": "code-block-writer", | ||
"version": "6.1.1", | ||
"version": "6.2.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", |
@@ -24,3 +24,4 @@ code-block-writer | ||
indentNumberOfSpaces: 2, // default: 4 | ||
useTabs: false // default: false | ||
useTabs: false, // default: false | ||
useSingleQuote: true // default: false | ||
}); | ||
@@ -30,5 +31,5 @@ const className = "MyClass"; | ||
writer.write(`class ${className} extends OtherClass`).block(() => { | ||
writer.writeLine(`@MyDecorator("myArgument1", "myArgument2")`); | ||
writer.writeLine(`@MyDecorator(1, 2)`); | ||
writer.write(`myMethod(myParam: any)`).block(() => { | ||
writer.write(`return this.post("myArgument");`); | ||
writer.write("return this.post(").quote("myArgument").write(");"); | ||
}); | ||
@@ -44,5 +45,5 @@ }); | ||
class MyClass extends OtherClass { | ||
@MyDecorator("myArgument1", "myArgument2") | ||
@MyDecorator(1, 2) | ||
myMethod(myParam: any) { | ||
return this.post("myArgument"); | ||
return this.post('myArgument'); | ||
} | ||
@@ -61,2 +62,4 @@ } | ||
* `blankLine()` - Writes a blank line. Does not allow consecutive blank lines. | ||
* `quote()` - Writes a quote character. | ||
* `quote(text: string)` - Writes text surrounded in quotes. | ||
* `indent()` - Indents the current line. | ||
@@ -63,0 +66,0 @@ * `spaceIfLastNotSpace()` - Writes a space if the last was not a space. |
@@ -7,2 +7,3 @@ import {CommentChar} from "./CommentChar"; | ||
private readonly _useTabs: boolean; | ||
private readonly _quoteChar: string; | ||
private readonly _indentNumberOfSpaces: number; | ||
@@ -15,3 +16,3 @@ private _currentIndentation = 0; | ||
constructor(opts?: { newLine?: string; indentNumberOfSpaces?: number; useTabs?: boolean; }) { | ||
constructor(opts?: { newLine?: string; indentNumberOfSpaces?: number; useTabs?: boolean; useSingleQuote?: boolean; }) { | ||
this._newLine = (opts && opts.newLine) || "\n"; | ||
@@ -21,2 +22,3 @@ this._useTabs = (opts && opts.useTabs) || false; | ||
this._indentationText = getIndentationText(this._useTabs, this._indentNumberOfSpaces); | ||
this._quoteChar = (opts && opts.useSingleQuote) ? "'" : `"`; | ||
} | ||
@@ -159,2 +161,17 @@ | ||
/** | ||
* Writes a quote character. | ||
*/ | ||
quote(): this; | ||
/** | ||
* Writes text surrounded in quotes. | ||
* @param text - Text to write. | ||
*/ | ||
quote(text: string): this; | ||
quote(text?: string) { | ||
this.newLineIfNewLineOnNextWrite(); | ||
this.writeIndentingNewLines(text == null ? this._quoteChar : this._quoteChar + text + this._quoteChar); | ||
return this; | ||
} | ||
/** | ||
* Writes a space if the last character was not a space. | ||
@@ -175,7 +192,7 @@ */ | ||
* @param condition - Condition to evaluate. | ||
* @param str - Text to write. | ||
* @param text - Text to write. | ||
*/ | ||
conditionalWrite(condition: boolean | undefined, str: string) { | ||
conditionalWrite(condition: boolean | undefined, text: string) { | ||
if (condition) | ||
this.write(str); | ||
this.write(text); | ||
@@ -187,7 +204,7 @@ return this; | ||
* Writes the provided text. | ||
* @param str - Text to write. | ||
* @param text - Text to write. | ||
*/ | ||
write(str: string) { | ||
write(text: string) { | ||
this.newLineIfNewLineOnNextWrite(); | ||
this.writeIndentingNewLines(str); | ||
this.writeIndentingNewLines(text); | ||
return this; | ||
@@ -224,4 +241,4 @@ } | ||
private writeIndentingNewLines(str: string) { | ||
const items = (str || "").split(/\r?\n/); | ||
private writeIndentingNewLines(text: string) { | ||
const items = (text || "").split(/\r?\n/); | ||
items.forEach((s, i) => { | ||
@@ -228,0 +245,0 @@ if (i > 0) |
@@ -23,8 +23,11 @@ import * as assert from "assert"; | ||
function runTestsForNewLineChar(opts: { newLine: string }) { | ||
function getWriter() { | ||
return new CodeBlockWriter(opts); | ||
function getWriter(additionalOpts: { useSingleQuote?: boolean; } = {}) { | ||
return new CodeBlockWriter({ | ||
newLine: opts.newLine, | ||
useSingleQuote: additionalOpts.useSingleQuote | ||
}); | ||
} | ||
function doTest(expected: string, writerCallback: (writer: CodeBlockWriter) => void) { | ||
const writer = getWriter(); | ||
function doTest(expected: string, writerCallback: (writer: CodeBlockWriter) => void, additionalOpts: { useSingleQuote?: boolean; } = {}) { | ||
const writer = getWriter(additionalOpts); | ||
writerCallback(writer); | ||
@@ -382,2 +385,25 @@ assert.equal(writer.toString(), expected.replace(/\r?\n/g, opts.newLine)); | ||
describe("#quote()", () => { | ||
it("should write out a double quote character", () => { | ||
const expected = `"`; | ||
doTest(expected, writer => { | ||
writer.quote(); | ||
}); | ||
}); | ||
it("should write out a single quote character", () => { | ||
const expected = `'`; | ||
doTest(expected, writer => { | ||
writer.quote(); | ||
}, { useSingleQuote: true }); | ||
}); | ||
it("should write out text surrounded by quotes", () => { | ||
const expected = `"test"`; | ||
doTest(expected, writer => { | ||
writer.quote("test"); | ||
}); | ||
}); | ||
}); | ||
describe("#spaceIfLastNotSpace()", () => { | ||
@@ -384,0 +410,0 @@ it("should do a space at the beginning of the file", () => { |
@@ -6,3 +6,2 @@ { | ||
"parameters", | ||
"arguments", | ||
"statements" | ||
@@ -9,0 +8,0 @@ ], |
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
82132
1875
71