UNPKG

@anoki/fse-ui

Version:

FSE UI components library

263 lines (262 loc) • 8.77 kB
import { eraForDateTime as D, monthForDateTime as F, weekdayForDateTime as M, meridiemForDateTime as S } from "./index.es275.js"; import { DATETIME_HUGE_WITH_SECONDS as _, DATETIME_FULL_WITH_SECONDS as p, DATETIME_MED_WITH_SECONDS as k, DATETIME_SHORT_WITH_SECONDS as w, DATETIME_HUGE as I, DATETIME_FULL as L, DATETIME_MED as g, DATETIME_SHORT as O, TIME_24_WITH_LONG_OFFSET as Z, TIME_24_WITH_SHORT_OFFSET as H, TIME_24_WITH_SECONDS as W, TIME_24_SIMPLE as d, TIME_WITH_LONG_OFFSET as N, TIME_WITH_SHORT_OFFSET as A, TIME_WITH_SECONDS as G, TIME_SIMPLE as C, DATE_HUGE as b, DATE_FULL as v, DATE_MED as R, DATE_SHORT as U } from "./index.es236.js"; import { padStart as q } from "./index.es231.js"; function T(E, e) { let r = ""; for (const t of E) t.literal ? r += t.val : r += e(t.val); return r; } const z = { D: U, DD: R, DDD: v, DDDD: b, t: C, tt: G, ttt: A, tttt: N, T: d, TT: W, TTT: H, TTTT: Z, f: O, ff: g, fff: L, ffff: I, F: w, FF: k, FFF: p, FFFF: _ }; class h { static create(e, r = {}) { return new h(e, r); } static parseFormat(e) { let r = null, t = "", n = !1; const s = []; for (let c = 0; c < e.length; c++) { const i = e.charAt(c); i === "'" ? (t.length > 0 && s.push({ literal: n || /^\s+$/.test(t), val: t }), r = null, t = "", n = !n) : n || i === r ? t += i : (t.length > 0 && s.push({ literal: /^\s+$/.test(t), val: t }), t = i, r = i); } return t.length > 0 && s.push({ literal: n || /^\s+$/.test(t), val: t }), s; } static macroTokenToFormatOpts(e) { return z[e]; } constructor(e, r) { this.opts = r, this.loc = e, this.systemLoc = null; } formatWithSystemDefault(e, r) { return this.systemLoc === null && (this.systemLoc = this.loc.redefaultToSystem()), this.systemLoc.dtFormatter(e, { ...this.opts, ...r }).format(); } dtFormatter(e, r = {}) { return this.loc.dtFormatter(e, { ...this.opts, ...r }); } formatDateTime(e, r) { return this.dtFormatter(e, r).format(); } formatDateTimeParts(e, r) { return this.dtFormatter(e, r).formatToParts(); } formatInterval(e, r) { return this.dtFormatter(e.start, r).dtf.formatRange(e.start.toJSDate(), e.end.toJSDate()); } resolvedOptions(e, r) { return this.dtFormatter(e, r).resolvedOptions(); } num(e, r = 0) { if (this.opts.forceSimple) return q(e, r); const t = { ...this.opts }; return r > 0 && (t.padTo = r), this.loc.numberFormatter(t).format(e); } formatDateTimeFromString(e, r) { const t = this.loc.listingMode() === "en", n = this.loc.outputCalendar && this.loc.outputCalendar !== "gregory", s = (a, m) => this.loc.extract(e, a, m), c = (a) => e.isOffsetFixed && e.offset === 0 && a.allowZ ? "Z" : e.isValid ? e.zone.formatOffset(e.ts, a.format) : "", i = () => t ? S(e) : s({ hour: "numeric", hourCycle: "h12" }, "dayperiod"), o = (a, m) => t ? F(e, a) : s(m ? { month: a } : { month: a, day: "numeric" }, "month"), u = (a, m) => t ? M(e, a) : s( m ? { weekday: a } : { weekday: a, month: "long", day: "numeric" }, "weekday" ), l = (a) => { const m = h.macroTokenToFormatOpts(a); return m ? this.formatWithSystemDefault(e, m) : a; }, f = (a) => t ? D(e, a) : s({ era: a }, "era"), y = (a) => { switch (a) { // ms case "S": return this.num(e.millisecond); case "u": // falls through case "SSS": return this.num(e.millisecond, 3); // seconds case "s": return this.num(e.second); case "ss": return this.num(e.second, 2); // fractional seconds case "uu": return this.num(Math.floor(e.millisecond / 10), 2); case "uuu": return this.num(Math.floor(e.millisecond / 100)); // minutes case "m": return this.num(e.minute); case "mm": return this.num(e.minute, 2); // hours case "h": return this.num(e.hour % 12 === 0 ? 12 : e.hour % 12); case "hh": return this.num(e.hour % 12 === 0 ? 12 : e.hour % 12, 2); case "H": return this.num(e.hour); case "HH": return this.num(e.hour, 2); // offset case "Z": return c({ format: "narrow", allowZ: this.opts.allowZ }); case "ZZ": return c({ format: "short", allowZ: this.opts.allowZ }); case "ZZZ": return c({ format: "techie", allowZ: this.opts.allowZ }); case "ZZZZ": return e.zone.offsetName(e.ts, { format: "short", locale: this.loc.locale }); case "ZZZZZ": return e.zone.offsetName(e.ts, { format: "long", locale: this.loc.locale }); // zone case "z": return e.zoneName; // meridiems case "a": return i(); // dates case "d": return n ? s({ day: "numeric" }, "day") : this.num(e.day); case "dd": return n ? s({ day: "2-digit" }, "day") : this.num(e.day, 2); // weekdays - standalone case "c": return this.num(e.weekday); case "ccc": return u("short", !0); case "cccc": return u("long", !0); case "ccccc": return u("narrow", !0); // weekdays - format case "E": return this.num(e.weekday); case "EEE": return u("short", !1); case "EEEE": return u("long", !1); case "EEEEE": return u("narrow", !1); // months - standalone case "L": return n ? s({ month: "numeric", day: "numeric" }, "month") : this.num(e.month); case "LL": return n ? s({ month: "2-digit", day: "numeric" }, "month") : this.num(e.month, 2); case "LLL": return o("short", !0); case "LLLL": return o("long", !0); case "LLLLL": return o("narrow", !0); // months - format case "M": return n ? s({ month: "numeric" }, "month") : this.num(e.month); case "MM": return n ? s({ month: "2-digit" }, "month") : this.num(e.month, 2); case "MMM": return o("short", !1); case "MMMM": return o("long", !1); case "MMMMM": return o("narrow", !1); // years case "y": return n ? s({ year: "numeric" }, "year") : this.num(e.year); case "yy": return n ? s({ year: "2-digit" }, "year") : this.num(e.year.toString().slice(-2), 2); case "yyyy": return n ? s({ year: "numeric" }, "year") : this.num(e.year, 4); case "yyyyyy": return n ? s({ year: "numeric" }, "year") : this.num(e.year, 6); // eras case "G": return f("short"); case "GG": return f("long"); case "GGGGG": return f("narrow"); case "kk": return this.num(e.weekYear.toString().slice(-2), 2); case "kkkk": return this.num(e.weekYear, 4); case "W": return this.num(e.weekNumber); case "WW": return this.num(e.weekNumber, 2); case "n": return this.num(e.localWeekNumber); case "nn": return this.num(e.localWeekNumber, 2); case "ii": return this.num(e.localWeekYear.toString().slice(-2), 2); case "iiii": return this.num(e.localWeekYear, 4); case "o": return this.num(e.ordinal); case "ooo": return this.num(e.ordinal, 3); case "q": return this.num(e.quarter); case "qq": return this.num(e.quarter, 2); case "X": return this.num(Math.floor(e.ts / 1e3)); case "x": return this.num(e.ts); default: return l(a); } }; return T(h.parseFormat(r), y); } formatDurationFromString(e, r) { const t = (o) => { switch (o[0]) { case "S": return "millisecond"; case "s": return "second"; case "m": return "minute"; case "h": return "hour"; case "d": return "day"; case "w": return "week"; case "M": return "month"; case "y": return "year"; default: return null; } }, n = (o) => (u) => { const l = t(u); return l ? this.num(o.get(l), u.length) : u; }, s = h.parseFormat(r), c = s.reduce( (o, { literal: u, val: l }) => u ? o : o.concat(l), [] ), i = e.shiftTo(...c.map(t).filter((o) => o)); return T(s, n(i)); } } export { h as default }; //# sourceMappingURL=index.es228.js.map