@anoki/fse-ui
Version:
FSE UI components library
284 lines (283 loc) • 9.21 kB
JavaScript
import { validateWeekSettings as C, hasLocaleWeekInfo as S, roundTo as N, padStart as O, hasRelative as F } from "./index.es167.js";
import { formatRelativeTime as I, months as W, weekdays as D, meridiems as x, eras as Z } from "./index.es313.js";
import m from "./index.es4.js";
import l from "./index.es161.js";
import v from "./index.es175.js";
let k = {};
function L(n, e = {}) {
const t = JSON.stringify([n, e]);
let s = k[t];
return s || (s = new Intl.ListFormat(n, e), k[t] = s), s;
}
let d = {};
function y(n, e = {}) {
const t = JSON.stringify([n, e]);
let s = d[t];
return s || (s = new Intl.DateTimeFormat(n, e), d[t] = s), s;
}
let g = {};
function E(n, e = {}) {
const t = JSON.stringify([n, e]);
let s = g[t];
return s || (s = new Intl.NumberFormat(n, e), g[t] = s), s;
}
let p = {};
function P(n, e = {}) {
const { base: t, ...s } = e, i = JSON.stringify([n, s]);
let r = p[i];
return r || (r = new Intl.RelativeTimeFormat(n, e), p[i] = r), r;
}
let f = null;
function R() {
return f || (f = new Intl.DateTimeFormat().resolvedOptions().locale, f);
}
let w = {};
function M(n) {
let e = w[n];
if (!e) {
const t = new Intl.Locale(n);
e = "getWeekInfo" in t ? t.getWeekInfo() : t.weekInfo, w[n] = e;
}
return e;
}
function z(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 $(n, e, t) {
return (t || e) && (n.includes("-u-") || (n += "-u"), t && (n += `-ca-${t}`), e && (n += `-nu-${e}`)), n;
}
function J(n) {
const e = [];
for (let t = 1; t <= 12; t++) {
const s = l.utc(2009, t, 1);
e.push(n(s));
}
return e;
}
function U(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 j(n) {
return n.numberingSystem && n.numberingSystem !== "latn" ? !1 : n.numberingSystem === "latn" || !n.locale || n.locale.startsWith("en") || new Intl.DateTimeFormat(n.intl).resolvedOptions().numberingSystem === "latn";
}
class A {
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 = E(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) : N(e, 3);
return O(t, this.padTo);
}
}
}
class G {
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 && v.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 q {
constructor(e, t, s) {
this.opts = { style: "long", ...s }, !t && F() && (this.rtf = P(e, s));
}
format(e, t) {
return this.rtf ? this.rtf.format(e, t) : I(t, e, this.opts.numeric, this.opts.style !== "long");
}
formatToParts(e, t) {
return this.rtf ? this.rtf.formatToParts(e, t) : [];
}
}
const K = {
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" : R()), u = t || m.defaultNumberingSystem, T = s || m.defaultOutputCalendar, b = C(i) || m.defaultWeekSettings;
return new h(o, u, T, b, a);
}
static resetCache() {
f = null, d = {}, g = {}, p = {};
}
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] = z(e);
this.locale = a, this.numberingSystem = t || o || null, this.outputCalendar = s || u || null, this.weekSettings = i, this.intl = $(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 = j(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,
C(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, W, () => {
const s = t ? { month: e, day: "numeric" } : { month: e }, i = t ? "format" : "standalone";
return this.monthsCache[i][e] || (this.monthsCache[i][e] = J((r) => this.extract(r, s, "month"))), this.monthsCache[i][e];
});
}
weekdays(e, t = !1) {
return c(this, e, D, () => {
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] = U(
(r) => this.extract(r, s, "weekday")
)), this.weekdaysCache[i][e];
});
}
meridiems() {
return c(
this,
void 0,
() => x,
() => {
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, Z, () => {
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 A(this.intl, e.forceSimple || this.fastNumbers, e);
}
dtFormatter(e, t = {}) {
return new G(e, this.intl, t);
}
relFormatter(e = {}) {
return new q(this.intl, this.isEnglish(), e);
}
listFormatter(e = {}) {
return L(this.intl, e);
}
isEnglish() {
return this.locale === "en" || this.locale.toLowerCase() === "en-us" || new Intl.DateTimeFormat(this.intl).resolvedOptions().locale.startsWith("en-us");
}
getWeekSettings() {
return this.weekSettings ? this.weekSettings : S() ? M(this.locale) : K;
}
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.es166.js.map