Skip to content

Commit

Permalink
Adds BigInt support to stringify util function (#4112)
Browse files Browse the repository at this point in the history
  • Loading branch information
José Jesús Sinohui Fernández committed Feb 3, 2021
1 parent 9878f32 commit 9122909
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/utils.js
Expand Up @@ -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 + ']';
Expand Down
8 changes: 8 additions & 0 deletions test/unit/utils.spec.js
@@ -1,3 +1,4 @@
/* global BigInt */
'use strict';

var utils = require('../../lib/utils');
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 9122909

Please sign in to comment.