Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISO Date typed interface #66

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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