Skip to content

Commit

Permalink
fix instance of classes (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Nov 22, 2023
1 parent 8d0f32d commit 5e40e91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,27 @@ describe("defaultComposer", () => {
expect(output.range.startDate).toBeInstanceOf(Date);
expect(output.range.endDate).toBeInstanceOf(Date);
});

it('should respect instance of any class', () => {
class TestClass {
constructor(public name: string) { }
}

const defaults = {
test: new TestClass('test'),
};

const object = {
test: new TestClass('test2'),
};

const output = defaultComposer<any>(defaults, object);

const expected = {
test: new TestClass('test2'),
};

expect(output).toEqual(expected);
expect(output.test).toBeInstanceOf(TestClass);
});
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function compose<T>(defaults: Partial<T>, obj: Partial<T>): Partial<T> {
}

function isObject(value: any): boolean {
return typeof value === "object" && value !== null && !(value instanceof Date) && !Array.isArray(value);
return typeof value === "object" && value !== null && value.constructor === Object && !Array.isArray(value);
}

function isEmptyObjectOrArray<T>(object: T): boolean {
Expand Down

0 comments on commit 5e40e91

Please sign in to comment.