From 2aa29a626aa05fa4c2056dfa30a9d77caa0d8c6f Mon Sep 17 00:00:00 2001 From: Jose Sinohui Date: Wed, 27 Nov 2019 11:15:24 -0700 Subject: [PATCH 1/2] Adds BigInt support to stringify util function. --- lib/utils.js | 3 +++ test/unit/utils.spec.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index 853fd5b108..c2c5a9573e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -298,6 +298,9 @@ function jsonStringify(object, spaces, depth) { ? '-0' : val.toString(); break; + case 'bigint': + val = val.toString() + 'n'; + break; case 'date': var sDate = isNaN(val.getTime()) ? val.toString() : val.toISOString(); val = '[Date: ' + sDate + ']'; diff --git a/test/unit/utils.spec.js b/test/unit/utils.spec.js index c239627f55..6adf57a037 100644 --- a/test/unit/utils.spec.js +++ b/test/unit/utils.spec.js @@ -1,3 +1,4 @@ +/* global BigInt */ 'use strict'; var utils = require('../../lib/utils'); @@ -204,6 +205,11 @@ describe('lib/utils', function() { expect(stringify(1.2), 'to be', '1.2'); expect(stringify(1e9), 'to be', '1000000000'); }); + + it('bigints', function() { + expect(stringify(BigInt(1)), 'to be', '1n'); + expect(stringify(BigInt(2)), 'to be', '2n'); + }); }); describe('canonicalize example', function() { From 2127bcde907c13023b94d9efdacafe5b091fe62d Mon Sep 17 00:00:00 2001 From: JosejeSinohui Date: Wed, 27 Nov 2019 14:28:47 -0700 Subject: [PATCH 2/2] only test BigInt if it's defined --- test/unit/utils.spec.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/unit/utils.spec.js b/test/unit/utils.spec.js index 6adf57a037..a04495a09a 100644 --- a/test/unit/utils.spec.js +++ b/test/unit/utils.spec.js @@ -206,10 +206,12 @@ describe('lib/utils', function() { expect(stringify(1e9), 'to be', '1000000000'); }); - it('bigints', function() { - expect(stringify(BigInt(1)), 'to be', '1n'); - expect(stringify(BigInt(2)), 'to be', '2n'); - }); + if (typeof BigInt === 'function') { + it('should work with bigints when possible', function() { + expect(stringify(BigInt(1)), 'to be', '1n'); + expect(stringify(BigInt(2)), 'to be', '2n'); + }); + } }); describe('canonicalize example', function() {