Skip to content

Commit

Permalink
ISO Date typed interface (#66)
Browse files Browse the repository at this point in the history
* 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
janekamata and Jaren Adams authored Jan 30, 2025
1 parent a91bc98 commit fa05bd1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
27 changes: 27 additions & 0 deletions backend/src/utils/date.ts
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;
34 changes: 34 additions & 0 deletions backend/tests/date.test.ts
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)?$/);
});
});
2 changes: 1 addition & 1 deletion frontend/tests/Mock.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Account component", () => {

// Act 1
render(
<MemoryRouter>
<MemoryRouter>
<AuthProvider>
<Account />
</AuthProvider>
Expand Down

0 comments on commit fa05bd1

Please sign in to comment.