Skip to content

Commit

Permalink
add support for cloning an Empty class instance from `fast-querystr…
Browse files Browse the repository at this point in the history
…ing` (#95)
  • Loading branch information
planttheidea committed Mar 16, 2024
1 parent ae0d587 commit 5606957
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion __tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 = {};

Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down

0 comments on commit 5606957

Please sign in to comment.