feat(core-shared): add toIsoString helper

This commit is contained in:
2026-05-04 20:37:26 +02:00
parent 3ffe752c7a
commit 520a792b7f
2 changed files with 28 additions and 0 deletions

View File

@@ -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();
});
});

View File

@@ -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;
}