-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor functions to use context object #452
Conversation
@mcollina in order to avoid the situation in #449 that led to having to create #453, once this and #453 are approved I will be merging in the following order: It will negate the approvals on those issues as I move through them, but nothing will be changing other than everything coming together. I broke this up into multiple PRs in an effort to make it a least somewhat easier to review. |
For what it's worth, I'm getting benchmark numbers like this with the PR:
Slightly different than the results discovered with #451. I think the maintenance benefits will far outweigh any decrease in speed. |
@@ -37,42 +26,50 @@ const jsonParser = input => { | |||
* @property {boolean} [colorizeObjects=true] Apply coloring to rendered objects | |||
* when coloring is enabled. | |||
* @property {boolean} [crlf=false] End lines with `\r\n` instead of `\n`. | |||
* @property {string|null} [customColors=null] A comma separated list of colors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc block got reorganized to order the properties in alphabetical order. No real change here.
@@ -84,22 +81,25 @@ const defaultOptions = { | |||
colorize: isColorSupported, | |||
colorizeObjects: true, | |||
crlf: false, | |||
customColors: null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as with the doc block. Properties were reorganized to be in alphabetical order.
const context = parseFactoryOptions(Object.assign({}, defaultOptions, options)) | ||
return pretty.bind({ ...context, context }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of the implementation code was moved to lib/pretty.js
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New file that contains the original prettification code from the root index.js
.
if (this.minimumLevel) { | ||
// We need to figure out if the custom levels has the desired minimum | ||
// level & use that one if found. If not, determine if the level exists | ||
// in the standard levels. In both cases, make sure we have the level | ||
// number instead of the level name. | ||
let condition | ||
if (this.useOnlyCustomProps) { | ||
condition = this.customLevels | ||
} else { | ||
condition = this.customLevelNames[this.minimumLevel] !== undefined | ||
} | ||
let minimum | ||
if (condition) { | ||
minimum = this.customLevelNames[this.minimumLevel] | ||
} else { | ||
minimum = LEVEL_NAMES[this.minimumLevel] | ||
} | ||
if (!minimum) { | ||
minimum = typeof this.minimumLevel === 'string' | ||
? LEVEL_NAMES[this.minimumLevel] | ||
: LEVEL_NAMES[LEVELS[this.minimumLevel].toLowerCase()] | ||
} | ||
|
||
const level = log[this.levelKey === undefined ? LEVEL_KEY : this.levelKey] | ||
if (level < minimum) return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a change from the original code. See the PR description for details on this block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new file that contains the original code from prettyFactory
that parsed the passed in options. This one parses the options and returns a standardized context object that we can pass around to the various prettifier functions.
@mcollina when you get a chance, please review this one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great work much needed!
This PR refactors all of the prettify functions to use a context object. This removes the need to keep track of all of the parsed options and other items such functions need in order to perform their work. I believe this will make the code base easier to maintain going forward, and will make PRs like #445 easier to implement.
It's not clear to me why the following block of code in 770f56a stops working when refactoring it to use the context object:
pino-pretty/index.js
Lines 117 to 122 in fa386a9
The short of it is that the following test starts failing:
pino-pretty/test/basic.test.js
Lines 642 to 664 in fa386a9
It starts failing because it ultimately invokes
Number("info")
, resulting inNaN
, on line 119 of the currentmaster
code becausethis.customLevels
defaults to an empty object. This is mysterious to me because the same default is present on the master branch.Regardless, unrolling the conditionals is far easier to read and reason through. But I'm still likely to revisit this block before considering this PR ready.
Update: I thought it might be the same thing as this:
pino-pretty/lib/pretty.js
Lines 88 to 91 in a74b7b1
But it does not seem to be. I'm going to leave the unrolled block in place. I really don't like the conditionals nested in ternaries to begin with.