diff --git a/packages/time/src/tests/isValidDate.test.ts b/packages/time/src/tests/isValidDate.test.ts index 57a3556..4d01970 100644 --- a/packages/time/src/tests/isValidDate.test.ts +++ b/packages/time/src/tests/isValidDate.test.ts @@ -1,16 +1,34 @@ -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 { + expect(() => { + // @ts-expect-error + notADate.getTime() + }).toThrowError() + } + }) +}) diff --git a/packages/time/src/tests/parse.test.ts b/packages/time/src/tests/parse.test.ts index 4ee0522..d8bd6ca 100644 --- a/packages/time/src/tests/parse.test.ts +++ b/packages/time/src/tests/parse.test.ts @@ -1,5 +1,5 @@ -import {describe, expect, test} from 'vitest'; -import {parse} from '../utils/parse'; +import { describe, expect, test } from 'vitest' +import { parse } from '../utils/parse' const dateTimeFormat = new Intl.DateTimeFormat('en-US', { year: 'numeric', @@ -9,124 +9,136 @@ const dateTimeFormat = new Intl.DateTimeFormat('en-US', { minute: '2-digit', second: '2-digit', timeZoneName: 'short', - timeZone: 'America/New_York' -}); + timeZone: 'America/New_York', +}) const timeOnlyFormat = new Intl.DateTimeFormat('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit', //timeZoneName: 'short', - timeZone: 'America/New_York' -}); + timeZone: 'America/New_York', +}) describe('parse', () => { test('should parse a valid year only date string', () => { - const date = parse('2021'); - expect(dateTimeFormat.format(date)).toBe('01/01/2021, 12:00:00 AM EST'); - }); + const date = parse('2021') + expect(dateTimeFormat.format(date)).toBe('01/01/2021, 12:00:00 AM EST') + }) test('should parse a valid year and month only date string', () => { - const date = parse('2021-03'); - expect(dateTimeFormat.format(date)).toBe('03/01/2021, 12:00:00 AM EST'); - }); + const date = parse('2021-03') + expect(dateTimeFormat.format(date)).toBe('03/01/2021, 12:00:00 AM EST') + }) test('should parse a valid year, month and day only date string', () => { - const date = parse('2021-03-12'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 12:00:00 AM EST'); - }); + const date = parse('2021-03-12') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 12:00:00 AM EST') + }) test('should cast date only string to UTC', () => { - const date = parse('2021-03-12TZ'); - expect(dateTimeFormat.format(date)).toBe('03/11/2021, 07:00:00 PM EST'); - }); + const date = parse('2021-03-12TZ') + expect(dateTimeFormat.format(date)).toBe('03/11/2021, 07:00:00 PM EST') + }) test('should cast date only string to offset', () => { - const date = parse('2021-03-12T-07:00'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:00:00 AM EST'); - }); + const date = parse('2021-03-12T-07:00') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:00:00 AM EST') + }) test('should parse a valid year, month, day and hour only date/time string with separator', () => { - const date = parse('2021-03-12T14'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:00:00 PM EST'); - }); + const date = parse('2021-03-12T14') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:00:00 PM EST') + }) test('should parse a valid year, month, day and hour only date/time string without separator', () => { - const date = parse('2021-03-12 16'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 04:00:00 PM EST'); - }); + const date = parse('2021-03-12 16') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 04:00:00 PM EST') + }) test('should parse a valid year, month, day, hour and minute only date/time string', () => { - const date = parse('2021-03-12T14:42'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:42:00 PM EST'); - }); + const date = parse('2021-03-12T14:42') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:42:00 PM EST') + }) test('should parse a valid year, month, day, hour, minute and seconds only date/time string', () => { - const date = parse('2021-03-12T14:42:12'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:42:12 PM EST'); - }); + const date = parse('2021-03-12T14:42:12') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:42:12 PM EST') + }) test('should parse a valid UTC date/time string', () => { - const date = parse('2021-03-12T14:42:12Z'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 09:42:12 AM EST'); - }); + const date = parse('2021-03-12T14:42:12Z') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 09:42:12 AM EST') + }) test('should parse a valid UTC date/time string with lower case separator and zulu', () => { - const date = parse('2021-03-12t14:42:12z'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 09:42:12 AM EST'); - }); + const date = parse('2021-03-12t14:42:12z') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 09:42:12 AM EST') + }) test('should parse a valid date/time string with offset', () => { - const date = parse('2021-03-12T14:42:12+09:00'); - expect(dateTimeFormat.format(date)).toBe('03/12/2021, 12:42:12 AM EST'); - }); + const date = parse('2021-03-12T14:42:12+09:00') + expect(dateTimeFormat.format(date)).toBe('03/12/2021, 12:42:12 AM EST') + }) test('should parse a valid epoch date/time', () => { - const date = parse(1711639239717); - expect(dateTimeFormat.format(date)).toBe('03/28/2024, 11:20:39 AM EDT'); - }); + const date = parse(1711639239717) + expect(dateTimeFormat.format(date)).toBe('03/28/2024, 11:20:39 AM EDT') + }) test('should parse a valid hour and minute time string', () => { - const date = parse('14:00'); - expect(timeOnlyFormat.format(date)).toBe('02:00:00 PM'); - }); + const date = parse('14:00') + expect(timeOnlyFormat.format(date)).toBe('02:00:00 PM') + }) test('should parse a valid hour, minute and second time string', () => { - const date = parse('14:14:34'); - expect(timeOnlyFormat.format(date)).toBe('02:14:34 PM'); - }); + const date = parse('14:14:34') + expect(timeOnlyFormat.format(date)).toBe('02:14:34 PM') + }) test('should parse a valid hour, minute and second time string with meridiem', () => { - const date = parse('08:14:34 PM'); - expect(timeOnlyFormat.format(date)).toBe('08:14:34 PM'); - }); + const date = parse('08:14:34 PM') + expect(timeOnlyFormat.format(date)).toBe('08:14:34 PM') + }) 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 RFC339 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 RFC339 Internet Date Time string', + ) + }) test('should throw an error for an invalid time string', () => { - expect(() => parse('14:12 PM')).toThrowError('"14:12 PM" is an invalid time string'); - }); + expect(() => parse('14:12 PM')).toThrowError( + '"14:12 PM" is an invalid time string', + ) + }) test('should throw an error for an invalid epoch', () => { - expect(() => parse(9274309587123413)).toThrowError('"9274309587123412" is an invalid epoch date value'); - }); - - /* test('should parse a valid time string', () => { - const date = parse('00:00:00.000Z'); - expect(dateTimeFormat.format(date)).toBe('12/31/2020, 7:00:00 PM EST'); - }); - - 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'); - }); - - test('should throw an error for an invalid time string', () => { - expect(() => parse('00:00:00.000')).toThrowError('"00:00:00.000" is an invalid time string'); - }); */ -}); \ No newline at end of file + expect(() => parse(9274309587123410)).toThrowError( + '"9274309587123410" is an invalid epoch date value', + ) + }) + + test.skip('should parse a valid time string', () => { + const date = parse('00:00:00.000Z') + expect(dateTimeFormat.format(date)).toBe('12/31/2020, 7:00:00 PM EST') + }) + + test.skip('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', + ) + }) + + test.skip('should throw an error for an invalid time string', () => { + expect(() => parse('00:00:00.000')).toThrowError( + '"00:00:00.000" is an invalid time string', + ) + }) +}) 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()); +}