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
Support template literals for nested calls #392
Changes from 4 commits
7d238e2
e5ea8df
60db67b
2451677
26c73d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -134,6 +134,11 @@ const createStyler = (open, close, parent) => { | |||||||||||
|
||||||||||||
const createBuilder = (self, _styler, _isEmpty) => { | ||||||||||||
const builder = (...arguments_) => { | ||||||||||||
if (Array.isArray(arguments_[0])) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise, this would break any sort of usecase where people are relying on array -> string coercion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ Would be good to have a test for this too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It migh also be a good idea to cache
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||
// Called as a template literal, for example: chalk.red`2 + 3 = {bold ${2+3}}` | ||||||||||||
return applyStyle(builder, chalkTag(builder, ...arguments_)); | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Single argument is hot path, implicit coercion is faster than anything | ||||||||||||
// eslint-disable-next-line no-implicit-coercion | ||||||||||||
return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' ')); | ||||||||||||
|
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.
Why manually set iterations here? This can skew results if too low.
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.
Parsing strings for the special
{bold ...}
-syntax is a lot slower.Benchmark 'cached: 1 style' is almost 200 times faster than 'cached: 1 style template literal'. So I reduced the number of iterations accordingly.
Better than specifying the number of iterations would be to set a minimal runtime for each benchmark. So instead of
chalk/benchmark.js
Line 6 in 26c73d2
something like
But this feels out of the scope of this PR.
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.
I agree. Mind opening a PR? :)