UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

179 lines (178 loc) • 5.75 kB
/*! All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://github.com/Esri/calcite-design-system/blob/dev/LICENSE.md for details. v3.2.1 */ import { n as numberStringFormatter } from "./locale.js"; function inRange(date, min, max) { if (!date) { return; } const time = date.getTime(); const afterMin = !(min instanceof Date) || time >= min.getTime(); const beforeMax = !(max instanceof Date) || time <= max.getTime(); return afterMin && beforeMax; } function dateFromRange(date, min, max) { if (!(date instanceof Date)) { return null; } const time = date.getTime(); const beforeMin = min instanceof Date && time < min.getTime(); const afterMax = max instanceof Date && time > max.getTime(); if (beforeMin) { return min; } if (afterMax) { return max; } return date; } function dateFromISO(iso8601, isEndDate = false) { if (iso8601 instanceof Date) { return iso8601; } if (!iso8601 || typeof iso8601 !== "string") { return null; } const d = iso8601.split(/[: T-]/).map(parseFloat); const date = new Date(d[0], (d[1] || 1) - 1, d[2] || 1); date.setFullYear(d[0]); if (isNaN(date.getTime())) { throw new Error(`Invalid ISO 8601 date: "${iso8601}"`); } if (isEndDate) { return setEndOfDay(date); } return date; } function dateFromLocalizedString(value, localeData) { if (!localeData) { return null; } const { separator } = localeData; const parts = parseDateString(value, localeData); const { day, month } = parts; const year = parseCalendarYear(parts.year, localeData); const date = new Date(year, month, day); date.setFullYear(year); const validDay = day > 0; const validMonth = month > -1; const validDate = !isNaN(date.getTime()); const validLength = value.split(separator).filter((c) => c).length > 2; const validYear = year.toString().length > 0; if (validDay && validMonth && validDate && validLength && validYear) { return date; } return null; } function parseCalendarYear(year, localeData) { return processCalendarYear(year, localeData, "read"); } function formatCalendarYear(year, localeData) { return processCalendarYear(year, localeData, "write"); } function processCalendarYear(year, localeData, mode) { if (localeData["default-calendar"] !== "buddhist") { return year; } const BUDDHIST_CALENDAR_YEAR_OFFSET = 543; const yearOffset = BUDDHIST_CALENDAR_YEAR_OFFSET * (mode === "read" ? -1 : 1); return year + yearOffset; } function datePartsFromLocalizedString(string, localeData) { const { separator, unitOrder } = localeData; const order = getOrder(unitOrder); const values = string.split(separator).map((part) => numberStringFormatter.delocalize(part)); const day = values[order.indexOf("d")]; const month = values[order.indexOf("m")]; const year = values[order.indexOf("y")]; return { day, month, year }; } function dateToISO(date) { if (date instanceof Date) { const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); const year = String(date.getFullYear()).padStart(4, "0"); return `${year}-${month}-${day}`; } return ""; } function datePartsFromISO(isoDate) { const dateParts = isoDate.split("-"); return { day: dateParts[2], month: dateParts[1], year: dateParts[0] }; } function sameDate(d1, d2) { return d1 instanceof Date && d2 instanceof Date && d1.getDate() === d2.getDate() && d1.getMonth() === d2.getMonth() && d1.getFullYear() === d2.getFullYear(); } function prevMonth(date) { const month = date.getMonth(); const nextDate = new Date(date); nextDate.setMonth(month - 1); if (month === nextDate.getMonth()) { return new Date(date.getFullYear(), month, 0); } return nextDate; } function getDateInMonth(date, month) { const nextDate = new Date(date); nextDate.setMonth(month); return nextDate; } function getFirstValidDateInMonth(date, min, max) { const newDate = new Date(date); newDate.setDate(1); return inRange(newDate, min, max) ? newDate : dateFromRange(newDate, min, max); } function nextMonth(date) { const month = date.getMonth(); const nextDate = new Date(date); nextDate.setMonth(month + 1); if ((month + 2) % 7 === nextDate.getMonth() % 7) { return new Date(date.getFullYear(), month + 2, 0); } return nextDate; } function parseDateString(string, localeData) { const { day, month, year } = datePartsFromLocalizedString(string, localeData); return { day: parseInt(day), month: parseInt(month) - 1, // this subtracts by 1 because the month in the Date constructor is zero-based https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth year: parseInt(year) }; } function getOrder(unitOrder) { const signifiers = ["d", "m", "y"]; const order = unitOrder.toLowerCase(); return signifiers.sort((a, b) => order.indexOf(a) - order.indexOf(b)); } function getDaysDiff(date1, date2) { const ts1 = date1.getTime(); const ts2 = date2.getTime(); return (ts1 - ts2) / (1e3 * 3600 * 24); } function setEndOfDay(date) { date.setHours(23, 59, 59, 999); return date; } function hasSameMonthAndYear(date1, date2) { return date1 && date2 && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear(); } export { dateFromISO as a, dateFromRange as b, dateToISO as c, datePartsFromISO as d, datePartsFromLocalizedString as e, dateFromLocalizedString as f, formatCalendarYear as g, parseCalendarYear as h, inRange as i, getDateInMonth as j, hasSameMonthAndYear as k, getOrder as l, getFirstValidDateInMonth as m, nextMonth as n, getDaysDiff as o, prevMonth as p, sameDate as s };