From 91229099b5a4bf8c7c5e78ae82ace3d9085030cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Jes=C3=BAs=20Sinohui=20Fern=C3=A1ndez?= Date: Wed, 3 Feb 2021 09:43:29 -0800 Subject: [PATCH] Adds BigInt support to stringify util function (#4112) --- lib/utils.js | 3 +++ test/unit/utils.spec.js | 8 ++++++++ 2 files changed, 11 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..a04495a09a 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,13 @@ describe('lib/utils', function() { expect(stringify(1.2), 'to be', '1.2'); expect(stringify(1e9), 'to be', '1000000000'); }); + + 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() {