Skip to content

Commit 5029fd0

Browse files
Masashi Hiranojasnell
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 e0f996c commit 5029fd0

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
@@ -202,8 +202,8 @@ Each placeholder token is replaced with the converted value from the
202202
corresponding argument. Supported placeholders are:
203203

204204
* `%s` - `String`.
205-
* `%d` - `Number` (integer or floating point value).
206-
* `%i` - Integer.
205+
* `%d` - `Number` (integer or floating point value) or `BigInt`.
206+
* `%i` - Integer or `BigInt`.
207207
* `%f` - Floating point value.
208208
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument
209209
contains circular references.

lib/util.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ function formatWithOptions(inspectOptions, f) {
102102
tempStr = tryStringify(arguments[a++]);
103103
break;
104104
case 100: // 'd'
105-
tempStr = `${Number(arguments[a++])}`;
105+
const tempNum = arguments[a++];
106+
// eslint-disable-next-line valid-typeof
107+
if (typeof tempNum === 'bigint') {
108+
tempStr = `${tempNum}n`;
109+
} else {
110+
tempStr = `${Number(tempNum)}`;
111+
}
106112
break;
107113
case 79: // 'O'
108114
tempStr = inspect(arguments[a++], inspectOptions);
@@ -117,7 +123,13 @@ function formatWithOptions(inspectOptions, f) {
117123
break;
118124
}
119125
case 105: // 'i'
120-
tempStr = `${parseInt(arguments[a++])}`;
126+
const tempInteger = arguments[a++];
127+
// eslint-disable-next-line valid-typeof
128+
if (typeof tempInteger === 'bigint') {
129+
tempStr = `${tempInteger}n`;
130+
} else {
131+
tempStr = `${parseInt(tempInteger)}`;
132+
}
121133
break;
122134
case 102: // 'f'
123135
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)