UNPKG

@aotearoan/neon

Version:

Neon is a lightweight design library of Vue 3 components with minimal dependencies.

127 lines (126 loc) 5.23 kB
class c { /** * Convert an ISO 8601 formatted string to a locale formatted date with a three letter month, a 2 digit day and a locale formatted time. * * @param date The date/time as an <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> string. * @param locale The locale for which to convert the date for display purposes, defaults to browser locale. * @param strict Do not add time component to localise the date. * * @returns Object representing the provided date. */ static stringToNeonDate(t, o, e = !1) { const a = o || navigator.language, i = /* @__PURE__ */ new Date(), n = new Date(!e && t.length === 10 ? `${t}T${i.toISOString().split("T")[1]}` : t); let r; t.length > 10 && (r = n.toLocaleString( a, t.length <= 16 ? { hour12: !1, hour: "2-digit", minute: "2-digit" } : { hour12: !1, hour: "2-digit", minute: "2-digit", second: "2-digit" } )); const d = { year: +n.toLocaleString("en-US", { year: "numeric" }), yearFormatted: n.toLocaleString(a, { year: "numeric" }), month: +n.toLocaleString("en-US", { month: "numeric" }), monthShortName: n.toLocaleString(a, { month: "short" }), monthLongName: n.toLocaleString(a, { month: "long" }), day: +n.toLocaleString("en-US", { day: "numeric" }), dayFormatted: n.toLocaleString(a, { day: "2-digit" }) }; return r && (d.time = r), d; } /** * Convert a date object to an ISO 8601 formatted string. By default, the time is not considered. * * @param date Javascript date object. * @param time Whether to consider the time as part of the conversion to the ISO string. * * @returns an ISO 8601 formatted date, optionally with the time as well. */ static dateToIso(t, o = !1) { const e = t.toISOString(); return o ? e : e.split("T")[0]; } /** * Convert the params received into an ISO 8601 formatted string with only date in it. * * @param day The number of the day to be used. * @param month The number of the month to be used. * @param year The number of the year to be used. * * @returns an ISO 8601 formatted date for the given parameters. */ static dmyToIso(t, o, e) { return `${e}-${o < 10 ? "0" + o : o}-${t < 10 ? "0" + t : t}`; } /** * Returns the days of the week starting at Monday day name translated using the locale received as a first element. * * @param locale The locale for which to convert the date for display purposes, defaults to browser locale. * * @returns an array with the days of the week named according to the locale received. */ static dowNames(t) { const o = t || navigator.language, e = /* @__PURE__ */ new Date(), a = []; for (; !a[e.getDay()]; ) a[e.getDay()] = e.toLocaleString(o, { weekday: "short" }), e.setDate(e.getDate() + 1); const i = a.shift(); return i && a.push(i), a; } /** * Returns the months of the year starting at January month name translated using the locale received as a first element. * * @param locale The locale for which to convert the date for display purposes, defaults to browser locale. * * @returns an array with the month names of the year, all named according to the locale received. */ static monthNames(t) { const o = t || navigator.language; return Array.from(Array(12).keys()).map((e) => new Date(2023, e, 15).toLocaleString(o, { month: "short" })); } /** * Collate & return the data required to drive the calendar popup in * <a href="/user-input/date-picker">NeonDatePicker</a>. * * @param selectedDate ISO-8601 date-only string for the selected date. * @param currentPageMonth month of the current calendar page. * @param currentPageYear year of the current calendar page. * @param currentPageDecadeStart decade start year of the current calendar page. * @param locale user's locale. * * @returns The calendar configuration. */ static toCalendarConfiguration(t, o, e, a, i) { const n = i || navigator.language, r = c.stringToNeonDate(c.dateToIso(/* @__PURE__ */ new Date()), n), d = t ? c.stringToNeonDate(t, n, !0) : void 0, u = o || r.month, f = o && e ? new Date(e, o - 1, 15) : null, L = (f == null ? void 0 : f.toLocaleString(n, { month: "long" })) || r.monthLongName, h = e || r.year, T = a || Math.floor(h / 10) * 10, l = new Date(h, (u + 11) % 12, 1), S = new Date(h, (u + 11) % 12, 1); S.setDate(S.getDate() - 1); const m = []; let y = 1, w = !1; for (let s = 0; s < 6; s++) { m[s] = []; for (let g = 0; g < 7; g++) if (s === 0 && g < S.getDay()) m[s][g] = null; else if (m[s][g] = l.getDate(), y = l.getDate(), l.setDate(l.getDate() + 1), l.getDate() === 1) { for (let D = g + 1; D < 7; D++) m[s][D] = null; w = !0; break; } if (w) break; } return { today: r, selected: d, pageMonth: u, pageMonthName: L, pageYear: h, pageDecadeStart: T, dowNames: c.dowNames(n), dates: m, lastDayOfMonth: y, months: c.monthNames(n) }; } } export { c as NeonDateUtils }; //# sourceMappingURL=NeonDateUtils.es.js.map