datewise/app/lib/format.ts
metatroncubeswdev a5941a5149 fix(locations): drop Shopify's literal "null" city from imported addresses
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 07:17:43 -04:00

42 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** Shared display helpers for admin screens (browser-safe). */
export const money = (cents: number, currency = "CAD") =>
new Intl.NumberFormat("en-CA", { style: "currency", currency }).format(cents / 100);
export const niceDate = (d: string | null | undefined, long = false) =>
d
? new Date(`${d}T12:00:00Z`).toLocaleDateString("en-CA", {
weekday: long ? "long" : "short",
month: long ? "long" : "short",
day: "numeric",
timeZone: "UTC",
})
: "—";
export const t12 = (hhmm?: string | null) => {
if (!hhmm) return "";
const [h, m] = hhmm.split(":").map(Number);
return `${h % 12 || 12}${m ? `:${String(m).padStart(2, "0")}` : ""} ${h >= 12 ? "PM" : "AM"}`;
};
export const slotLabel = (s?: string | null, e?: string | null) => (s && e ? `${t12(s)} – ${t12(e)}` : "—");
export const STATUS_TONE: Record<string, "success" | "critical" | "neutral" | "info" | "warning"> = {
confirmed: "success",
needs_review: "critical",
cancelled: "neutral",
fulfilled: "info",
};
export const METHOD_LABEL: Record<string, string> = { pickup: "Pickup", delivery: "Delivery", shipping: "Shipping" };
export const rangesToText = (r?: { start: string; end: string }[]) => (r ?? []).map((x) => `${x.start}-${x.end}`).join(", ");
/** Shopify's formatted address prints a missing city as the word "null" (e.g. "null ON, Canada"). */
export function cleanAddress(address: string) {
return address
.split(",")
.map((part) => part.replace(/\bnull\b/g, "").replace(/\s+/g, " ").trim())
.filter(Boolean)
.join(", ");
}