code-block-writer
Advanced tools
Comparing version 8.1.2 to 8.2.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="8.2.0"></a> | ||
# [8.2.0](https://github.com/dsherret/code-block-writer/compare/v8.1.2...v8.2.0) (2019-05-05) | ||
### Features | ||
* Add `#withQueuedIndentationLevel` and `#withIndentationLevel`. ([1bcf7c8](https://github.com/dsherret/code-block-writer/commit/1bcf7c8)) | ||
<a name="8.1.2"></a> | ||
@@ -7,0 +17,0 @@ ## [8.1.2](https://github.com/dsherret/code-block-writer/compare/v8.1.1...v8.1.2) (2019-05-02) |
@@ -50,2 +50,16 @@ /** | ||
/** | ||
* Queues the indentation level for the next lines written within the provided action | ||
* and restores the writer's indentation state after afterwards. | ||
* @param indentationLevel - Indentation level to queue. | ||
* @param action - Action to perform with the queued indentation. | ||
*/ | ||
withQueuedIndentationLevel(indentationLevel: number, action: () => void): this; | ||
/** | ||
* Queues the indentation level for the next lines written using the provided indentation | ||
* text within the provided action and restores the writer's indentation state afterwards. | ||
* @param whitespaceText - Gets the indentation level from the indentation text. | ||
* @param action - Action to perform with the queued indentation. | ||
*/ | ||
withQueuedIndentationLevel(whitespaceText: string, action: () => void): this; | ||
/** | ||
* Sets the current indentation level. | ||
@@ -61,2 +75,17 @@ * @param indentationLevel - Indentation level to be at. | ||
/** | ||
* Sets the indentation level within the provided action and restores the writer's indentation | ||
* state afterwards. | ||
* @remarks Restores the writer's state after the action. | ||
* @param indentationLevel - Indentation level to queue. | ||
* @param action - Action to perform with the indentation. | ||
*/ | ||
withIdentationLevel(indentationLevel: number, action: () => void): this; | ||
/** | ||
* Sets the identation level with the provided indentation text within the provided action | ||
* and restores the writer's indentation state afterwards. | ||
* @param whitespaceText - Gets the indentation level from the indentation text. | ||
* @param action - Action to perform with the indentation. | ||
*/ | ||
withIdentationLevel(whitespaceText: string, action: () => void): this; | ||
/** | ||
* Gets the current indentation level. | ||
@@ -63,0 +92,0 @@ */ |
@@ -17,4 +17,2 @@ "use strict"; | ||
/** @internal */ | ||
this._texts = []; | ||
/** @internal */ | ||
this._length = 0; | ||
@@ -31,2 +29,6 @@ /** @internal */ | ||
this._isOnFirstLineOfBlock = true; | ||
// An array of strings is used rather than a single string because it was | ||
// found to be ~11x faster when printing a 10K line file (~11s to ~1s). | ||
/** @internal */ | ||
this._texts = []; | ||
this._newLine = opts.newLine || "\n"; | ||
@@ -53,2 +55,13 @@ this._useTabs = opts.useTabs || false; | ||
} | ||
withQueuedIndentationLevel(countOrText, action) { | ||
const previousState = this._getIndentationState(); | ||
this.queueIndentationLevel(countOrText); | ||
try { | ||
action(); | ||
} | ||
finally { | ||
this._setIdentationState(previousState); | ||
} | ||
return this; | ||
} | ||
setIndentationLevel(countOrText) { | ||
@@ -58,2 +71,13 @@ this._currentIndentation = this._getIndentationLevelFromArg(countOrText); | ||
} | ||
withIdentationLevel(countOrText, action) { | ||
const previousState = this._getIndentationState(); | ||
this.setIndentationLevel(countOrText); | ||
try { | ||
action(); | ||
} | ||
finally { | ||
this._setIdentationState(previousState); | ||
} | ||
return this; | ||
} | ||
/** | ||
@@ -403,3 +427,4 @@ * Gets the current indentation level. | ||
// This is a performance optimization to short circuit all the checks below. If the current char | ||
// is not in this set then it won't change any internal state so no need to continue. | ||
// is not in this set then it won't change any internal state so no need to continue and do | ||
// so many other checks (this made it 3x faster in one scenario I tested). | ||
if (!CodeBlockWriter._isCharToHandle.has(currentChar)) | ||
@@ -489,2 +514,14 @@ continue; | ||
} | ||
/** @internal */ | ||
_setIdentationState(state) { | ||
this._currentIndentation = state.current; | ||
this._queuedIndentation = state.queued; | ||
} | ||
/** @internal */ | ||
_getIndentationState() { | ||
return { | ||
current: this._currentIndentation, | ||
queued: this._queuedIndentation | ||
}; | ||
} | ||
} | ||
@@ -514,6 +551,6 @@ /** @internal */ | ||
for (const char of str) { | ||
if (char === "\t") | ||
if (char === " ") | ||
spacesCount++; | ||
else if (char === "\t") | ||
tabsCount++; | ||
else if (char === " ") | ||
spacesCount++; | ||
} | ||
@@ -520,0 +557,0 @@ return { spacesCount, tabsCount }; |
{ | ||
"name": "code-block-writer", | ||
"version": "8.1.2", | ||
"version": "8.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", |
@@ -62,7 +62,7 @@ code-block-writer | ||
* `quote(text: string)` - Writes text surrounded in quotes. | ||
* `indent()` - Indents the current line. | ||
* `indent(times?: number)` - Indents the current line. Optionally indents multiple times when providing a number. | ||
* `indentBlock(block?: () => void)` - Indents a block of code. | ||
* `space(times?: number)` - Writes a space. Optionally writes multiple spaces if providing a number. | ||
* `space(times?: number)` - Writes a space. Optionally writes multiple spaces when providing a number. | ||
* `spaceIfLastNot()` - Writes a space if the last was not a space. | ||
* `tab(times?: number)` - Writes a tab. Optionally writes multiple tabs if providing a number. | ||
* `tab(times?: number)` - Writes a tab. Optionally writes multiple tabs when providing a number. | ||
* `tabIfLastNot()` - Writes a tab if the last was not a tab. | ||
@@ -78,5 +78,9 @@ * `write(text: string)` - Writes some text. | ||
* `setIndentationLevel(whitespaceText: string)` - Sets the current indentation level based on the provided whitespace text. | ||
* `withIdentationLevel(indentationLevel: number, action: () => void)` - Sets the indentation level within the provided action. | ||
* `withIdentationLevel(whitespaceText: string, action: () => void)` - Sets the indentation level based on the provided whitespace text within the action. | ||
* `getIndentationLevel()` - Gets the current indentation level. | ||
* `queueIndentationLevel(indentationLevel: number)` - Queues an indentation level to be used once a new line is written. | ||
* `queueIndentationLevel(whitespaceText: string)` - Queues an indentation level to be used once a new line is written based on the provided whitespace text. | ||
* `withQueuedIdentationLevel(indentationLevel: number, action: () => void)` - Sets the queued indentation level within the provided action. | ||
* `withQueuedIdentationLevel(whitespaceText: string, action: () => void)` - Sets the queued indentation level based on the provided whitespace text within the action. | ||
* `closeComment()` - Writes text to exit a comment if in a comment. | ||
@@ -83,0 +87,0 @@ * `isInComment()` - Gets if the writer is currently in a comment. |
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
103288
833
105