Skip to content
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

Fix printf-like formatting for custom messages #6389

Merged
merged 1 commit into from Oct 3, 2022

Conversation

ybiquitous
Copy link
Member

@ybiquitous ybiquitous commented Oct 3, 2022

Which issue, if any, is this issue related to?

Ref: #6312

Is there anything in the PR that needs further explanation?

The Node.js util.format() API can concatenate extra arguments to the result string. This behavior is not suitable for custom messages. So, this commit replace util.format() with our implementation.

If there are more arguments passed to the util.format() method than the number of specifiers, the extra arguments are concatenated to the returned string, separated by spaces:

util.format('%s:%s', 'foo', 'bar', 'baz');
// Returns: 'foo:bar baz'

The Node.js `util.format()` API can concatenate extra arguments to the result string.
This behavior is not suitable for custom messages.
So, this commit replace `util.format()` with our implementation.

See https://nodejs.org/api/util.html#utilformatformat-args

> If there are more arguments passed to the `util.format()` method than the number of specifiers,
> the extra arguments are concatenated to the returned string, separated by spaces:
@changeset-bot
Copy link

changeset-bot bot commented Oct 3, 2022

🦋 Changeset detected

Latest commit: 3947cc4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
stylelint Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

*/
function printfLike(format, ...args) {
return args.reduce((/** @type {string} */ result, arg) => {
return result.replace(/%[ds]/, String(arg));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] For now, only %s and %d support seems enough.

@ybiquitous ybiquitous marked this pull request as ready for review October 3, 2022 10:03
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@ybiquitous ybiquitous merged commit 6599729 into main Oct 3, 2022
@ybiquitous ybiquitous deleted the fix-printf-like-for-custom-message branch October 3, 2022 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants