Skip to content

Commit

Permalink
test: replace forEach() in test-stream-pipe-unpipe-stream
Browse files Browse the repository at this point in the history
PR-URL: #50786
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
Chenyu Yang authored May 12, 2024
1 parent 06cde5c commit 7129915
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const {
SymbolIterator,
SymbolPrototypeToString,
SymbolPrototypeValueOf,
SymbolToPrimitive,
SymbolToStringTag,
TypedArrayPrototypeGetLength,
TypedArrayPrototypeGetSymbolToStringTag,
Expand Down Expand Up @@ -2103,6 +2104,11 @@ function hasBuiltInToString(value) {
value = proxyTarget;
}

// Check if value has a custom Symbol.toPrimitive transformation.
if (typeof value[SymbolToPrimitive] === 'function') {
return false;
}

// Count objects that have no `toString` function as built-in.
if (typeof value.toString !== 'function') {
return true;
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,27 @@ assert.strictEqual(util.format('%s', -Infinity), '-Infinity');
);
}

// Symbol.toPrimitive handling for string format specifier
{
const objectWithToPrimitive = {
[Symbol.toPrimitive](hint) {
switch (hint) {
case 'number':
return 42;
case 'string':
return 'string representation';
case 'default':
default:
return 'default context';
}
}
};

assert.strictEqual(util.format('%s', +objectWithToPrimitive), '42');
assert.strictEqual(util.format('%s', objectWithToPrimitive), 'string representation');
assert.strictEqual(util.format('%s', objectWithToPrimitive + ''), 'default context');
}

// JSON format specifier
assert.strictEqual(util.format('%j'), '%j');
assert.strictEqual(util.format('%j', 42), '42');
Expand Down

0 comments on commit 7129915

Please sign in to comment.