util: support BigInt in util.format

`util.format` and `console.log` now support BigInt via the existing
format specifiers `%i` and `%d`.

PR-URL: https://github.com/nodejs/node/pull/22097
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Masashi Hirano 2018-08-03 00:52:27 +09:00 committed by Roman Reiss
parent 186ce7e837
commit d71dd97263
No known key found for this signature in database
GPG Key ID: 2E62B41C93869443
3 changed files with 50 additions and 4 deletions

View File

@ -202,8 +202,8 @@ Each placeholder token is replaced with the converted value from the
corresponding argument. Supported placeholders are: corresponding argument. Supported placeholders are:
* `%s` - `String`. * `%s` - `String`.
* `%d` - `Number` (integer or floating point value). * `%d` - `Number` (integer or floating point value) or `BigInt`.
* `%i` - Integer. * `%i` - Integer or `BigInt`.
* `%f` - Floating point value. * `%f` - Floating point value.
* `%j` - JSON. Replaced with the string `'[Circular]'` if the argument * `%j` - JSON. Replaced with the string `'[Circular]'` if the argument
contains circular references. contains circular references.

View File

@ -102,7 +102,13 @@ function formatWithOptions(inspectOptions, f) {
tempStr = tryStringify(arguments[a++]); tempStr = tryStringify(arguments[a++]);
break; break;
case 100: // 'd' 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; break;
case 79: // 'O' case 79: // 'O'
tempStr = inspect(arguments[a++], inspectOptions); tempStr = inspect(arguments[a++], inspectOptions);
@ -117,7 +123,13 @@ function formatWithOptions(inspectOptions, f) {
break; break;
} }
case 105: // 'i' 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; break;
case 102: // 'f' case 102: // 'f'
tempStr = `${parseFloat(arguments[a++])}`; tempStr = `${parseFloat(arguments[a++])}`;

View File

@ -68,6 +68,18 @@ assert.strictEqual(util.format('%d', -0.5), '-0.5');
assert.strictEqual(util.format('%d', ''), '0'); assert.strictEqual(util.format('%d', ''), '0');
assert.strictEqual(util.format('%d %d', 42, 43), '42 43'); assert.strictEqual(util.format('%d %d', 42, 43), '42 43');
assert.strictEqual(util.format('%d %d', 42), '42 %d'); 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 // Integer format specifier
assert.strictEqual(util.format('%i'), '%i'); assert.strictEqual(util.format('%i'), '%i');
@ -80,6 +92,28 @@ assert.strictEqual(util.format('%i', -0.5), '0');
assert.strictEqual(util.format('%i', ''), 'NaN'); assert.strictEqual(util.format('%i', ''), 'NaN');
assert.strictEqual(util.format('%i %i', 42, 43), '42 43'); assert.strictEqual(util.format('%i %i', 42, 43), '42 43');
assert.strictEqual(util.format('%i %i', 42), '42 %i'); 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 // Float format specifier
assert.strictEqual(util.format('%f'), '%f'); assert.strictEqual(util.format('%f'), '%f');