code-block-writer
Advanced tools
Comparing version 9.1.3 to 9.2.0
@@ -5,2 +5,13 @@ # Change Log | ||
<a name="9.2.0"></a> | ||
# [9.2.0](https://github.com/dsherret/code-block-writer/compare/v9.1.3...v9.2.0) (2019-05-31) | ||
### Features | ||
* [#27](https://github.com/dsherret/code-block-writer/issues/27) - Add `iterateLastChars`. ([5ce22c2](https://github.com/dsherret/code-block-writer/commit/5ce22c2)) | ||
* Add `withHangingIndentationUnlessBlock(action)` ([c04bef2](https://github.com/dsherret/code-block-writer/commit/c04bef2)) | ||
<a name="9.1.3"></a> | ||
@@ -7,0 +18,0 @@ ## [9.1.3](https://github.com/dsherret/code-block-writer/compare/v9.1.1...v9.1.3) (2019-05-20) |
@@ -55,2 +55,7 @@ /** | ||
/** | ||
* Writes the text within the provided action with hanging indentation unless writing a block. | ||
* @param action - Action to perform with hanging indentation unless a block is written. | ||
*/ | ||
withHangingIndentationUnlessBlock(action: () => void): this; | ||
/** | ||
* Sets the current indentation level. | ||
@@ -74,3 +79,3 @@ * @param indentationLevel - Indentation level to be at. | ||
/** | ||
* Sets the identation level with the provided indentation text within the provided action | ||
* Sets the indentation level with the provided indentation text within the provided action | ||
* and restores the writer's indentation state afterwards. | ||
@@ -81,2 +86,3 @@ * @param whitespaceText - Gets the indentation level from the indentation text. | ||
withIndentationLevel(whitespaceText: string, action: () => void): this; | ||
private _withResetIndentation; | ||
/** | ||
@@ -237,2 +243,10 @@ * Gets the current indentation level. | ||
/** | ||
* Iterates over the writer characters in reverse order. The iteration stops when a non-null or | ||
* undefined value is returned from the action. The returned value is then returned by the method. | ||
* | ||
* @remarks It is much more efficient to use this method rather than `#toString()` since `#toString()` | ||
* will combine the internal array into a string. | ||
*/ | ||
iterateLastChars<T>(action: (char: string, index: number) => T | undefined): T | undefined; | ||
/** | ||
* Gets the writer's text. | ||
@@ -239,0 +253,0 @@ */ |
@@ -51,16 +51,5 @@ "use strict"; | ||
this._queuedIndentation = this._getIndentationLevelFromArg(countOrText); | ||
this._queuedOnlyIfNotBlock = undefined; | ||
return this; | ||
} | ||
/** @internal */ | ||
withQueuedIndentationLevel(countOrText, action) { | ||
const previousState = this._getIndentationState(); | ||
this.queueIndentationLevel(countOrText); | ||
try { | ||
action(); | ||
} | ||
finally { | ||
this._setIndentationState(previousState); | ||
} | ||
return this; | ||
} | ||
/** | ||
@@ -71,4 +60,14 @@ * Writes the text within the provided action with hanging indentation. | ||
withHangingIndentation(action) { | ||
return this.withQueuedIndentationLevel(this.getIndentationLevel() + 1, action); | ||
return this._withResetIndentation(() => this.queueIndentationLevel(this.getIndentationLevel() + 1), action); | ||
} | ||
/** | ||
* Writes the text within the provided action with hanging indentation unless writing a block. | ||
* @param action - Action to perform with hanging indentation unless a block is written. | ||
*/ | ||
withHangingIndentationUnlessBlock(action) { | ||
return this._withResetIndentation(() => { | ||
this.queueIndentationLevel(this.getIndentationLevel() + 1); | ||
this._queuedOnlyIfNotBlock = true; | ||
}, action); | ||
} | ||
setIndentationLevel(countOrText) { | ||
@@ -79,6 +78,9 @@ this._currentIndentation = this._getIndentationLevelFromArg(countOrText); | ||
withIndentationLevel(countOrText, action) { | ||
return this._withResetIndentation(() => this.setIndentationLevel(countOrText), action); | ||
} | ||
_withResetIndentation(setStateAction, writeAction) { | ||
const previousState = this._getIndentationState(); | ||
this.setIndentationLevel(countOrText); | ||
setStateAction(); | ||
try { | ||
action(); | ||
writeAction(); | ||
} | ||
@@ -365,2 +367,22 @@ finally { | ||
} | ||
/** | ||
* Iterates over the writer characters in reverse order. The iteration stops when a non-null or | ||
* undefined value is returned from the action. The returned value is then returned by the method. | ||
* | ||
* @remarks It is much more efficient to use this method rather than `#toString()` since `#toString()` | ||
* will combine the internal array into a string. | ||
*/ | ||
iterateLastChars(action) { | ||
let index = this._length; | ||
for (let i = this._texts.length - 1; i >= 0; i--) { | ||
const currentText = this._texts[i]; | ||
for (let j = currentText.length - 1; j >= 0; j--) { | ||
index--; | ||
const result = action(currentText[j], index); | ||
if (result != null) | ||
return result; | ||
} | ||
} | ||
return undefined; | ||
} | ||
/** @internal */ | ||
@@ -420,10 +442,35 @@ _getLastCharWithOffset(offset) { | ||
this._isOnFirstLineOfBlock = false; | ||
this.dequeueQueuedIndentation(); | ||
this._dequeueQueuedIndentation(); | ||
} | ||
/** @internal */ | ||
dequeueQueuedIndentation() { | ||
_dequeueQueuedIndentation() { | ||
if (this._queuedIndentation == null) | ||
return; | ||
this._currentIndentation = this._queuedIndentation; | ||
this._queuedIndentation = undefined; | ||
if (this._queuedOnlyIfNotBlock && wasLastBlock(this)) { | ||
this._queuedIndentation = undefined; | ||
this._queuedOnlyIfNotBlock = undefined; | ||
} | ||
else { | ||
this._currentIndentation = this._queuedIndentation; | ||
this._queuedIndentation = undefined; | ||
} | ||
function wasLastBlock(writer) { | ||
let foundNewLine = false; | ||
return writer.iterateLastChars(char => { | ||
switch (char) { | ||
case "\n": | ||
if (foundNewLine) | ||
return false; | ||
else | ||
foundNewLine = true; | ||
break; | ||
case "\r": | ||
return undefined; | ||
case "{": | ||
return true; | ||
default: | ||
return false; | ||
} | ||
}); | ||
} | ||
} | ||
@@ -527,2 +574,3 @@ /** @internal */ | ||
this._queuedIndentation = state.queued; | ||
this._queuedOnlyIfNotBlock = state.queuedOnlyIfNotBlock; | ||
} | ||
@@ -533,3 +581,4 @@ /** @internal */ | ||
current: this._currentIndentation, | ||
queued: this._queuedIndentation | ||
queued: this._queuedIndentation, | ||
queuedOnlyIfNotBlock: this._queuedOnlyIfNotBlock | ||
}; | ||
@@ -536,0 +585,0 @@ } |
{ | ||
"name": "code-block-writer", | ||
"version": "9.1.3", | ||
"version": "9.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", |
@@ -48,3 +48,3 @@ code-block-writer | ||
} | ||
de``` | ||
``` | ||
@@ -84,2 +84,3 @@ ## Methods | ||
* `withHangingIndentation(action: () => void)` - Writes the code within the action with hanging indentation. | ||
* `withHangingIndentationUnlessBlock(action: () => void)` - Writes the code within the action with hanging indentation unless a block is written going from the first line to the second. | ||
* `closeComment()` - Writes text to exit a comment if in a comment. | ||
@@ -95,2 +96,3 @@ * `isInComment()` - Gets if the writer is currently in a comment. | ||
* `getLastChar()` - Gets the last character written. | ||
* `iterateLastChars<T>(action: (char: string, index: number) => T | undefined): T | undefined` - Iterates over the writer's characters in reverse order, stopping once a non-null or undefined value is returned and returns that value. | ||
* `getOptions()` - Gets the writer options. | ||
@@ -97,0 +99,0 @@ * `toString()` - Gets the string. |
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
107384
896
106