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

util: support BigInt in util.format #22097

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ Each placeholder token is replaced with the converted value from the
corresponding argument. Supported placeholders are:

* `%s` - `String`.
* `%d` - `Number` (integer or floating point value).
* `%i` - Integer.
* `%d` - `Number` (integer or floating point value) or `BigInt`.
* `%i` - Integer or `BigInt`.
* `%f` - Floating point value.
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument
contains circular references.
Expand Down
16 changes: 14 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,13 @@ function formatWithOptions(inspectOptions, f) {
tempStr = tryStringify(arguments[a++]);
break;
case 100: // 'd'
tempStr = `${Number(arguments[a++])}`;
const tempNum = arguments[a++];
// eslint-disable-next-line valid-typeof
if (typeof tempNum === 'bigint') {
tempStr = `${tempNum}n`;
} else {
tempStr = `${Number(tempNum)}`;
}
break;
case 79: // 'O'
tempStr = inspect(arguments[a++], inspectOptions);
Expand All @@ -307,7 +313,13 @@ function formatWithOptions(inspectOptions, f) {
break;
}
case 105: // 'i'
tempStr = `${parseInt(arguments[a++])}`;
const tempInteger = arguments[a++];
// eslint-disable-next-line valid-typeof
if (typeof tempInteger === 'bigint') {
tempStr = `${tempInteger}n`;
} else {
tempStr = `${parseInt(tempInteger)}`;
}
break;
case 102: // 'f'
tempStr = `${parseFloat(arguments[a++])}`;
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ assert.strictEqual(util.format('%d', -0.5), '-0.5');
assert.strictEqual(util.format('%d', ''), '0');
assert.strictEqual(util.format('%d %d', 42, 43), '42 43');
assert.strictEqual(util.format('%d %d', 42), '42 %d');
assert.strictEqual(
util.format('%d', 1180591620717411303424),
'1.1805916207174113e+21'
);
assert.strictEqual(
util.format('%d', 1180591620717411303424n),
'1180591620717411303424n'
);
assert.strictEqual(
util.format('%d %d', 1180591620717411303424n, 12345678901234567890123n),
'1180591620717411303424n 12345678901234567890123n'
);

// Integer format specifier
assert.strictEqual(util.format('%i'), '%i');
Expand All @@ -80,6 +92,28 @@ assert.strictEqual(util.format('%i', -0.5), '0');
assert.strictEqual(util.format('%i', ''), 'NaN');
assert.strictEqual(util.format('%i %i', 42, 43), '42 43');
assert.strictEqual(util.format('%i %i', 42), '42 %i');
assert.strictEqual(
util.format('%i', 1180591620717411303424),
'1'
);
assert.strictEqual(
util.format('%i', 1180591620717411303424n),
'1180591620717411303424n'
);
assert.strictEqual(
util.format('%i %i', 1180591620717411303424n, 12345678901234567890123n),
'1180591620717411303424n 12345678901234567890123n'
);

assert.strictEqual(
util.format('%d %i', 1180591620717411303424n, 12345678901234567890123n),
'1180591620717411303424n 12345678901234567890123n'
);

assert.strictEqual(
util.format('%i %d', 1180591620717411303424n, 12345678901234567890123n),
'1180591620717411303424n 12345678901234567890123n'
);

// Float format specifier
assert.strictEqual(util.format('%f'), '%f');
Expand Down