UNPKG

@hakit/components

Version:
101 lines (100 loc) 2.94 kB
import { j as l, F as P } from "../../../emotion-react-jsx-runtime.browser.esm-Tq_btErF.js"; import { AmOrPm as p } from "./shared.js"; const f = /* @__PURE__ */ new Map(), $ = [{ weekday: "long", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit" }, { month: "long", hour: "2-digit", hour12: !1 }]; function j(e) { return function(n, r, s) { const o = I(n, s); return N(e, r, o, n); }; } const k = (e) => $.map((t) => new Intl.DateTimeFormat(e.locale, { ...t, timeZone: e.timezone })), D = (e) => e.type !== "literal" ? { type: `l${e.type}`, value: e.value } : e, M = (e, t) => (e[t.type] = t.value, e), g = (e, t) => e.formatToParts(t).filter((n) => n.type !== "literal"), w = (e) => (e.dayPeriod = e.dayPeriod || // @ts-expect-error - see note above e.dayperiod || "", delete e.dayperiod, e.lhour = ("0" + Number(e.lhour) % 24).slice(-2), e), F = (e) => { const [t, n] = k(e); return function(s) { const o = g(t, s), i = g(n, s).map(D), a = [...o, ...i].reduce(M, {}); return w(a); }; }; function I(e, t = {}) { const n = `${t.locale}${t.timezone}`; let r = f.get(n); return r || (r = F(t), f.set(n, r)), r(e); } function x(e) { let t = ""; switch (e) { case 1: case 21: case 31: t = "st"; break; case 2: case 22: t = "nd"; break; case 3: case 23: t = "rd"; break; default: t = "th"; } return t; } const T = "[YMDxdAaHhms]+", Y = { YYYY: (e) => e.year, YY: (e) => e.year.slice(-2), MMMM: (e) => e.lmonth, MMM: (e) => e.lmonth.slice(0, 3), MM: (e) => e.month, DD: (e) => e.day, DDx: (e) => `${e.day}${x(parseInt(e.day))}`, dd: (e) => `${parseInt(e.day, 10)}`, ddx: (e) => `${parseInt(e.day, 10)}${x(parseInt(e.day))}`, dddd: (e) => e.weekday, ddd: (e) => e.weekday.slice(0, 3), A: (e) => /* @__PURE__ */ l(p, { className: "time-suffix", children: e.dayPeriod }), a: (e) => /* @__PURE__ */ l(p, { className: "time-suffix", children: e.dayPeriod.toLowerCase() }), // XXX: fix Chrome 80+ bug going over 24h HH: (e) => ("0" + Number(e.lhour) % 24).slice(-2), hh: (e) => e.hour, mm: (e) => e.minute, ss: (e) => e.second }, b = (e) => Object.keys(e).reduce((t, n) => `|${n}`, ""); function N(e, t, n, r) { const s = "\\[([^\\]]+)\\]|", o = b(e), i = new RegExp(`${s}${T}${o}`, "g"), m = { ...Y, ...e }, a = []; let c = 0; return t.replace(i, (h, y, d) => { if (d > c) { const u = t.slice(c, d); u.length > 0 && u.trim().length === 0 ? a.push(/* @__PURE__ */ l("span", { className: "whitespace", children: " " })) : a.push(u); } return c = d + h.length, y ? a.push(y) : a.push(m[h](n, r)), ""; }), c < t.length && a.push(t.slice(c)), /* @__PURE__ */ l(P, { children: a }); } export { j as createDateFormatter, x as daySuffix }; //# sourceMappingURL=formatter.js.map