UNPKG

@anoki/fse-marche-ui

Version:

FSE UI components library

346 lines (345 loc) 8.93 kB
import { hasOwnProperty as m, isUndefined as y, parseMillis as q, signedOffset as $, untruncateYear as M } from "./index.es124.js"; import g from "./index.es121.js"; import P from "./index.es122.js"; import C from "./index.es132.js"; import O from "./index.es118.js"; import { digitRegex as c, parseDigits as H } from "./index.es136.js"; import { ConflictingSpecificationError as N } from "./index.es130.js"; const z = "missing Intl.DateTimeFormat.formatToParts support"; function s(e, r = (t) => t) { return { regex: e, deser: ([t]) => r(H(t)) }; } const I = " ", Z = `[ ${I}]`, v = new RegExp(Z, "g"); function U(e) { return e.replace(/\./g, "\\.?").replace(v, Z); } function E(e) { return e.replace(/\./g, "").replace(v, " ").toLowerCase(); } function l(e, r) { return e === null ? null : { regex: RegExp(e.map(U).join("|")), deser: ([t]) => e.findIndex((n) => E(t) === E(n)) + r }; } function w(e, r) { return { regex: e, deser: ([, t, n]) => $(t, n), groups: r }; } function h(e) { return { regex: e, deser: ([r]) => r }; } function D(e) { return e.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); } function b(e, r) { const t = c(r), n = c(r, "{2}"), a = c(r, "{3}"), u = c(r, "{4}"), o = c(r, "{6}"), i = c(r, "{1,2}"), f = c(r, "{1,3}"), S = c(r, "{1,6}"), R = c(r, "{1,9}"), k = c(r, "{2,4}"), F = c(r, "{4,6}"), T = (d) => ({ regex: RegExp(D(d.val)), deser: ([L]) => L, literal: !0 }), x = ((d) => { if (e.literal) return T(d); switch (d.val) { // era case "G": return l(r.eras("short"), 0); case "GG": return l(r.eras("long"), 0); // years case "y": return s(S); case "yy": return s(k, M); case "yyyy": return s(u); case "yyyyy": return s(F); case "yyyyyy": return s(o); // months case "M": return s(i); case "MM": return s(n); case "MMM": return l(r.months("short", !0), 1); case "MMMM": return l(r.months("long", !0), 1); case "L": return s(i); case "LL": return s(n); case "LLL": return l(r.months("short", !1), 1); case "LLLL": return l(r.months("long", !1), 1); // dates case "d": return s(i); case "dd": return s(n); // ordinals case "o": return s(f); case "ooo": return s(a); // time case "HH": return s(n); case "H": return s(i); case "hh": return s(n); case "h": return s(i); case "mm": return s(n); case "m": return s(i); case "q": return s(i); case "qq": return s(n); case "s": return s(i); case "ss": return s(n); case "S": return s(f); case "SSS": return s(a); case "u": return h(R); case "uu": return h(i); case "uuu": return s(t); // meridiem case "a": return l(r.meridiems(), 0); // weekYear (k) case "kkkk": return s(u); case "kk": return s(k, M); // weekNumber (W) case "W": return s(i); case "WW": return s(n); // weekdays case "E": case "c": return s(t); case "EEE": return l(r.weekdays("short", !1), 1); case "EEEE": return l(r.weekdays("long", !1), 1); case "ccc": return l(r.weekdays("short", !0), 1); case "cccc": return l(r.weekdays("long", !0), 1); // offset/zone case "Z": case "ZZ": return w(new RegExp(`([+-]${i.source})(?::(${n.source}))?`), 2); case "ZZZ": return w(new RegExp(`([+-]${i.source})(${n.source})?`), 2); // we don't support ZZZZ (PST) or ZZZZZ (Pacific Standard Time) in parsing // because we don't have any way to figure out what they are case "z": return h(/[a-z_+-/]{1,256}?/i); // this special-case "token" represents a place where a macro-token expanded into a white-space literal // in this case we accept any non-newline white-space case " ": return h(/[^\S\n\r]/); default: return T(d); } })(e) || { invalidReason: z }; return x.token = e, x; } const G = { year: { "2-digit": "yy", numeric: "yyyyy" }, month: { numeric: "M", "2-digit": "MM", short: "MMM", long: "MMMM" }, day: { numeric: "d", "2-digit": "dd" }, weekday: { short: "EEE", long: "EEEE" }, dayperiod: "a", dayPeriod: "a", hour12: { numeric: "h", "2-digit": "hh" }, hour24: { numeric: "H", "2-digit": "HH" }, minute: { numeric: "m", "2-digit": "mm" }, second: { numeric: "s", "2-digit": "ss" }, timeZoneName: { long: "ZZZZZ", short: "ZZZ" } }; function W(e, r, t) { const { type: n, value: a } = e; if (n === "literal") { const f = /^\s+$/.test(a); return { literal: !f, val: f ? " " : a }; } const u = r[n]; let o = n; n === "hour" && (r.hour12 != null ? o = r.hour12 ? "hour12" : "hour24" : r.hourCycle != null ? r.hourCycle === "h11" || r.hourCycle === "h12" ? o = "hour12" : o = "hour24" : o = t.hour12 ? "hour12" : "hour24"); let i = G[o]; if (typeof i == "object" && (i = i[u]), i) return { literal: !1, val: i }; } function j(e) { return [`^${e.map((t) => t.regex).reduce((t, n) => `${t}(${n.source})`, "")}$`, e]; } function A(e, r, t) { const n = e.match(r); if (n) { const a = {}; let u = 1; for (const o in t) if (m(t, o)) { const i = t[o], f = i.groups ? i.groups + 1 : 1; !i.literal && i.token && (a[i.token.val[0]] = i.deser(n.slice(u, u + f))), u += f; } return [n, a]; } else return [n, {}]; } function B(e) { const r = (u) => { switch (u) { case "S": return "millisecond"; case "s": return "second"; case "m": return "minute"; case "h": case "H": return "hour"; case "d": return "day"; case "o": return "ordinal"; case "L": case "M": return "month"; case "y": return "year"; case "E": case "c": return "weekday"; case "W": return "weekNumber"; case "k": return "weekYear"; case "q": return "quarter"; default: return null; } }; let t = null, n; return y(e.z) || (t = C.create(e.z)), y(e.Z) || (t || (t = new P(e.Z)), n = e.Z), y(e.q) || (e.M = (e.q - 1) * 3 + 1), y(e.h) || (e.h < 12 && e.a === 1 ? e.h += 12 : e.h === 12 && e.a === 0 && (e.h = 0)), e.G === 0 && e.y && (e.y = -e.y), y(e.u) || (e.S = q(e.u)), [Object.keys(e).reduce((u, o) => { const i = r(o); return i && (u[i] = e[o]), u; }, {}), t, n]; } let p = null; function V() { return p || (p = O.fromMillis(1555555555555)), p; } function Y(e, r) { if (e.literal) return e; const t = g.macroTokenToFormatOpts(e.val), n = Q(t, r); return n == null || n.includes(void 0) ? e : n; } function _(e, r) { return Array.prototype.concat(...e.map((t) => Y(t, r))); } class J { constructor(r, t) { if (this.locale = r, this.format = t, this.tokens = _(g.parseFormat(t), r), this.units = this.tokens.map((n) => b(n, r)), this.disqualifyingUnit = this.units.find((n) => n.invalidReason), !this.disqualifyingUnit) { const [n, a] = j(this.units); this.regex = RegExp(n, "i"), this.handlers = a; } } explainFromTokens(r) { if (this.isValid) { const [t, n] = A(r, this.regex, this.handlers), [a, u, o] = n ? B(n) : [null, null, void 0]; if (m(n, "a") && m(n, "H")) throw new N( "Can't include meridiem when specifying 24-hour format" ); return { input: r, tokens: this.tokens, regex: this.regex, rawMatches: t, matches: n, result: a, zone: u, specificOffset: o }; } else return { input: r, tokens: this.tokens, invalidReason: this.invalidReason }; } get isValid() { return !this.disqualifyingUnit; } get invalidReason() { return this.disqualifyingUnit ? this.disqualifyingUnit.invalidReason : null; } } function K(e, r, t) { return new J(e, t).explainFromTokens(r); } function oe(e, r, t) { const { result: n, zone: a, specificOffset: u, invalidReason: o } = K(e, r, t); return [n, a, u, o]; } function Q(e, r) { if (!e) return null; const n = g.create(r, e).dtFormatter(V()), a = n.formatToParts(), u = n.resolvedOptions(); return a.map((o) => W(o, e, u)); } export { J as TokenParser, _ as expandMacroTokens, K as explainFromTokens, Q as formatOptsToTokens, oe as parseFromTokens }; //# sourceMappingURL=index.es127.js.map