UNPKG

web-ui-pack

Version:
165 lines (164 loc) 5.13 kB
export class WUPlocaleInfo { locale = ""; localeUser = new Intl.DateTimeFormat().resolvedOptions().locale; sepDecimal = "."; sep1000 = ","; date = "YYYY-MM-DD"; time = "hh:mm:ss A"; dateTime = "YYYY-MM-DD hh:mm:ss A"; firstWeekDay = 1; refresh(locale) { const cur = new Intl.DateTimeFormat(locale).resolvedOptions(); if (cur.numberingSystem !== "latn") { console.warn(`WUP.LocaleInfo (${cur.locale}). Not latin numbers are not supported. Setup it itself`); return; } localeInfo.locale = cur.locale; this.#namesMonth = undefined; this.#namesMonthShort = undefined; this.#namesDayShort = undefined; Object.assign(localeInfo, this.getNumberOptions(locale), this.getDateFormat(locale)); try { if (Intl.Locale) { const firstDay = new Intl.Locale(cur.locale).weekInfo?.firstDay; if (firstDay) { this.firstWeekDay = firstDay; } } } catch (error) { console.error(error); } } getNumberOptions(locale) { const s = (1234.5).toLocaleString(locale); let sep1000; for (let i = 1;; ++i) { const ascii = s.charCodeAt(i); if (ascii >= 48 && ascii <= 57) { sep1000 = s.substring(1, i); break; } } let sepDecimal; for (let i = s.length - 2;; --i) { const ascii = s.charCodeAt(i); if (ascii >= 48 && ascii <= 57) { sepDecimal = s.substring(i + 1, s.length - 1); break; } } return { sep1000, sepDecimal }; } getDateFormat(locale) { const s = new Date(2222, 0, 3, 4, 5, 6).toLocaleString(locale).replace(/am/, "a").replace(/AM/, "A"); let dateTime = ""; let endDate = 0; let endTime = 0; for (let i = 0, inc = 0; i < s.length; ++i) { const c = s.charCodeAt(i) - 48; let canAdd = true; const isDate = c > 0 && c < 4; if (isDate) { endDate = i; } else if (c > 3 && c < 7) { endTime = i; } switch (c) { case 2: dateTime += "Y"; break; case 1: dateTime += "M"; break; case 3: dateTime += "D"; break; case 4: dateTime += "h"; break; case 5: dateTime += "m"; break; case 6: dateTime += "s"; break; case 49: case 17: dateTime += s[i]; endTime = i; break; case 0: ++inc; canAdd = false; break; default: dateTime += s[i]; canAdd = false; break; } if (canAdd && inc) { dateTime += dateTime[dateTime.length - 1].repeat(inc); inc = 0; } } const startDate = /[DMY]/.exec(dateTime).index; const startTime = /[hms]/.exec(dateTime).index; const r = { date: dateTime.substring(startDate, endDate + 1), time: dateTime.substring(startTime, endTime + 1), dateTime, }; return r; } getDateNames(opts) { const arr = []; const f = new Intl.DateTimeFormat(this.locale || "en-US", opts); const dt = new Date(2022, 0, 1); for (let i = 0; i < 12; ++i) { dt.setMonth(i); arr.push(f.format(dt)); } return arr; } #namesMonth; get namesMonth() { if (this.#namesMonth) { return this.#namesMonth; } return this.getDateNames({ month: "long" }); } set namesMonth(v) { this.#namesMonth = v; } #namesMonthShort; get namesMonthShort() { if (this.#namesMonthShort) { return this.#namesMonthShort; } return this.getDateNames({ month: "short" }); } set namesMonthShort(v) { this.#namesMonthShort = v; } #namesDayShort = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]; get namesDayShort() { if (this.#namesDayShort) { return this.#namesDayShort; } const arr = []; const f = new Intl.DateTimeFormat(this.locale || "en-US", { weekday: "short" }); const dt = new Date(2022, 7, 1); for (let i = 1; i < 8; ++i) { dt.setDate(i); arr.push(f.format(dt)); } return arr; } set namesDayShort(v) { this.#namesDayShort = v; } } const localeInfo = new WUPlocaleInfo(); export default localeInfo;