Skip to content
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
40 changes: 40 additions & 0 deletions utils/dateRange.empty-fallback.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it } from 'vitest';
import { formatDateRange, DEFAULT_DATE_RANGE } from './dateRange';

describe('formatDateRange - Edge Cases & Empty/Missing Inputs Verification', () => {
it('1. falls back to default date range when year is completely omitted', () => {
expect(formatDateRange()).toEqual(DEFAULT_DATE_RANGE);
});

it('2. falls back to default date range when year is undefined', () => {
expect(formatDateRange(undefined)).toEqual(DEFAULT_DATE_RANGE);
});

it('3. falls back to default date range when year is null', () => {
expect(formatDateRange(null as any)).toEqual(DEFAULT_DATE_RANGE);
});

it('4. falls back to default date range when year is an empty or whitespace string', () => {
expect(formatDateRange('')).toEqual(DEFAULT_DATE_RANGE);
expect(formatDateRange(' ')).toEqual(DEFAULT_DATE_RANGE);
});

it('5. processes numerical years correctly without throwing', () => {
expect(formatDateRange(2024)).toEqual({
from: '2024-01-01T00:00:00Z',
to: '2024-12-31T23:59:59Z',
});

expect(formatDateRange(24)).toEqual({
from: '2024-01-01T00:00:00Z',
to: '2024-12-31T23:59:59Z',
});
});

it('6. falls back to default date range when year resolves to invalid numeric bounds or NaN', () => {
expect(formatDateRange(NaN as any)).toEqual(DEFAULT_DATE_RANGE);
expect(formatDateRange(false as any)).toEqual(DEFAULT_DATE_RANGE);
expect(formatDateRange({} as any)).toEqual(DEFAULT_DATE_RANGE);
});
});
9 changes: 6 additions & 3 deletions utils/dateRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ export interface DateRange {
* formatDateRange("") // returns DEFAULT_DATE_RANGE
* formatDateRange(undefined) // returns DEFAULT_DATE_RANGE
*/
export function formatDateRange(year?: string): DateRange {
if (!year || year.trim() === '') {
export function formatDateRange(year?: string | number | null): DateRange {
if (year === undefined || year === null) {
return { ...DEFAULT_DATE_RANGE };
}

const trimmedYear = year.trim();
const trimmedYear = String(year).trim();
if (trimmedYear === '') {
return { ...DEFAULT_DATE_RANGE };
}

// Handle partial years (1 or 2 digits): assume 20xx
// For 2-digit: '24' -> 2024
Expand Down
Loading