From b690ed21af83621c1c3e99d0b47772cbd692f1ff Mon Sep 17 00:00:00 2001 From: KATT Date: Tue, 20 Jul 2021 00:25:18 +0200 Subject: [PATCH 1/2] add failing test --- src/index.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/index.test.ts b/src/index.test.ts index fd162e8..2fdce8c 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -936,3 +936,13 @@ test('regression #108: Error#stack should not be included by default', () => { ) as any; expect(thatShouldExist).toEqual(input.stack); }); + +test('regression: `Object.create(null)` / object without prototype', () => { + const input: Record = Object.create(null); + input.date = new Date(); + + const stringified = SuperJSON.stringify(input); + const parsed: any = SuperJSON.parse(stringified); + + expect(parsed.date).toBeInstanceOf(Date); +}); From c5ebb655572cdb21015db46b3b42b080f518e63a Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 27 Jul 2021 10:03:49 +0200 Subject: [PATCH 2/2] consider null-prototype object a "plain" object --- src/is.test.ts | 6 ++++++ src/is.ts | 1 + 2 files changed, 7 insertions(+) diff --git a/src/is.test.ts b/src/is.test.ts index 54c8ade..c94b22f 100644 --- a/src/is.test.ts +++ b/src/is.test.ts @@ -9,6 +9,7 @@ import { isString, isSymbol, isUndefined, + isPlainObject, } from './is'; test('Basic true tests', () => { @@ -68,3 +69,8 @@ test('Primitive tests', () => { test('Date exception', () => { expect(isDate(new Date('_'))).toBe(false); }); + +test('Regression: null-prototype object', () => { + expect(isPlainObject(Object.create(null))).toBe(true); + expect(isPrimitive(Object.create(null))).toBe(false); +}); diff --git a/src/is.ts b/src/is.ts index 9fceb9f..010c10f 100644 --- a/src/is.ts +++ b/src/is.ts @@ -10,6 +10,7 @@ export const isPlainObject = ( payload: any ): payload is { [key: string]: any } => { if (getType(payload) !== 'Object') return false; + if (Object.getPrototypeOf(payload) === null) return true; return ( payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype