Skip to content

Commit 5606957

Browse files
authored
add support for cloning an Empty class instance from fast-querystring (#95)
1 parent ae0d587 commit 5606957

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

__tests__/utils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('createCache', () => {
5656
});
5757

5858
describe('getCleanClone', () => {
59-
it('will return a pure object when there is no constructor', () => {
59+
it('will return a pure object when there is no prototype', () => {
6060
const object = Object.create(null);
6161

6262
const result = utils.getCleanClone(Object.getPrototypeOf(object));
@@ -67,6 +67,23 @@ describe('getCleanClone', () => {
6767
expect(Object.getPrototypeOf(result)).toBe(null);
6868
});
6969

70+
it('will return a pure object when there is a prototype but no constructor', () => {
71+
const Empty = function () {
72+
// empty
73+
};
74+
Empty.prototype = Object.create(null);
75+
76+
// @ts-expect-error - Testing `fast-querystring` V8 optimization
77+
const object = new Empty();
78+
79+
const result = utils.getCleanClone(Object.getPrototypeOf(object));
80+
81+
expect(result).not.toBe(object);
82+
expect(result).toEqual(object);
83+
84+
expect(Object.getPrototypeOf(result)).toBe(Empty.prototype);
85+
});
86+
7087
it('will return a pure object when there is no __proto__ property', () => {
7188
const object: PlainObject = {};
7289

src/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ export function getCleanClone(prototype: any): any {
5757
return prototype === Object.prototype ? {} : create(prototype);
5858
}
5959

60-
if (~toStringFunction.call(Constructor).indexOf('[native code]')) {
60+
if (
61+
Constructor &&
62+
~toStringFunction.call(Constructor).indexOf('[native code]')
63+
) {
6164
try {
6265
return new Constructor();
6366
} catch {}

0 commit comments

Comments
 (0)