From 169e06d83cef5116f0e39c74d2c497f04ba6278d Mon Sep 17 00:00:00 2001 From: Bart Krakowski <bartlomiej@krakowski.dev> Date: Wed, 29 May 2024 16:55:35 +0200 Subject: [PATCH 1/5] refactor: update isValidDate return type for improved type safety --- packages/time/src/tests/isValidDate.test.ts | 38 +++++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/time/src/tests/isValidDate.test.ts b/packages/time/src/tests/isValidDate.test.ts index 57a3556..188c31c 100644 --- a/packages/time/src/tests/isValidDate.test.ts +++ b/packages/time/src/tests/isValidDate.test.ts @@ -1,16 +1,32 @@ -import {describe, expect, test} from 'vitest'; -import {isValidDate} from '../utils/isValidDate'; +import { describe, expect, test } from 'vitest' +import { isValidDate } from '../utils/isValidDate' describe('isValidDate', () => { test('should return true for a valid date', () => { - expect(isValidDate(new Date())).toBe(true); - }); + const date = new Date(); + expect(isValidDate(date)).toBe(true) + }) - test('should return false for an invalid date', () => { - expect(isValidDate(new Date("invalid"))).toBe(false); - }); + test.each([ + '2021-10-10', + new Date('invalid'), + {}, + undefined, + null, + NaN, + 0, + ])('should return false for invalid date %p', (date) => { + expect(isValidDate(date)).toBe(false) + }) - test("should return false for null", () => { - expect(isValidDate(null)).toBe(false); - }); -}); \ No newline at end of file + test('should assert type guards correctly', () => { + const notADate = 'not a date'; + if (isValidDate(notADate)) { + expect(notADate).toBeInstanceOf(Date) + notADate.getDate() + } else { + // @ts-expect-error + notADate.getTime() + } + }) +}) From 75cb7b331a5c2ee41263f45509378197e9645b60 Mon Sep 17 00:00:00 2001 From: Bart Krakowski <bartlomiej@krakowski.dev> Date: Wed, 29 May 2024 16:55:47 +0200 Subject: [PATCH 2/5] refactor: update isValidDate return type for improved type safety --- packages/time/src/utils/isValidDate.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/time/src/utils/isValidDate.ts b/packages/time/src/utils/isValidDate.ts index 987df31..c0d1599 100644 --- a/packages/time/src/utils/isValidDate.ts +++ b/packages/time/src/utils/isValidDate.ts @@ -4,9 +4,6 @@ * @param date Date * @returns boolean */ -export function isValidDate(date: any): boolean { - if (Object.prototype.toString.call(date) !== '[object Date]') { - return false; - } - return date.getTime() === date.getTime(); -} \ No newline at end of file +export function isValidDate(date: unknown): date is Date { + return date instanceof Date && !isNaN(date.getTime()); +} From 5dca8d5e763a5e7cb2879ae836fec9cd16070a89 Mon Sep 17 00:00:00 2001 From: Bart Krakowski <bartlomiej@krakowski.dev> Date: Thu, 30 May 2024 17:52:05 +0200 Subject: [PATCH 3/5] refactor: update isValidDate return type for improved type safety --- packages/time/src/tests/isValidDate.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/time/src/tests/isValidDate.test.ts b/packages/time/src/tests/isValidDate.test.ts index 188c31c..4d01970 100644 --- a/packages/time/src/tests/isValidDate.test.ts +++ b/packages/time/src/tests/isValidDate.test.ts @@ -25,8 +25,10 @@ describe('isValidDate', () => { expect(notADate).toBeInstanceOf(Date) notADate.getDate() } else { - // @ts-expect-error - notADate.getTime() + expect(() => { + // @ts-expect-error + notADate.getTime() + }).toThrowError() } }) }) From e9e4be9c36c35283dded374d5475822fd888d203 Mon Sep 17 00:00:00 2001 From: Bart Krakowski <bartlomiej@krakowski.dev> Date: Thu, 30 May 2024 22:29:49 +0200 Subject: [PATCH 4/5] revert: isValidDate util --- packages/time/src/tests/isValidDate.test.ts | 38 ++++++--------------- packages/time/src/utils/isValidDate.ts | 9 +++-- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/packages/time/src/tests/isValidDate.test.ts b/packages/time/src/tests/isValidDate.test.ts index 4d01970..01afdc6 100644 --- a/packages/time/src/tests/isValidDate.test.ts +++ b/packages/time/src/tests/isValidDate.test.ts @@ -1,34 +1,16 @@ -import { describe, expect, test } from 'vitest' -import { isValidDate } from '../utils/isValidDate' +import {describe, expect, test} from 'vitest'; +import {isValidDate} from '../utils/isValidDate'; describe('isValidDate', () => { test('should return true for a valid date', () => { - const date = new Date(); - expect(isValidDate(date)).toBe(true) + expect(isValidDate(new Date())).toBe(true); }) - test.each([ - '2021-10-10', - new Date('invalid'), - {}, - undefined, - null, - NaN, - 0, - ])('should return false for invalid date %p', (date) => { - expect(isValidDate(date)).toBe(false) - }) + test('should return false for an invalid date', () => { + expect(isValidDate(new Date("invalid"))).toBe(false); + }); - test('should assert type guards correctly', () => { - const notADate = 'not a date'; - if (isValidDate(notADate)) { - expect(notADate).toBeInstanceOf(Date) - notADate.getDate() - } else { - expect(() => { - // @ts-expect-error - notADate.getTime() - }).toThrowError() - } - }) -}) + test("should return false for null", () => { + expect(isValidDate(null)).toBe(false); + }); +}); diff --git a/packages/time/src/utils/isValidDate.ts b/packages/time/src/utils/isValidDate.ts index c0d1599..987df31 100644 --- a/packages/time/src/utils/isValidDate.ts +++ b/packages/time/src/utils/isValidDate.ts @@ -4,6 +4,9 @@ * @param date Date * @returns boolean */ -export function isValidDate(date: unknown): date is Date { - return date instanceof Date && !isNaN(date.getTime()); -} +export function isValidDate(date: any): boolean { + if (Object.prototype.toString.call(date) !== '[object Date]') { + return false; + } + return date.getTime() === date.getTime(); +} \ No newline at end of file From b4511373b7358790690e85009d1c0762409adc04 Mon Sep 17 00:00:00 2001 From: Bart Krakowski <bartlomiej@krakowski.dev> Date: Sat, 1 Jun 2024 20:19:40 +0200 Subject: [PATCH 5/5] fix: error handling --- packages/time/src/tests/parse.test.ts | 6 +++--- packages/time/src/utils/parse.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/time/src/tests/parse.test.ts b/packages/time/src/tests/parse.test.ts index 4ee0522..ededa27 100644 --- a/packages/time/src/tests/parse.test.ts +++ b/packages/time/src/tests/parse.test.ts @@ -102,11 +102,11 @@ describe('parse', () => { }); test('should throw an error for an invalid date string', () => { - expect(() => parse('2021-15-37')).toThrowError('"2021-15-37" is an invalid RFC339 Internet Date Time string'); + expect(() => parse('2021-15-37')).toThrowError('"2021-15-37" is an invalid RFC3339 Internet Date Time string'); }); test('should throw an error for an invalid time string', () => { - expect(() => parse('27:62')).toThrowError('"27:62" is an invalid RFC339 Internet Date Time string'); + expect(() => parse('27:62')).toThrowError('"27:62" is an invalid RFC3339 Internet Date Time string'); }); test('should throw an error for an invalid time string', () => { @@ -123,7 +123,7 @@ describe('parse', () => { }); test('should throw an error for an invalid date string', () => { - expect(() => parse('2021-01-01T00:00:00.000')).toThrowError('"2021-01-01T00:00:00.000" is an invalid RFC339 Internet Date Time string'); + expect(() => parse('2021-01-01T00:00:00.000')).toThrowError('"2021-01-01T00:00:00.000" is an invalid RFC3339 Internet Date Time string'); }); test('should throw an error for an invalid time string', () => { diff --git a/packages/time/src/utils/parse.ts b/packages/time/src/utils/parse.ts index 265021c..8d610f5 100644 --- a/packages/time/src/utils/parse.ts +++ b/packages/time/src/utils/parse.ts @@ -71,7 +71,7 @@ function parseDateTimeString(value: string): Date { `${year}-${month}-${day}T${hour}:${minute}:${second}.${millisecond}${timezone}`, ); } - throw new Error(`"${value}" is an invalid RFC339 Internet Date Time string`); + throw new Error(`"${value}" is an invalid RFC3339 Internet Date Time string`); } /**