summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMasashi Hirano <cherrydog07@gmail.com>2018-08-03 00:52:27 +0900
committerRoman Reiss <me@silverwind.io>2018-10-09 20:11:48 +0200
commitd71dd97263afc65c5cbf9e08ddc94c13f2479b8c (patch)
treee10e97425b5df13dd6cf475e77548fc3d3cf2679
parent186ce7e837bde20bd2726d662721a93c95ba24a6 (diff)
downloadandroid-node-v8-d71dd97263afc65c5cbf9e08ddc94c13f2479b8c.tar.gz
android-node-v8-d71dd97263afc65c5cbf9e08ddc94c13f2479b8c.tar.bz2
android-node-v8-d71dd97263afc65c5cbf9e08ddc94c13f2479b8c.zip
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>
-rw-r--r--doc/api/util.md4
-rw-r--r--lib/util.js16
-rw-r--r--test/parallel/test-util-format.js34
3 files changed, 50 insertions, 4 deletions
diff --git a/doc/api/util.md b/doc/api/util.md
index e8576d7b8d..ead1afcd42 100644
--- a/doc/api/util.md
+++ b/doc/api/util.md
@@ -202,8 +202,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.
diff --git a/lib/util.js b/lib/util.js
index 27affda109..2e42beb58b 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -102,7 +102,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);
@@ -117,7 +123,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++])}`;
diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js
index a8adf93671..0c4ba82fec 100644
--- a/test/parallel/test-util-format.js
+++ b/test/parallel/test-util-format.js
@@ -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');
@@ -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');