symfony-style-console
Version:
Use the style and utilities of the Symfony Console in Node.js
96 lines (95 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var OutputFormatterStyle_1 = require("./OutputFormatterStyle");
/**
* Manages nesting of [[OutputFormatterStyleInterface]] styles.
*
*
* @author Jean-François Simon <contact@jfsimon.fr>
*
* Original PHP class
*
* @author Florian Reuschel <florian@loilo.de>
*
* Port to TypeScript
*
*/
var OutputFormatterStyleStack = /** @class */ (function () {
/**
* Constructor.
*
* @param emptyStyle
*/
function OutputFormatterStyleStack(emptyStyle) {
if (emptyStyle === void 0) { emptyStyle = null; }
this.emptyStyle = emptyStyle || new OutputFormatterStyle_1.default();
this.reset();
}
/**
* Resets stack (ie. empty internal arrays).
*/
OutputFormatterStyleStack.prototype.reset = function () {
this.styles = [];
};
/**
* Pushes a style in the stack.
*
* @param style
*/
OutputFormatterStyleStack.prototype.push = function (style) {
this.styles.push(style);
};
/**
* Pops a style from the stack.
*
* @param OutputFormatterStyle|null style
*
* @return OutputFormatterStyle
*
* @throws InvalidArgumentException When style tags incorrectly nested
*/
OutputFormatterStyleStack.prototype.pop = function (style) {
if (style === void 0) { style = null; }
if (!this.styles.length) {
return this.emptyStyle;
}
if (null === style) {
return this.styles.pop();
}
for (var index = this.styles.length - 1; index >= 0; index--) {
var stackedStyle = this.styles[index];
if (style.apply('') === stackedStyle.apply('')) {
this.styles = this.styles.slice(0, index);
return stackedStyle;
}
}
throw new SyntaxError('Incorrectly nested style tag found.');
};
/**
* Computes current style with stacks top codes.
*
* @return OutputFormatterStyle
*/
OutputFormatterStyleStack.prototype.getCurrent = function () {
if (!this.styles.length) {
return this.emptyStyle;
}
return this.styles[this.styles.length - 1];
};
/**
* @param emptyStyle
* @return this
*/
OutputFormatterStyleStack.prototype.setEmptyStyle = function (emptyStyle) {
this.emptyStyle = emptyStyle;
return this;
};
/**
* @return OutputFormatterStyle
*/
OutputFormatterStyleStack.prototype.getEmptyStyle = function () {
return this.emptyStyle;
};
return OutputFormatterStyleStack;
}());
exports.default = OutputFormatterStyleStack;