util: allow styleText text param to accept non-string scalars#64452
Open
SparshGarg999 wants to merge 1 commit into
Open
util: allow styleText text param to accept non-string scalars#64452SparshGarg999 wants to merge 1 commit into
SparshGarg999 wants to merge 1 commit into
Conversation
Allow util.styleText() to accept
umber, �oolean, and �igint`nvalues for the ext parameter, automatically coercing them to string
via String(text).
Previously, passing anything other than a string would throw
ERR_INVALID_ARG_TYPE. This forced callers to wrap every non-string
value manually:
util.styleText('red', String(count)) // before
util.styleText('red', count) // after
The coercion is applied before the fast path so there is no extra
branching cost for the common string case. Values that cannot be
meaningfully stringified (null, undefined, Symbol, Function, Object)
still throw ERR_INVALID_ARG_TYPE as before.
Fixes: nodejs#63841
Signed-off-by: SparshGarg999 <sparshgarg999@gmail.com>
2e48257 to
e899ec4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Allow
util.styleText()to acceptnumber,boolean, andbigintvalues for thetextparameter, automatically coercing them to a string internally usingString(text).Description
Currently, passing a non-string value (such as a number, boolean, or bigint) to
util.styleText()immediately throwsERR_INVALID_ARG_TYPE. This requires callers to manually cast non-string values before formatting them:This commit updates
util.styleTextto check the type oftextand automatically coerce scalar values (number, boolean, bigint) to strings. The coercion happens before the fast path checks to avoid adding extra branching overhead for the common string case.Non-string values that cannot be meaningfully coerced without potential issues (e.g.
null,undefined,Symbol,Function,Object) still throwERR_INVALID_ARG_TYPE.Verification
Existing tests in
test/parallel/test-util-styletext.jshave been updated to check for scalar coercion behavior, and to verify that incorrect non-string types still throw.Fixes: #63841