From 12cd83a428a04445871efa79e0823747b8361f1a Mon Sep 17 00:00:00 2001 From: Lisa Huang Date: Mon, 6 Aug 2018 23:24:03 -0700 Subject: [PATCH] fix: style functions are now defined on Logger prototype instead of each instance --- src/logger.ts | 70 +++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/logger.ts b/src/logger.ts index 2605bd0..7c097db 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -121,7 +121,6 @@ export class Logger { this.printer = config.printer; this.env = config.env || (isBrowser() ? 'browser' : 'node'); } - this.setupStyles(); } /** @@ -229,40 +228,6 @@ export class Logger { return this.printMessage(Level.debug); } - /** - * According to the logStyles in './style.ts', set up - * a property for each, kind of like - * ```ts - * { - * get red() { }, // store "color: red" - * get bgYellow() { } // store "background-color: yellow" - * } - * ``` - * Ultimately, we want to be able to do something like - * logger.red.bgYellow.txt('Hello red and yellow') - */ - private setupStyles() { - // Loop over each style name (i.e. "red") - for (let styleName in logStyles) { - // Make sure the property is on the instance, not the prototype - if (logStyles.hasOwnProperty(styleName)) { - // Define a new property on this, of name c (i.e. "red") - // that is getter-based (instead of value based) - const self = this; - Object.defineProperty(this, styleName, { - get() { - /** - * Store the styleName, to be dealt with the next time - * a `.txt()`, `.log()`, `.debug()`, `.error()` or `.warn()` call is made - */ - self.stylesInProgress.push(styleName); - return this; - } - }); - } - } - } - /** * Actually print the message to the printer (maybe console.*) * @param level the level of the current message @@ -367,6 +332,39 @@ export class Logger { } } +/** + * According to the logStyles in './style.ts', set up + * a property for each, kind of like + * ```ts + * { + * get red() { }, // store "color: red" + * get bgYellow() { } // store "background-color: yellow" + * } + * ``` + * Ultimately, we want to be able to do something like + * logger.red.bgYellow.txt('Hello red and yellow') + */ +function setupStyles() { + // Loop over each style name (i.e. "red") + for (let styleName in logStyles) { + // Make sure the property is on the instance, not the prototype + if (logStyles.hasOwnProperty(styleName)) { + // Define a new property on this, of name c (i.e. "red") + // that is getter-based (instead of value based) + Object.defineProperty(Logger.prototype, styleName, { + get() { + /** + * Store the styleName, to be dealt with the next time + * a `.txt()`, `.log()`, `.debug()`, `.error()` or `.warn()` call is made + */ + this.stylesInProgress.push(styleName); + return this; + } + }); + } + } +} + export type LoggerWithStyles = Logger & { [K in keyof (TextColors & @@ -382,4 +380,6 @@ export interface LoggerConstructor { ): LoggerWithStyles; } +setupStyles(); // install all the color getter functions onto the prototype + export default Logger as LoggerConstructor;