Skip to content

Commit b0434e2

Browse files
fix(normalize): Treat Infinity as NaN both are non-serializable numbers
1 parent 615c670 commit b0434e2

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

packages/utils/src/normalize.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function visit(
8181
// Get the simple cases out of the way first
8282
if (
8383
value == null || // this matches null and undefined -> eqeq not eqeqeq
84-
(['number', 'boolean', 'string'].includes(typeof value) && !Number.isNaN(value))
84+
(['number', 'boolean', 'string'].includes(typeof value) && Number.isFinite(value))
8585
) {
8686
return value as Primitive;
8787
}
@@ -224,6 +224,14 @@ function stringifyValue(
224224
return '[NaN]';
225225
}
226226

227+
if (typeof value === 'number' && value === Infinity) {
228+
return '[Infinity]';
229+
}
230+
231+
if (typeof value === 'number' && value === -Infinity) {
232+
return '[-Infinity]';
233+
}
234+
227235
if (typeof value === 'function') {
228236
return `[Function: ${getFunctionName(value)}]`;
229237
}

packages/utils/test/normalize.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ describe('normalize()', () => {
403403
describe('changes unserializeable/global values/classes to their respective string representations', () => {
404404
test('primitive values', () => {
405405
expect(normalize(NaN)).toEqual('[NaN]');
406+
expect(normalize(Infinity)).toEqual('[Infinity]');
407+
expect(normalize(-Infinity)).toEqual('[-Infinity]');
406408
expect(normalize(Symbol('dogs'))).toEqual('[Symbol(dogs)]');
407409
expect(normalize(BigInt(1121201212312012))).toEqual('[BigInt: 1121201212312012]');
408410
});

0 commit comments

Comments
 (0)