Skip to content

Commit 384221e

Browse files
Masashi Hiranotargos
Masashi Hirano
authored andcommitted
util: support BigInt in util.format
`util.format` and `console.log` now support BigInt via the existing format specifiers `%i` and `%d`. PR-URL: #22097 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 76cb52c commit 384221e

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

doc/api/util.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ Each placeholder token is replaced with the converted value from the
198198
corresponding argument. Supported placeholders are:
199199

200200
* `%s` - `String`.
201-
* `%d` - `Number` (integer or floating point value).
202-
* `%i` - Integer.
201+
* `%d` - `Number` (integer or floating point value) or `BigInt`.
202+
* `%i` - Integer or `BigInt`.
203203
* `%f` - Floating point value.
204204
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument
205205
contains circular references.

lib/util.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ function formatWithOptions(inspectOptions, f) {
103103
tempStr = tryStringify(arguments[a++]);
104104
break;
105105
case 100: // 'd'
106-
tempStr = `${Number(arguments[a++])}`;
106+
const tempNum = arguments[a++];
107+
// eslint-disable-next-line valid-typeof
108+
if (typeof tempNum === 'bigint') {
109+
tempStr = `${tempNum}n`;
110+
} else {
111+
tempStr = `${Number(tempNum)}`;
112+
}
107113
break;
108114
case 79: // 'O'
109115
tempStr = inspect(arguments[a++], inspectOptions);
@@ -119,7 +125,13 @@ function formatWithOptions(inspectOptions, f) {
119125
break;
120126
}
121127
case 105: // 'i'
122-
tempStr = `${parseInt(arguments[a++])}`;
128+
const tempInteger = arguments[a++];
129+
// eslint-disable-next-line valid-typeof
130+
if (typeof tempInteger === 'bigint') {
131+
tempStr = `${tempInteger}n`;
132+
} else {
133+
tempStr = `${parseInt(tempInteger)}`;
134+
}
123135
break;
124136
case 102: // 'f'
125137
tempStr = `${parseFloat(arguments[a++])}`;

test/parallel/test-util-format.js

+34
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ assert.strictEqual(util.format('%d', -0.5), '-0.5');
6868
assert.strictEqual(util.format('%d', ''), '0');
6969
assert.strictEqual(util.format('%d %d', 42, 43), '42 43');
7070
assert.strictEqual(util.format('%d %d', 42), '42 %d');
71+
assert.strictEqual(
72+
util.format('%d', 1180591620717411303424),
73+
'1.1805916207174113e+21'
74+
);
75+
assert.strictEqual(
76+
util.format('%d', 1180591620717411303424n),
77+
'1180591620717411303424n'
78+
);
79+
assert.strictEqual(
80+
util.format('%d %d', 1180591620717411303424n, 12345678901234567890123n),
81+
'1180591620717411303424n 12345678901234567890123n'
82+
);
7183

7284
// Integer format specifier
7385
assert.strictEqual(util.format('%i'), '%i');
@@ -80,6 +92,28 @@ assert.strictEqual(util.format('%i', -0.5), '0');
8092
assert.strictEqual(util.format('%i', ''), 'NaN');
8193
assert.strictEqual(util.format('%i %i', 42, 43), '42 43');
8294
assert.strictEqual(util.format('%i %i', 42), '42 %i');
95+
assert.strictEqual(
96+
util.format('%i', 1180591620717411303424),
97+
'1'
98+
);
99+
assert.strictEqual(
100+
util.format('%i', 1180591620717411303424n),
101+
'1180591620717411303424n'
102+
);
103+
assert.strictEqual(
104+
util.format('%i %i', 1180591620717411303424n, 12345678901234567890123n),
105+
'1180591620717411303424n 12345678901234567890123n'
106+
);
107+
108+
assert.strictEqual(
109+
util.format('%d %i', 1180591620717411303424n, 12345678901234567890123n),
110+
'1180591620717411303424n 12345678901234567890123n'
111+
);
112+
113+
assert.strictEqual(
114+
util.format('%i %d', 1180591620717411303424n, 12345678901234567890123n),
115+
'1180591620717411303424n 12345678901234567890123n'
116+
);
83117

84118
// Float format specifier
85119
assert.strictEqual(util.format('%f'), '%f');

0 commit comments

Comments
 (0)