Repository: blitz-js/superjson
Pull request #145 (2021-07-19 22:26:08)

support serializing objects without prototype
failing test for #146


Patch:
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
diff --git a/yarn.lock b/yarn.lock
index 6c353b6..ff941e6 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3304,9 +3304,9 @@ getpass@^0.1.1:
     assert-plus "^1.0.0"
 
 glob-parent@^5.0.0, glob-parent@^5.1.0:
-  version "5.1.1"
-  resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.1.tgz#b6c1ef417c4e5663ea498f1c45afac6916bbc229"
-  integrity sha512-FnI+VGOpnlGHWZxthPGR+QhR78fuiK0sNLkHQv+bL9fQi57lNNdquIbna/WrfROrolq8GK5Ek6BiMwqL/voRYQ==
+  version "5.1.2"
+  resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
+  integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==
   dependencies:
     is-glob "^4.0.1"
 
@@ -3668,7 +3668,7 @@ is-extendable@^1.0.1:
 
 is-extglob@^2.1.1:
   version "2.1.1"
-  resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz"
+  resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
   integrity sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=
 
 is-fullwidth-code-point@^2.0.0:
@@ -3688,7 +3688,7 @@ is-generator-fn@^2.0.0:
 
 is-glob@^4.0.0, is-glob@^4.0.1:
   version "4.0.1"
-  resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.1.tgz"
+  resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.1.tgz#7567dbe9f2f5e2467bc77ab83c4a29482407a5dc"
   integrity sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==
   dependencies:
     is-extglob "^2.1.1"


Test patch:
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<string, unknown> = Object.create(null);
+  input.date = new Date();
+
+  const stringified = SuperJSON.stringify(input);
+  const parsed: any = SuperJSON.parse(stringified);
+
+  expect(parsed.date).toBeInstanceOf(Date);
+});
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);
+});