-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added navigation controls from account settings to grant list; changed dashboard to account * [JAN-1] Notifications base layer wrap-up * [JAN-11] Add navigation controls to go from the "dashboard" < -- > grant list * [JAN-1] Notifications base layer wrap-up * Created typed interface for ISO dates and tested --------- Co-authored-by: Jaren Adams <[email protected]>
- Loading branch information
1 parent
a91bc98
commit fa05bd1
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Extending the built-in Date interface to give a precise return type to toISOString | ||
declare global { | ||
interface Date { | ||
toISOString(): TDateISO; | ||
} | ||
} | ||
|
||
// Type aliases for different parts of a date/time string | ||
type TYear = `${number}${number}${number}${number}`; | ||
type TMonth = `${number}${number}`; | ||
type TDay = `${number}${number}`; | ||
type THours = `${number}${number}`; | ||
type TMinutes = `${number}${number}`; | ||
type TSeconds = `${number}${number}`; | ||
type TMilliseconds = `${number}${number}${number}`; | ||
|
||
// Represent a string like `2021-01-08` (date only) | ||
type TDateISODate = `${TYear}-${TMonth}-${TDay}`; | ||
|
||
// Represent a string like `14:42:34.678` (time part) | ||
type TDateISOTime = `${THours}:${TMinutes}:${TSeconds}.${TMilliseconds}`; | ||
|
||
// Represent a string like `2021-01-08T14:42:34.678Z` (full ISO 8601 format) | ||
type TDateISOFull = `${TDateISODate}T${TDateISOTime}Z`; | ||
|
||
// Expects either the full date-time format or date-only format | ||
export type TDateISO = TDateISOFull | TDateISODate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { TDateISO } from '../src/utils/date'; // Import the type | ||
|
||
// Helper function to check if a string matches the YYYY-MM-DD or YYYY-MM-DDTHH:mm:ss.sssZ format | ||
const isDateISO = (date: string): boolean => { | ||
// Regex for the format YYYY-MM-DD or YYYY-MM-DDTHH:mm:ss.sssZ | ||
const regex = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)$/; | ||
return regex.test(date); | ||
}; | ||
|
||
describe('Date ISO Format', () => { | ||
it('should match the expected date format (YYYY-MM-DD)', () => { | ||
const date: TDateISO = '2025-01-28'; // YYYY-MM-DD | ||
expect(isDateISO(date)).toBe(true); | ||
}); | ||
|
||
it('should match the expected date-time format (YYYY-MM-DDTHH:mm:ss.sssZ)', () => { | ||
const date: TDateISO = '2025-01-28T14:42:34.678Z'; // YYYY-MM-DDTHH:mm:ss.sssZ | ||
expect(isDateISO(date)).toBe(true); | ||
}); | ||
|
||
it('should match the correct format for `toISOString()` method', () => { | ||
const date = new Date(); | ||
const isoString: TDateISO = date.toISOString(); | ||
expect(isDateISO(isoString)).toBe(true); | ||
}); | ||
|
||
it('should return a string in the correct format from `toISOString()`', () => { | ||
const date = new Date(); | ||
const isoString = date.toISOString(); | ||
// Check if the ISO string matches either of the two formats | ||
expect(isoString).toMatch(/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}\.\d{3}Z)?$/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters