From 8d0f32d7c31a0e31dc8e363401b369284429023e Mon Sep 17 00:00:00 2001 From: Aral Roca Date: Wed, 22 Nov 2023 11:33:16 +0100 Subject: [PATCH] fix date object --- package.json | 2 +- src/index.test.ts | 16 +++++++++++++++- src/index.ts | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 82baa43..a7b48a0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.test.ts b/src/index.test.ts index 0af0bae..6a0fc2a 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -313,6 +313,10 @@ 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 = { @@ -320,11 +324,21 @@ describe("defaultComposer", () => { anotherDate: new Date(2020, 2, 1), }; + const output = defaultComposer(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(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); }); }); diff --git a/src/index.ts b/src/index.ts index 2be792b..f0e7f4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,7 +68,7 @@ function compose(defaults: Partial, obj: Partial): Partial { } 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(object: T): boolean {