@anoki/fse-ui
Version:
FSE UI components library
289 lines (288 loc) • 9.54 kB
JavaScript
import { validateWeekSettings as w, hasLocaleWeekInfo as F, roundTo as I, padStart as W, hasRelative as D } from "./index.es231.js";
import { formatRelativeTime as v, months as x, weekdays as Z, meridiems as L, eras as M } from "./index.es275.js";
import m from "./index.es4.js";
import l from "./index.es225.js";
import R from "./index.es239.js";
let T = {};
function E(n, e = {}) {
const t = JSON.stringify([n, e]);
let s = T[t];
return s || (s = new Intl.ListFormat(n, e), T[t] = s), s;
}
const d = /* @__PURE__ */ new Map();
function y(n, e = {}) {
const t = JSON.stringify([n, e]);
let s = d.get(t);
return s === void 0 && (s = new Intl.DateTimeFormat(n, e), d.set(t, s)), s;
}
const g = /* @__PURE__ */ new Map();
function P(n, e = {}) {
const t = JSON.stringify([n, e]);
let s = g.get(t);
return s === void 0 && (s = new Intl.NumberFormat(n, e), g.set(t, s)), s;
}
const p = /* @__PURE__ */ new Map();
function z(n, e = {}) {
const { base: t, ...s } = e, i = JSON.stringify([n, s]);
let r = p.get(i);
return r === void 0 && (r = new Intl.RelativeTimeFormat(n, e), p.set(i, r)), r;
}
let f = null;
function $() {
return f || (f = new Intl.DateTimeFormat().resolvedOptions().locale, f);
}
const C = /* @__PURE__ */ new Map();
function b(n) {
let e = C.get(n);
return e === void 0 && (e = new Intl.DateTimeFormat(n).resolvedOptions(), C.set(n, e)), e;
}
const k = /* @__PURE__ */ new Map();
function J(n) {
let e = k.get(n);
if (!e) {
const t = new Intl.Locale(n);
e = "getWeekInfo" in t ? t.getWeekInfo() : t.weekInfo, "minimalDays" in e || (e = { ...N, ...e }), k.set(n, e);
}
return e;
}
function U(n) {
const e = n.indexOf("-x-");
e !== -1 && (n = n.substring(0, e));
const t = n.indexOf("-u-");
if (t === -1)
return [n];
{
let s, i;
try {
s = y(n).resolvedOptions(), i = n;
} catch {
const u = n.substring(0, t);
s = y(u).resolvedOptions(), i = u;
}
const { numberingSystem: r, calendar: a } = s;
return [i, r, a];
}
}
function j(n, e, t) {
return (t || e) && (n.includes("-u-") || (n += "-u"), t && (n += `-ca-${t}`), e && (n += `-nu-${e}`)), n;
}
function A(n) {
const e = [];
for (let t = 1; t <= 12; t++) {
const s = l.utc(2009, t, 1);
e.push(n(s));
}
return e;
}
function G(n) {
const e = [];
for (let t = 1; t <= 7; t++) {
const s = l.utc(2016, 11, 13 + t);
e.push(n(s));
}
return e;
}
function c(n, e, t, s) {
const i = n.listingMode();
return i === "error" ? null : i === "en" ? t(e) : s(e);
}
function q(n) {
return n.numberingSystem && n.numberingSystem !== "latn" ? !1 : n.numberingSystem === "latn" || !n.locale || n.locale.startsWith("en") || b(n.locale).numberingSystem === "latn";
}
class K {
constructor(e, t, s) {
this.padTo = s.padTo || 0, this.floor = s.floor || !1;
const { padTo: i, floor: r, ...a } = s;
if (!t || Object.keys(a).length > 0) {
const o = { useGrouping: !1, ...s };
s.padTo > 0 && (o.minimumIntegerDigits = s.padTo), this.inf = P(e, o);
}
}
format(e) {
if (this.inf) {
const t = this.floor ? Math.floor(e) : e;
return this.inf.format(t);
} else {
const t = this.floor ? Math.floor(e) : I(e, 3);
return W(t, this.padTo);
}
}
}
class B {
constructor(e, t, s) {
this.opts = s, this.originalZone = void 0;
let i;
if (this.opts.timeZone)
this.dt = e;
else if (e.zone.type === "fixed") {
const a = -1 * (e.offset / 60), o = a >= 0 ? `Etc/GMT+${a}` : `Etc/GMT${a}`;
e.offset !== 0 && R.create(o).valid ? (i = o, this.dt = e) : (i = "UTC", this.dt = e.offset === 0 ? e : e.setZone("UTC").plus({ minutes: e.offset }), this.originalZone = e.zone);
} else e.zone.type === "system" ? this.dt = e : e.zone.type === "iana" ? (this.dt = e, i = e.zone.name) : (i = "UTC", this.dt = e.setZone("UTC").plus({ minutes: e.offset }), this.originalZone = e.zone);
const r = { ...this.opts };
r.timeZone = r.timeZone || i, this.dtf = y(t, r);
}
format() {
return this.originalZone ? this.formatToParts().map(({ value: e }) => e).join("") : this.dtf.format(this.dt.toJSDate());
}
formatToParts() {
const e = this.dtf.formatToParts(this.dt.toJSDate());
return this.originalZone ? e.map((t) => {
if (t.type === "timeZoneName") {
const s = this.originalZone.offsetName(this.dt.ts, {
locale: this.dt.locale,
format: this.opts.timeZoneName
});
return {
...t,
value: s
};
} else
return t;
}) : e;
}
resolvedOptions() {
return this.dtf.resolvedOptions();
}
}
class H {
constructor(e, t, s) {
this.opts = { style: "long", ...s }, !t && D() && (this.rtf = z(e, s));
}
format(e, t) {
return this.rtf ? this.rtf.format(e, t) : v(t, e, this.opts.numeric, this.opts.style !== "long");
}
formatToParts(e, t) {
return this.rtf ? this.rtf.formatToParts(e, t) : [];
}
}
const N = {
firstDay: 1,
minimalDays: 4,
weekend: [6, 7]
};
class h {
static fromOpts(e) {
return h.create(
e.locale,
e.numberingSystem,
e.outputCalendar,
e.weekSettings,
e.defaultToEN
);
}
static create(e, t, s, i, r = !1) {
const a = e || m.defaultLocale, o = a || (r ? "en-US" : $()), u = t || m.defaultNumberingSystem, S = s || m.defaultOutputCalendar, O = w(i) || m.defaultWeekSettings;
return new h(o, u, S, O, a);
}
static resetCache() {
f = null, d.clear(), g.clear(), p.clear(), C.clear(), k.clear();
}
static fromObject({ locale: e, numberingSystem: t, outputCalendar: s, weekSettings: i } = {}) {
return h.create(e, t, s, i);
}
constructor(e, t, s, i, r) {
const [a, o, u] = U(e);
this.locale = a, this.numberingSystem = t || o || null, this.outputCalendar = s || u || null, this.weekSettings = i, this.intl = j(this.locale, this.numberingSystem, this.outputCalendar), this.weekdaysCache = { format: {}, standalone: {} }, this.monthsCache = { format: {}, standalone: {} }, this.meridiemCache = null, this.eraCache = {}, this.specifiedLocale = r, this.fastNumbersCached = null;
}
get fastNumbers() {
return this.fastNumbersCached == null && (this.fastNumbersCached = q(this)), this.fastNumbersCached;
}
listingMode() {
const e = this.isEnglish(), t = (this.numberingSystem === null || this.numberingSystem === "latn") && (this.outputCalendar === null || this.outputCalendar === "gregory");
return e && t ? "en" : "intl";
}
clone(e) {
return !e || Object.getOwnPropertyNames(e).length === 0 ? this : h.create(
e.locale || this.specifiedLocale,
e.numberingSystem || this.numberingSystem,
e.outputCalendar || this.outputCalendar,
w(e.weekSettings) || this.weekSettings,
e.defaultToEN || !1
);
}
redefaultToEN(e = {}) {
return this.clone({ ...e, defaultToEN: !0 });
}
redefaultToSystem(e = {}) {
return this.clone({ ...e, defaultToEN: !1 });
}
months(e, t = !1) {
return c(this, e, x, () => {
const s = t ? { month: e, day: "numeric" } : { month: e }, i = t ? "format" : "standalone";
return this.monthsCache[i][e] || (this.monthsCache[i][e] = A((r) => this.extract(r, s, "month"))), this.monthsCache[i][e];
});
}
weekdays(e, t = !1) {
return c(this, e, Z, () => {
const s = t ? { weekday: e, year: "numeric", month: "long", day: "numeric" } : { weekday: e }, i = t ? "format" : "standalone";
return this.weekdaysCache[i][e] || (this.weekdaysCache[i][e] = G(
(r) => this.extract(r, s, "weekday")
)), this.weekdaysCache[i][e];
});
}
meridiems() {
return c(
this,
void 0,
() => L,
() => {
if (!this.meridiemCache) {
const e = { hour: "numeric", hourCycle: "h12" };
this.meridiemCache = [l.utc(2016, 11, 13, 9), l.utc(2016, 11, 13, 19)].map(
(t) => this.extract(t, e, "dayperiod")
);
}
return this.meridiemCache;
}
);
}
eras(e) {
return c(this, e, M, () => {
const t = { era: e };
return this.eraCache[e] || (this.eraCache[e] = [l.utc(-40, 1, 1), l.utc(2017, 1, 1)].map(
(s) => this.extract(s, t, "era")
)), this.eraCache[e];
});
}
extract(e, t, s) {
const i = this.dtFormatter(e, t), r = i.formatToParts(), a = r.find((o) => o.type.toLowerCase() === s);
return a ? a.value : null;
}
numberFormatter(e = {}) {
return new K(this.intl, e.forceSimple || this.fastNumbers, e);
}
dtFormatter(e, t = {}) {
return new B(e, this.intl, t);
}
relFormatter(e = {}) {
return new H(this.intl, this.isEnglish(), e);
}
listFormatter(e = {}) {
return E(this.intl, e);
}
isEnglish() {
return this.locale === "en" || this.locale.toLowerCase() === "en-us" || b(this.intl).locale.startsWith("en-us");
}
getWeekSettings() {
return this.weekSettings ? this.weekSettings : F() ? J(this.locale) : N;
}
getStartOfWeek() {
return this.getWeekSettings().firstDay;
}
getMinDaysInFirstWeek() {
return this.getWeekSettings().minimalDays;
}
getWeekendDays() {
return this.getWeekSettings().weekend;
}
equals(e) {
return this.locale === e.locale && this.numberingSystem === e.numberingSystem && this.outputCalendar === e.outputCalendar;
}
toString() {
return `Locale(${this.locale}, ${this.numberingSystem}, ${this.outputCalendar})`;
}
}
export {
h as default
};
//# sourceMappingURL=index.es230.js.map