Skip to content

Commit

Permalink
fix: style functions are now defined on Logger prototype instead of e…
Browse files Browse the repository at this point in the history
…ach instance
  • Loading branch information
lisaychuang committed Aug 7, 2018
1 parent 41d7ff9 commit 12cd83a
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ export class Logger {
this.printer = config.printer;
this.env = config.env || (isBrowser() ? 'browser' : 'node');
}
this.setupStyles();
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 &
Expand All @@ -382,4 +380,6 @@ export interface LoggerConstructor {
): LoggerWithStyles;
}

setupStyles(); // install all the color getter functions onto the prototype

export default Logger as LoggerConstructor;

0 comments on commit 12cd83a

Please sign in to comment.