Skip to content

Commit d764c08

Browse files
rdmurphylukeed
andauthored
fix(diff): print BigInts within JSON correctly (#186)
* Detect BigInt within circular() and properly convert them within stringify * narrower bigint check * update test expectants * fix: avoid bigint syntax errors in Node8 Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
1 parent f735f5a commit d764c08

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/diff.js

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export function circular() {
181181
return function print(key, val) {
182182
if (val === void 0) return '[__VOID__]';
183183
if (typeof val === 'number' && val !== val) return '[__NAN__]';
184+
if (typeof val === 'bigint') return val.toString();
184185
if (!val || typeof val !== 'object') return val;
185186
if (cache.has(val)) return '[Circular]';
186187
cache.add(val); return val;

test/diff.js

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { suite } from 'uvu';
22
import * as assert from 'uvu/assert';
33
import * as $ from '../src/diff';
44

5+
const isNode8 = process.versions.node.startsWith('8.');
56
const strip = str => str.replace(/[\u001B\u009B][[\]()#;?]*(?:(?:(?:[a-zA-Z\d]*(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-ntqry=><~]))/g, '');
67

78
const arrays = suite('arrays');
@@ -1167,4 +1168,17 @@ stringify('should retain `undefined` and `NaN` values :: Array', () => {
11671168
);
11681169
});
11691170

1171+
if (!isNode8) {
1172+
// Not currently supporting :: Object(BigInt(3)) && Object(4n)
1173+
stringify('should handle `BigInt` values correctly', () => {
1174+
let bigint = eval('100n'); // avoid Node8 syntax error
1175+
assert.is($.stringify(BigInt(1)), '"1"');
1176+
assert.is($.stringify(bigint), '"100"');
1177+
assert.is(
1178+
$.stringify([BigInt(1), bigint]),
1179+
'[\n "1",\n "100"\n]'
1180+
);
1181+
});
1182+
}
1183+
11701184
stringify.run();

0 commit comments

Comments
 (0)