From 08f67c4e87d7a2ad32fc3e4038f57e2699ef0e8d Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Fri, 3 Aug 2018 00:52:27 +0900 Subject: [PATCH 1/4] util: support BigInt in util.format Adding support for BigInt in util.format and console.log. Placeholder `%d` is replaced as Number when BigInt is set in second argument. Now, `util.format('%d', 1180591620717411303424n)` returns `'1.1805916207174113e+21'`. However, expected result is `'1180591620717411303424'`. --- doc/api/util.md | 2 +- lib/util.js | 8 +++++++- test/parallel/test-util-format.js | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index d29fbfc5b7f050..96838fb8e2f77c 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -198,7 +198,7 @@ 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). +* `%d` - `Number` (integer or floating point value) or `BigInt`. * `%i` - Integer. * `%f` - Floating point value. * `%j` - JSON. Replaced with the string `'[Circular]'` if the argument diff --git a/lib/util.js b/lib/util.js index 89c86cb7fd0050..24b7a931396aac 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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); diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index a8adf936719932..5a85870f1a33c0 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -68,6 +68,14 @@ 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' +); // Integer format specifier assert.strictEqual(util.format('%i'), '%i'); From 932002cb0017e54df844db7ed65a7b9ccf026cda Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Mon, 17 Sep 2018 00:46:12 +0900 Subject: [PATCH 2/4] util: %i supports BigInt --- lib/util.js | 8 +++++++- test/parallel/test-util-format.js | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 24b7a931396aac..4697c0c8d70939 100644 --- a/lib/util.js +++ b/lib/util.js @@ -313,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++])}`; diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 5a85870f1a33c0..f861e82ae442cb 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -88,6 +88,15 @@ 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' +); + // Float format specifier assert.strictEqual(util.format('%f'), '%f'); From 097ba592f309360c9d67ccb68eb0429452fc6bfc Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Tue, 18 Sep 2018 12:42:56 +0900 Subject: [PATCH 3/4] util: update util.md that supports BigInt --- doc/api/util.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/util.md b/doc/api/util.md index 96838fb8e2f77c..cdff27e952f564 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -199,7 +199,7 @@ corresponding argument. Supported placeholders are: * `%s` - `String`. * `%d` - `Number` (integer or floating point value) or `BigInt`. -* `%i` - Integer. +* `%i` - Integer or `BigInt`. * `%f` - Floating point value. * `%j` - JSON. Replaced with the string `'[Circular]'` if the argument contains circular references. From 946fdecbdffefa01f1a38515f005ca8e6b6545dd Mon Sep 17 00:00:00 2001 From: Masashi Hirano Date: Tue, 18 Sep 2018 15:37:01 +0900 Subject: [PATCH 4/4] util: add tests to check multiple %d/%i specifiers for BigInt --- test/parallel/test-util-format.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index f861e82ae442cb..0c4ba82fec8359 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -76,6 +76,10 @@ 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'); @@ -96,7 +100,20 @@ 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');