UNPKG

@harvest-profit/npk

Version:
133 lines 6.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MONTH_ABBREVIATIONS = exports.endOfYear = exports.startOfYear = exports.endOfMonth = exports.startOfMonth = exports.endOfWeek = exports.startOfWeek = exports.lastYear = exports.nextYear = exports.lastMonth = exports.nextMonth = exports.tomorrow = exports.yesterday = exports.today = void 0; exports.fromISO = fromISO; exports.toISO = toISO; exports.fromTimestamp = fromTimestamp; exports.toTimestamp = toTimestamp; exports.monthIndexToHuman = monthIndexToHuman; exports.monthHumanToIndex = monthHumanToIndex; exports.monthIndexToAbbrev = monthIndexToAbbrev; exports.monthAbbrevToIndex = monthAbbrevToIndex; exports.isSameDay = isSameDay; exports.isPartOfSelection = isPartOfSelection; exports.getDatesInWeek = getDatesInWeek; exports.getWeeksInMonth = getWeeksInMonth; exports.nameForVisibleDates = nameForVisibleDates; const today = () => new Date(); exports.today = today; const yesterday = () => new Date(new Date().setDate((0, exports.today)().getDate() - 1)); exports.yesterday = yesterday; const tomorrow = () => new Date(new Date().setDate((0, exports.today)().getDate() + 1)); exports.tomorrow = tomorrow; const nextMonth = () => new Date(new Date().setMonth((0, exports.today)().getMonth() + 1)); exports.nextMonth = nextMonth; const lastMonth = () => new Date(new Date().setMonth((0, exports.today)().getMonth() - 1)); exports.lastMonth = lastMonth; const nextYear = () => new Date(new Date().setFullYear((0, exports.today)().getFullYear() + 1)); exports.nextYear = nextYear; const lastYear = () => new Date(new Date().setFullYear((0, exports.today)().getFullYear() - 1)); exports.lastYear = lastYear; const startOfWeek = (date = (0, exports.today)()) => new Date(new Date().setDate(date.getDate() - date.getDay())); exports.startOfWeek = startOfWeek; const endOfWeek = (date = (0, exports.today)()) => new Date(new Date().setDate(date.getDate() + (6 - date.getDay()))); exports.endOfWeek = endOfWeek; const startOfMonth = (date = (0, exports.today)()) => new Date(new Date(date).setDate(1)); exports.startOfMonth = startOfMonth; const endOfMonth = (date = (0, exports.today)()) => new Date(new Date(date).setMonth(date.getMonth() + 1, 0)); exports.endOfMonth = endOfMonth; const startOfYear = (date = (0, exports.today)()) => new Date(new Date(date).setMonth(0, 1)); exports.startOfYear = startOfYear; const endOfYear = (date = (0, exports.today)()) => new Date(new Date(date).setMonth(11, 31)); exports.endOfYear = endOfYear; function fromISO(dateString) { if (!dateString) return null; if (dateString instanceof Date) return dateString; if (dateString.includes('T')) return new Date(dateString); const dateParts = dateString.split('-'); return new Date(dateParts[0], parseInt(dateParts[1]) - 1, dateParts[2]); } function toISO(date) { if (!date) return null; return date.toISOString(); } function fromTimestamp(number) { if (!number) return null; return new Date(number); } function toTimestamp(date) { if (!date) return null; return date.getTime(); } function monthIndexToHuman(monthIndex) { return isFinite(monthIndex) ? monthIndex + 1 : null; } function monthHumanToIndex(monthNumber) { return (monthNumber || '').length > 0 ? (parseInt(monthNumber, 10) - 1) : null; } exports.MONTH_ABBREVIATIONS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']; function monthIndexToAbbrev(monthIndex) { return isFinite(monthIndex) ? exports.MONTH_ABBREVIATIONS[monthIndex] : null; } function monthAbbrevToIndex(monthAbbrev) { if (!monthAbbrev) return null; for (let i = 0; i < exports.MONTH_ABBREVIATIONS.length; i++) { if (exports.MONTH_ABBREVIATIONS[i].startsWith(monthAbbrev.toLowerCase())) return i; // Check if the next value starts with a valid month abbreviation } return null; } function isSameDay(one, two) { if (!one || !two) return false; return one.getFullYear() === two.getFullYear() && one.getMonth() === two.getMonth() && one.getDate() === two.getDate(); } function isPartOfSelection(state, date) { if (state.range) { if (isSameDay(date, state.value.start) || isSameDay(date, state.value.end)) return true; if (state.value?.start && state.value?.end) { if (date >= state.value.start && date <= state.value.end) return true; return false; } return false; } else { return isSameDay(date, state.selectingValue); } } function getDatesInWeek(weekIndex, visibleDate) { const firstDay = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), 1).getDay(); const startOfWeek = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), weekIndex * 7 - firstDay + 1); return [...Array(7)].map((_, i) => { const date = new Date(startOfWeek); date.setDate(date.getDate() + i); return date; }); } function getWeeksInMonth(visibleDate) { const firstDay = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), 1).getDay(); const totalDays = new Date(visibleDate.getFullYear(), visibleDate.getMonth() + 1, 0).getDate(); return Math.ceil((firstDay + totalDays) / 7); } function nameForVisibleDates(visibleDate, visibleMonths = 1) { if (visibleMonths <= 1 || !isFinite(visibleMonths)) return visibleDate.toLocaleString('default', { month: 'long', year: 'numeric' }); const startMonth = (0, exports.startOfMonth)(visibleDate); const endMonth = (0, exports.startOfMonth)(new Date(new Date(visibleDate).setMonth(visibleDate.getMonth() + visibleMonths))); const startMonthName = startMonth.toLocaleString('default', { month: 'long' }); const endMonthName = endMonth.toLocaleString('default', { month: 'long' }); if (startMonth.getFullYear() === endMonth.getFullYear()) { return `${startMonthName}${endMonthName} ${startMonth.getFullYear()}`; } return `${startMonthName} ${startMonth.getFullYear()}${endMonthName} ${endMonth.getFullYear()}`; } //# sourceMappingURL=utils.js.map