diff --git a/__tests__/utils.ts b/__tests__/utils.ts index 8be9289..54c3d64 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -56,7 +56,7 @@ describe('createCache', () => { }); describe('getCleanClone', () => { - it('will return a pure object when there is no constructor', () => { + it('will return a pure object when there is no prototype', () => { const object = Object.create(null); const result = utils.getCleanClone(Object.getPrototypeOf(object)); @@ -67,6 +67,23 @@ describe('getCleanClone', () => { expect(Object.getPrototypeOf(result)).toBe(null); }); + it('will return a pure object when there is a prototype but no constructor', () => { + const Empty = function () { + // empty + }; + Empty.prototype = Object.create(null); + + // @ts-expect-error - Testing `fast-querystring` V8 optimization + const object = new Empty(); + + const result = utils.getCleanClone(Object.getPrototypeOf(object)); + + expect(result).not.toBe(object); + expect(result).toEqual(object); + + expect(Object.getPrototypeOf(result)).toBe(Empty.prototype); + }); + it('will return a pure object when there is no __proto__ property', () => { const object: PlainObject = {}; diff --git a/src/utils.ts b/src/utils.ts index d03116a..075d23c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -57,7 +57,10 @@ export function getCleanClone(prototype: any): any { return prototype === Object.prototype ? {} : create(prototype); } - if (~toStringFunction.call(Constructor).indexOf('[native code]')) { + if ( + Constructor && + ~toStringFunction.call(Constructor).indexOf('[native code]') + ) { try { return new Constructor(); } catch {}