code-block-writer
Advanced tools
Comparing version 8.1.0 to 8.1.1
@@ -5,2 +5,18 @@ # Change Log | ||
<a name="8.1.1"></a> | ||
## [8.1.1](https://github.com/dsherret/code-block-writer/compare/v8.1.0...v8.1.1) (2019-05-02) | ||
### Bug Fixes | ||
* Restrict `newLine` to "\n" | "\r\n". ([699a41f](https://github.com/dsherret/code-block-writer/commit/699a41f)) | ||
### Performance Improvements | ||
* Calling `#getLength()` does not internally call `#toString()`. ([b19c7ab](https://github.com/dsherret/code-block-writer/commit/b19c7ab)) | ||
* Calling `#isLastBlankLine()` does not internally call `#toString()`. ([d225b67](https://github.com/dsherret/code-block-writer/commit/d225b67)) | ||
<a name="8.1.0"></a> | ||
@@ -7,0 +23,0 @@ # [8.1.0](https://github.com/dsherret/code-block-writer/compare/v8.0.0...v8.1.0) (2019-05-02) |
@@ -0,20 +1,34 @@ | ||
/** | ||
* Options for the writer. | ||
*/ | ||
export interface Options { | ||
newLine: string; | ||
/** | ||
* Newline character. | ||
* @remarks Defaults to \n. | ||
*/ | ||
newLine: "\n" | "\r\n"; | ||
/** | ||
* Number of spaces to indent when `useTabs` is false. | ||
* @remarks Defaults to 4. | ||
*/ | ||
indentNumberOfSpaces: number; | ||
/** | ||
* Whether to use tabs (true) or spaces (false). | ||
* @remarks Defaults to false. | ||
*/ | ||
useTabs: boolean; | ||
/** | ||
* Whether to use a single quote (true) or double quote (false). | ||
* @remarks Defaults to false. | ||
*/ | ||
useSingleQuote: boolean; | ||
} | ||
/** | ||
* Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code. | ||
*/ | ||
export default class CodeBlockWriter { | ||
private readonly _indentationText; | ||
private readonly _newLine; | ||
private readonly _useTabs; | ||
private readonly _quoteChar; | ||
private readonly _indentNumberOfSpaces; | ||
private _currentIndentation; | ||
private _queuedIndentation; | ||
private _texts; | ||
private _newLineOnNextWrite; | ||
private _stringCharStack; | ||
private _isInRegEx; | ||
private _isOnFirstLineOfBlock; | ||
/** | ||
* Constructor. | ||
* @param opts - Options for the writer. | ||
*/ | ||
constructor(opts?: Partial<Options>); | ||
@@ -64,3 +78,3 @@ /** | ||
indentBlock(block: () => void): this; | ||
private _indentBlockInternal(block?); | ||
private _indentBlockInternal; | ||
/** | ||
@@ -201,3 +215,3 @@ * Conditionally writes a line of text. | ||
getLastChar(): string | undefined; | ||
private _getLastCharWithOffset(offset); | ||
private _getLastCharWithOffset; | ||
/** | ||
@@ -208,11 +222,12 @@ * Gets the writer's text. | ||
private static readonly _newLineRegEx; | ||
private _writeIndentingNewLines(text); | ||
private _baseWriteNewline(); | ||
private dequeueQueuedIndentation(); | ||
private _writeIndentingNewLines; | ||
private _baseWriteNewline; | ||
private dequeueQueuedIndentation; | ||
private static readonly _isCharToHandle; | ||
private _updateInternalState(str); | ||
private _writeIndentation(); | ||
private _newLineIfNewLineOnNextWrite(); | ||
private _updateInternalState; | ||
private _writeIndentation; | ||
private _newLineIfNewLineOnNextWrite; | ||
private _internalWrite; | ||
private static readonly _spacesOrTabsRegEx; | ||
private _getIndentationLevelFromArg(countOrText); | ||
private _getIndentationLevelFromArg; | ||
} |
@@ -5,11 +5,26 @@ "use strict"; | ||
const CommentChar_1 = require("./CommentChar"); | ||
/** | ||
* Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code. | ||
*/ | ||
class CodeBlockWriter { | ||
/** | ||
* Constructor. | ||
* @param opts - Options for the writer. | ||
*/ | ||
constructor(opts = {}) { | ||
/** @internal */ | ||
this._currentIndentation = 0; | ||
/** @internal */ | ||
this._texts = []; | ||
/** @internal */ | ||
this._length = 0; | ||
/** @internal */ | ||
this._newLineOnNextWrite = false; | ||
/** @internal */ | ||
this._currentCommentChar = undefined; | ||
/** @internal */ | ||
this._stringCharStack = []; | ||
/** @internal */ | ||
this._isInRegEx = false; | ||
/** @internal */ | ||
this._isOnFirstLineOfBlock = true; | ||
@@ -242,3 +257,3 @@ this._newLine = opts.newLine || "\n"; | ||
getLength() { | ||
return this.toString().length; | ||
return this._length; | ||
} | ||
@@ -281,12 +296,16 @@ /** | ||
let foundCount = 0; | ||
const text = this.toString(); | ||
for (let i = text.length - 1; i >= 0; i--) { | ||
const currentChar = text[i]; | ||
if (currentChar === "\n") { | ||
foundCount++; | ||
if (foundCount === 2) | ||
return true; | ||
// todo: consider extracting out iterating over past characters, but don't use | ||
// an iterator because it will be slow. | ||
for (let i = this._texts.length - 1; i >= 0; i--) { | ||
const currentText = this._texts[i]; | ||
for (let j = currentText.length - 1; j >= 0; j--) { | ||
const currentChar = currentText[j]; | ||
if (currentChar === "\n") { | ||
foundCount++; | ||
if (foundCount === 2) | ||
return true; | ||
} | ||
else if (currentChar !== "\r") | ||
return false; | ||
} | ||
else if (currentChar !== "\r") | ||
return false; | ||
} | ||
@@ -356,3 +375,3 @@ return false; | ||
writer._updateInternalState(s); | ||
writer._texts.push(s); | ||
writer._internalWrite(s); | ||
writer.dequeueQueuedIndentation(); | ||
@@ -364,3 +383,3 @@ } | ||
this._currentCommentChar = undefined; | ||
this._texts.push(this._newLine); | ||
this._internalWrite(this._newLine); | ||
this._isOnFirstLineOfBlock = false; | ||
@@ -420,7 +439,7 @@ this.dequeueQueuedIndentation(); | ||
const flooredIndentation = Math.floor(this._currentIndentation); | ||
this._texts.push(this._indentationText.repeat(flooredIndentation)); | ||
this._internalWrite(this._indentationText.repeat(flooredIndentation)); | ||
const overflow = this._currentIndentation - flooredIndentation; | ||
if (this._useTabs) { | ||
if (overflow > 0.5) | ||
this._texts.push(this._indentationText); | ||
this._internalWrite(this._indentationText); | ||
} | ||
@@ -433,3 +452,3 @@ else { | ||
text += this._indentationText[i]; | ||
this._texts.push(text); | ||
this._internalWrite(text); | ||
} | ||
@@ -443,2 +462,6 @@ } | ||
} | ||
_internalWrite(text) { | ||
this._texts.push(text); | ||
this._length += text.length; | ||
} | ||
_getIndentationLevelFromArg(countOrText) { | ||
@@ -445,0 +468,0 @@ if (typeof countOrText === "number") { |
{ | ||
"name": "code-block-writer", | ||
"version": "8.1.0", | ||
"version": "8.1.1", | ||
"description": "A simple code writer that assists with formatting and visualizing blocks of code.", | ||
@@ -47,12 +47,12 @@ "main": "dist/code-block-writer.js", | ||
"devDependencies": { | ||
"@types/mocha": "^2.2.44", | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^8.0.53", | ||
"coveralls": "^2.13.1", | ||
"mocha": "^3.3.0", | ||
"nyc": "^11.6.0", | ||
"mocha": "^5.2.0", | ||
"nyc": "^14.1.0", | ||
"source-map-support": "^0.5.4", | ||
"ts-node": "^5.0.1", | ||
"tslint": "^5.6.0", | ||
"typescript": "^2.8.1" | ||
"typescript": "^3.4.5" | ||
} | ||
} |
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
767
99387