Skip to content

Commit 9122909

Browse files
authored
Adds BigInt support to stringify util function (#4112)
1 parent 9878f32 commit 9122909

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/utils.js

+3
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ function jsonStringify(object, spaces, depth) {
298298
? '-0'
299299
: val.toString();
300300
break;
301+
case 'bigint':
302+
val = val.toString() + 'n';
303+
break;
301304
case 'date':
302305
var sDate = isNaN(val.getTime()) ? val.toString() : val.toISOString();
303306
val = '[Date: ' + sDate + ']';

test/unit/utils.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* global BigInt */
12
'use strict';
23

34
var utils = require('../../lib/utils');
@@ -204,6 +205,13 @@ describe('lib/utils', function() {
204205
expect(stringify(1.2), 'to be', '1.2');
205206
expect(stringify(1e9), 'to be', '1000000000');
206207
});
208+
209+
if (typeof BigInt === 'function') {
210+
it('should work with bigints when possible', function() {
211+
expect(stringify(BigInt(1)), 'to be', '1n');
212+
expect(stringify(BigInt(2)), 'to be', '2n');
213+
});
214+
}
207215
});
208216

209217
describe('canonicalize example', function() {

0 commit comments

Comments
 (0)