UNPKG

@harvest-profit/npk

Version:
264 lines 10.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.endOfDay = exports.startOfDay = 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 = exports.timeZones = void 0; exports.change = change; exports.get = get; exports.add = add; exports.subtract = subtract; exports.dayIsInFrontForCurrentLocale = dayIsInFrontForCurrentLocale; exports.parse = parse; exports.fromISO = fromISO; exports.toISODateTime = toISODateTime; exports.toISODate = toISODate; exports.fromTimestamp = fromTimestamp; exports.toTimestamp = toTimestamp; exports.getMonthNames = getMonthNames; exports.getMonthNamesStartingWith = getMonthNamesStartingWith; exports.monthIndexToMonthNumber = monthIndexToMonthNumber; exports.monthNumberToMonthIndex = monthNumberToMonthIndex; exports.monthIndexToAbbrev = monthIndexToAbbrev; exports.monthAbbrevToMonthIndex = monthAbbrevToMonthIndex; exports.isEqual = isEqual; exports.getDatesInWeek = getDatesInWeek; exports.getWeeksInMonth = getWeeksInMonth; exports.nameForVisibleDates = nameForVisibleDates; const timeZones = () => Intl.supportedValuesOf('timeZone'); exports.timeZones = timeZones; const today = () => new Date(); exports.today = today; // Shortcuts Relative const yesterday = () => subtract((0, exports.today)(), 1, 'day'); exports.yesterday = yesterday; const tomorrow = () => add((0, exports.today)(), 1, 'day'); exports.tomorrow = tomorrow; const nextMonth = () => add((0, exports.today)(), 1, 'month'); exports.nextMonth = nextMonth; const lastMonth = () => subtract((0, exports.today)(), 1, 'month'); exports.lastMonth = lastMonth; const nextYear = () => add((0, exports.today)(), 1, 'year'); exports.nextYear = nextYear; const lastYear = () => subtract((0, exports.today)(), 1, 'year'); exports.lastYear = lastYear; // Shortcuts Start/End const startOfWeek = (date = (0, exports.today)()) => change(date, date.getDate() - date.getDay(), 'day'); exports.startOfWeek = startOfWeek; const endOfWeek = (date = (0, exports.today)()) => change(date, date.getDate() + (6 - date.getDay()), 'day'); exports.endOfWeek = endOfWeek; const startOfMonth = (date = (0, exports.today)()) => change(date, 1, 'day'); exports.startOfMonth = startOfMonth; const endOfMonth = (date = (0, exports.today)()) => subtract(add((0, exports.startOfMonth)(date), 1, 'month'), 1, 'day'); exports.endOfMonth = endOfMonth; const startOfYear = (date = (0, exports.today)()) => change(change(date, 1, 'day'), 1, 'month'); exports.startOfYear = startOfYear; const endOfYear = (date = (0, exports.today)()) => subtract(add((0, exports.startOfYear)(date), 1, 'year'), 1, 'day'); exports.endOfYear = endOfYear; const startOfDay = (date = (0, exports.today)()) => { const newValue = new Date(date); newValue.setHours(0, 0, 0, 0); return newValue; }; exports.startOfDay = startOfDay; const endOfDay = (date = (0, exports.today)()) => { const newValue = new Date(date); newValue.setHours(23, 59, 59, 999); return newValue; }; exports.endOfDay = endOfDay; function change(date, value, granularity) { const newValue = new Date(date); const numberValue = (Number.isFinite(value)) ? value : parseInt(value); switch (granularity) { case "minute": newValue.setMinutes(numberValue); break; case "hour": newValue.setHours(numberValue); break; case "day": newValue.setDate(numberValue); break; case "TOD": if (get(newValue, 'TOD') !== value) { if (value === 'AM') { return subtract(newValue, 12, 'hour'); } else { return add(newValue, 12, 'hour'); } } break; case "month": newValue.setMonth(numberValue - 1); break; case "monthIndex": newValue.setMonth(numberValue); break; case "year": newValue.setFullYear(numberValue); break; } return newValue; } function get(date, granularity, locale = 'default', options = {}) { if (!date) return null; const timeChunks = date.toLocaleTimeString(locale, { hour12: true, hour: 'numeric', minute: 'numeric', second: 'numeric', ...options }); switch (granularity) { case "minute": return timeChunks.split(':')[1]; case "hour": return timeChunks.split(':')[0]; case "24Hour": const time24Chunks = date.toLocaleTimeString(locale, { hour12: false, hour: 'numeric', minute: 'numeric', second: 'numeric', ...options }); return time24Chunks.split(':')[0]; case "TOD": return timeChunks.split(' ')[1]; case "day": return date.toLocaleString(locale, { day: 'numeric', ...options }); case "month": return date.toLocaleString(locale, { month: 'numeric', ...options }); case "monthIndex": return `${(parseInt(date.toLocaleString(locale, { month: 'numeric', ...options })) - 1)}`; case "year": return date.toLocaleString(locale, { year: 'numeric', ...options }); } } function add(date, amount, granularity) { switch (granularity) { case "minute": return change(date, date.getMinutes() + amount, 'minute'); case "hour": return change(date, date.getHours() + amount, 'hour'); case "day": return change(date, date.getDate() + amount, 'day'); case "month": return change(date, date.getMonth() + amount, 'monthIndex'); case "year": return change(date, date.getFullYear() + amount, 'year'); default: return date; } } function subtract(date, amount, granularity) { return add(date, amount * -1, granularity); } function dayIsInFrontForCurrentLocale() { return (new Date(2020, 11, 31).toLocaleDateString('default').indexOf('31') < 2); } function parse(dateValue) { if (dateValue instanceof Date) return new Date(dateValue); if (Number.isFinite(dateValue)) return fromTimestamp(dateValue); if (typeof dateValue === 'string') return fromISO(dateValue); return new Date(dateValue); } function fromISO(dateString) { if (!dateString) return null; if (dateString instanceof Date) return dateString; if (dateString.includes('T')) return new Date(dateString); const parts = dateString.split('-'); return new Date(parseInt(parts[0]), parseInt(parts[1]) - 1, parseInt(parts[2])); } function toISODateTime(date) { if (!date) return null; return date.toISOString(); } function toISODate(date) { if (!date) return null; // Using the `get` method here uses the toLocaleString method to return the value as the user sees it. // This is critical for date only ISO strings as we don't care about the timezone. return `${get(date, 'year')}-${get(date, 'month', 'default', { month: '2-digit' })}-${get(date, 'day', 'default', { day: '2-digit' })}`; } function fromTimestamp(number) { if (!number) return null; return new Date(number); } function toTimestamp(date) { if (!date) return null; return date.getTime(); } function getMonthNames(locale = 'default', format = 'long') { const months = []; // Use a fixed year (e.g., 2017) and the 1st day to avoid issues with month rollovers // for months that have fewer than 31 days (e.g., February 31st is invalid) for (let i = 0; i < 12; i++) { const date = new Date(2017, i, 1); months.push(date.toLocaleString(locale, { month: format })); } return months; } function getMonthNamesStartingWith(monthNamePart, locale = 'default', format = 'long') { const monthAbbrevs = getMonthNames(locale, format); return monthAbbrevs.filter(abbrev => abbrev.toLowerCase().startsWith(monthNamePart.toLowerCase())); } function monthIndexToMonthNumber(monthIndex) { return isFinite(monthIndex) ? monthIndex + 1 : null; } function monthNumberToMonthIndex(monthNumber) { return (`${monthNumber}` || '').length > 0 ? (parseInt(`${monthNumber}`, 10) - 1) : null; } function monthIndexToAbbrev(monthIndex) { return isFinite(monthIndex) ? getMonthNames('default', 'short')[monthIndex] : null; } function monthAbbrevToMonthIndex(monthAbbrev) { if (!monthAbbrev) return null; const abrevs = getMonthNames('default', 'short'); for (let i = 0; i < abrevs.length; i++) { if (abrevs[i].toLowerCase().startsWith(monthAbbrev.toLowerCase())) return i; // Check if the next value starts with a valid month abbreviation } return null; } function isEqual(one, two, granularity = 'day') { if (!one || !two) return false; switch (granularity) { case "day": return isEqual(one, two, 'month') && one.getDate() === two.getDate(); case "month": return isEqual(one, two, 'year') && one.getMonth() === two.getMonth(); case "year": return one.getFullYear() === two.getFullYear(); case "time": case "minute": return isEqual(one, two, 'day') && one.getHours() === two.getHours() && one.getMinutes() === two.getMinutes(); default: return false; } } 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 = (0, exports.startOfMonth)(visibleDate).getDay(); const totalDays = (0, exports.endOfMonth)(visibleDate).getDate(); return Math.ceil((firstDay + totalDays) / 7); } function nameForVisibleDates(visibleDate, visibleMonths = 1, showYear = true) { if (visibleMonths <= 1 || !isFinite(visibleMonths)) { return visibleDate.toLocaleString('default', { month: 'long', year: showYear ? 'numeric' : undefined }); } const startMonth = (0, exports.startOfMonth)(visibleDate); const endMonth = (0, exports.startOfMonth)(add(visibleDate, visibleMonths - 1, 'month')); return [ nameForVisibleDates(startMonth, 1, !isEqual(startMonth, endMonth, 'year')), nameForVisibleDates(endMonth) ].join(' – '); } //# sourceMappingURL=utils.js.map