Refactor date parsing function and improve test coverage

Renamed the `parseWithRegex` function to `parseDateWithRegex` for clarity and consistency. Added a blank line in the `toISO` test to enhance readability. These changes aim to improve code maintainability and ensure better test organization.
This commit is contained in:
2025-10-08 19:43:01 -04:00
parent 9661db0b46
commit 15e807f093
2 changed files with 6 additions and 2 deletions
+1
View File
@@ -18,6 +18,7 @@ test("toISO parses ISO and common whois formats", () => {
const mdy = toISO("Jan 02 2023"); const mdy = toISO("Jan 02 2023");
expect(mdy).toBe("2023-01-02T00:00:00Z"); expect(mdy).toBe("2023-01-02T00:00:00Z");
// Registrar style timezone offsets // Registrar style timezone offsets
const plus0000 = toISO("2025-03-23T10:53:03+0000"); const plus0000 = toISO("2025-03-23T10:53:03+0000");
expect(plus0000).toBe("2025-03-23T10:53:03Z"); expect(plus0000).toBe("2025-03-23T10:53:03Z");
+5 -2
View File
@@ -22,7 +22,7 @@ export function toISO(
for (const re of tryFormats) { for (const re of tryFormats) {
const m = raw.match(re); const m = raw.match(re);
if (!m) continue; if (!m) continue;
const d = parseWithRegex(m, re); const d = parseDateWithRegex(m, re);
if (d) return toIsoFromDate(d); if (d) return toIsoFromDate(d);
} }
// Fallback to native Date parsing (handles ISO and RFC2822 with TZ) // Fallback to native Date parsing (handles ISO and RFC2822 with TZ)
@@ -51,7 +51,10 @@ function toIsoFromDate(d: Date): string | undefined {
} }
} }
function parseWithRegex(m: RegExpMatchArray, _re: RegExp): Date | undefined { function parseDateWithRegex(
m: RegExpMatchArray,
_re: RegExp,
): Date | undefined {
const monthMap: Record<string, number> = { const monthMap: Record<string, number> = {
jan: 0, jan: 0,
feb: 1, feb: 1,