Skip to content

Commit

Permalink
fix date object
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Nov 22, 2023
1 parent 709b3dd commit 8d0f32d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "default-composer",
"version": "0.5.2",
"version": "0.5.3",
"description": "A JavaScript library that allows you to set default values for nested objects",
"main": "dist/index.js",
"umd:main": "dist/index.umd.js",
Expand Down
16 changes: 15 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,32 @@ describe("defaultComposer", () => {
it('should respect the Date object', () => {
const defaults = {
date: new Date(),
range: {
startDate: new Date(2020, 1, 1),
endDate: new Date(2020, 1, 2),
}
};

const object = {
date: new Date(2020, 1, 1),
anotherDate: new Date(2020, 2, 1),
};

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

const expected = {
date: new Date(2020, 1, 1),
anotherDate: new Date(2020, 2, 1),
range: {
startDate: new Date(2020, 1, 1),
endDate: new Date(2020, 1, 2),
}
};

expect(defaultComposer<any>(defaults, object)).toEqual(expected);
expect(output).toEqual(expected);
expect(output.date).toBeInstanceOf(Date);
expect(output.anotherDate).toBeInstanceOf(Date);
expect(output.range.startDate).toBeInstanceOf(Date);
expect(output.range.endDate).toBeInstanceOf(Date);
});
});
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 && !Array.isArray(value);
return typeof value === "object" && value !== null && !(value instanceof Date) && !Array.isArray(value);
}

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

0 comments on commit 8d0f32d

Please sign in to comment.