From 520a792b7fe6bfb52deff74b55ad22e86a3196bc Mon Sep 17 00:00:00 2001 From: Danijel Martinek Date: Mon, 4 May 2026 20:37:26 +0200 Subject: [PATCH] feat(core-shared): add toIsoString helper --- packages/core-shared/src/lib/date.test.ts | 23 +++++++++++++++++++++++ packages/core-shared/src/lib/date.ts | 5 +++++ 2 files changed, 28 insertions(+) create mode 100644 packages/core-shared/src/lib/date.test.ts create mode 100644 packages/core-shared/src/lib/date.ts diff --git a/packages/core-shared/src/lib/date.test.ts b/packages/core-shared/src/lib/date.test.ts new file mode 100644 index 0000000..bfcc316 --- /dev/null +++ b/packages/core-shared/src/lib/date.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from "vitest"; +import { toIsoString } from "./date"; + +describe("toIsoString", () => { + it("converts a Date to ISO string", () => { + const d = new Date("2026-05-04T12:00:00.000Z"); + expect(toIsoString(d)).toBe("2026-05-04T12:00:00.000Z"); + }); + + it("passes through an existing ISO string", () => { + expect(toIsoString("2026-05-04T12:00:00.000Z")).toBe( + "2026-05-04T12:00:00.000Z", + ); + }); + + it("returns null for null input", () => { + expect(toIsoString(null)).toBeNull(); + }); + + it("returns null for undefined input", () => { + expect(toIsoString(undefined)).toBeNull(); + }); +}); diff --git a/packages/core-shared/src/lib/date.ts b/packages/core-shared/src/lib/date.ts new file mode 100644 index 0000000..6e3ee48 --- /dev/null +++ b/packages/core-shared/src/lib/date.ts @@ -0,0 +1,5 @@ +export function toIsoString(input: Date | string | null | undefined): string | null { + if (input === null || input === undefined) return null; + if (input instanceof Date) return input.toISOString(); + return input; +}