Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#94] Support for cloning an Empty class instance from fast-querystring #95

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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