Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(normalize): Treat Infinity as NaN both are non-serializable numbers #13406

Merged
merged 9 commits into from
Sep 12, 2024
10 changes: 9 additions & 1 deletion packages/utils/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function visit(
// Get the simple cases out of the way first
if (
value == null || // this matches null and undefined -> eqeq not eqeqeq
(['number', 'boolean', 'string'].includes(typeof value) && !Number.isNaN(value))
(['number', 'boolean', 'string'].includes(typeof value) && Number.isFinite(value))
) {
return value as Primitive;
}
Expand Down Expand Up @@ -224,6 +224,14 @@ function stringifyValue(
return '[NaN]';
}

if (typeof value === 'number' && value === Infinity) {
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
return '[Infinity]';
}

if (typeof value === 'number' && value === -Infinity) {
return '[-Infinity]';
}

if (typeof value === 'function') {
return `[Function: ${getFunctionName(value)}]`;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ describe('normalize()', () => {
describe('changes unserializeable/global values/classes to their respective string representations', () => {
test('primitive values', () => {
expect(normalize(NaN)).toEqual('[NaN]');
expect(normalize(Infinity)).toEqual('[Infinity]');
expect(normalize(-Infinity)).toEqual('[-Infinity]');
expect(normalize(Symbol('dogs'))).toEqual('[Symbol(dogs)]');
expect(normalize(BigInt(1121201212312012))).toEqual('[BigInt: 1121201212312012]');
});
Expand Down
Loading