feat: add birth date value adapter

This commit is contained in:
Jesse_Chen
2026-07-17 21:02:18 +08:00
parent b0eb52f792
commit cf2099f683
4 changed files with 44 additions and 0 deletions
@@ -1,5 +1,8 @@
import { format, isValid, parse } from "date-fns";
import type { JourneySnapshot } from "./birth-time-journey.ts";
const birthDatePattern = "yyyy-MM-dd";
export type BirthTimeSource =
| ""
| "hospital_record"
@@ -39,6 +42,17 @@ export type BirthTimeDraft = {
export type BirthTimeDraftPatch = Partial<BirthTimeDraft>;
export function parseBirthDate(value: string): Date | undefined {
if (value === "") return undefined;
const parsed = parse(value, birthDatePattern, new Date(2000, 0, 1));
if (!isValid(parsed) || format(parsed, birthDatePattern) !== value) return undefined;
return parsed;
}
export function formatBirthDate(value: Date): string {
return format(value, birthDatePattern);
}
export const birthTimeSourceOptions = [
{ value: "hospital_record", label: "出生证明或医院记录", hint: "先检查前后两分钟是否稳定" },
{ value: "family_exact", label: "家人明确记得具体时间", hint: "进行 5—15 分钟轻量校正" },