code-block-writer
Advanced tools
Comparing version 6.7.0 to 6.7.1
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="6.7.1"></a> | ||
## [6.7.1](https://github.com/dsherret/code-block-writer/compare/v6.7.0...v6.7.1) (2018-03-27) | ||
### Bug Fixes | ||
* Writing within quote should escape string chars. ([bd9ddaa](https://github.com/dsherret/code-block-writer/commit/bd9ddaa)) | ||
<a name="6.7.0"></a> | ||
@@ -7,0 +17,0 @@ # [6.7.0](https://github.com/dsherret/code-block-writer/compare/v6.6.0...v6.7.0) (2018-03-25) |
@@ -137,3 +137,3 @@ "use strict"; | ||
this._newLineIfNewLineOnNextWrite(); | ||
this._writeIndentingNewLines(text == null ? this._quoteChar : this._quoteChar + text + this._quoteChar); | ||
this._writeIndentingNewLines(text == null ? this._quoteChar : this._quoteChar + stringUtils_1.escapeForWithinString(text, this._quoteChar) + this._quoteChar); | ||
return this; | ||
@@ -140,0 +140,0 @@ }; |
@@ -22,1 +22,17 @@ "use strict"; | ||
exports.es5StringRepeat = es5StringRepeat; | ||
function escapeForWithinString(str, quoteKind) { | ||
return escapeChar(str, quoteKind).replace(/(\r?\n)/g, "\\$1"); | ||
} | ||
exports.escapeForWithinString = escapeForWithinString; | ||
function escapeChar(str, char) { | ||
if (char.length !== 1) | ||
throw new Error("Specified char must be one character long."); | ||
var result = ""; | ||
for (var i = 0; i < str.length; i++) { | ||
if (str[i] === char) | ||
result += "\\"; | ||
result += str[i]; | ||
} | ||
return result; | ||
} | ||
exports.escapeChar = escapeChar; |
{ | ||
"name": "code-block-writer", | ||
"version": "6.7.0", | ||
"version": "6.7.1", | ||
"description": "A simple code writer that assists with formatting and visualizing blocks of code.", | ||
@@ -5,0 +5,0 @@ "main": "dist/code-block-writer.js", |
@@ -1,2 +0,2 @@ | ||
import {stringRepeat} from "./utils/stringUtils"; | ||
import {stringRepeat, escapeForWithinString} from "./utils/stringUtils"; | ||
import {CommentChar} from "./CommentChar"; | ||
@@ -197,3 +197,3 @@ | ||
this._newLineIfNewLineOnNextWrite(); | ||
this._writeIndentingNewLines(text == null ? this._quoteChar : this._quoteChar + text + this._quoteChar); | ||
this._writeIndentingNewLines(text == null ? this._quoteChar : this._quoteChar + escapeForWithinString(text, this._quoteChar) + this._quoteChar); | ||
return this; | ||
@@ -200,0 +200,0 @@ } |
@@ -505,2 +505,9 @@ import * as assert from "assert"; | ||
}); | ||
it("should write out text surrounded by quotes and escape quotes and new lines", () => { | ||
const expected = `"te\\"\\\r\nst"`; | ||
doTest(expected, writer => { | ||
writer.quote("te\"\r\nst"); | ||
}); | ||
}); | ||
}); | ||
@@ -507,0 +514,0 @@ |
import * as assert from "assert"; | ||
import {es5StringRepeat, stringRepeat} from "../../utils/stringUtils"; | ||
import {es5StringRepeat, stringRepeat, escapeChar, escapeForWithinString} from "../../utils/stringUtils"; | ||
function doTests(func: (str: string, times: number) => string) { | ||
it("should repeat the string", () => { | ||
assert.equal(func(" ", 2), " "); | ||
describe("string repeat", () => { | ||
function doTests(func: (str: string, times: number) => string) { | ||
it("should repeat the string", () => { | ||
assert.equal(func(" ", 2), " "); | ||
}); | ||
it("should not repeat the string if specifying 0", () => { | ||
assert.equal(func(" ", 0), ""); | ||
}); | ||
it("should throw if specifying a negative number", () => { | ||
assert.throws(() => func(" ", -1)); | ||
}); | ||
} | ||
describe("es5StringRepeat", () => { | ||
doTests(es5StringRepeat); | ||
}); | ||
it("should not repeat the string if specifying 0", () => { | ||
assert.equal(func(" ", 0), ""); | ||
describe("stringRepeat", () => { | ||
doTests(stringRepeat); | ||
}); | ||
}); | ||
it("should throw if specifying a negative number", () => { | ||
assert.throws(() => func(" ", -1)); | ||
describe("escapeForWithinString", () => { | ||
function doTest(input: string, expected: string) { | ||
assert.equal(escapeForWithinString(input, "\""), expected); | ||
} | ||
it("should escape the quotes and newline", () => { | ||
doTest(`"testing\n this out"`, `\\"testing\\\n this out\\"`); | ||
}); | ||
} | ||
describe("es5StringRepeat", () => { | ||
doTests(es5StringRepeat); | ||
}); | ||
describe("stringRepeat", () => { | ||
doTests(stringRepeat); | ||
describe("escapeChar", () => { | ||
function doTest(input: string, char: string, expected: string) { | ||
assert.equal(escapeChar(input, char), expected); | ||
} | ||
it("should throw when specifying a char length > 1", () => { | ||
assert.throws(() => escapeChar("", "ab")); | ||
}); | ||
it("should throw when specifying a char length < 1", () => { | ||
assert.throws(() => escapeChar("", "")); | ||
}); | ||
it("should escape the single quotes when specified", () => { | ||
doTest(`'testing "this" out'`, `'`, `\\'testing "this" out\\'`); | ||
}); | ||
it("should escape regardless of if the character is already escaped", () => { | ||
doTest(`"testing \\"this\\" out"`, `"`, `\\"testing \\\\"this\\\\" out\\"`); | ||
}); | ||
}); |
@@ -20,1 +20,18 @@ /** @internal */ | ||
} | ||
export function escapeForWithinString(str: string, quoteKind: string) { | ||
return escapeChar(str, quoteKind).replace(/(\r?\n)/g, "\\$1"); | ||
} | ||
export function escapeChar(str: string, char: string) { | ||
if (char.length !== 1) | ||
throw new Error(`Specified char must be one character long.`); | ||
let result = ""; | ||
for (let i = 0; i < str.length; i++) { | ||
if (str[i] === char) | ||
result += "\\"; | ||
result += str[i]; | ||
} | ||
return result; | ||
} |
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
82312
1925