@coreui/react-pro
Version:
UI Components Library for React.js
495 lines (492 loc) • 18.2 kB
JavaScript
import { __spreadArray } from '../../node_modules/tslib/tslib.es6.js';
/**
* Converts an ISO week string to a Date object representing the Monday of that week.
* @param isoWeek - The ISO week string (e.g., "2023W05" or "2023w05").
* @returns The Date object for the Monday of the specified week, or null if invalid.
*/
var convertIsoWeekToDate = function (isoWeek) {
var _a = isoWeek.split(/[Ww]/), year = _a[0], week = _a[1];
var date = new Date(Number(year), 0, 4); // 4th Jan is always in week 1
date.setDate(date.getDate() - (date.getDay() || 7) + 1 + (Number(week) - 1) * 7);
return date;
};
/**
* Converts a date string or Date object to a Date object based on selection type.
* @param date - The date to convert.
* @param selectionType - The type of selection ('day', 'week', 'month', 'year').
* @returns The corresponding Date object or null if invalid.
*/
var convertToDateObject = function (date, selectionType) {
if (date instanceof Date) {
return date;
}
if (selectionType === 'week') {
return convertIsoWeekToDate(date);
}
if (selectionType === 'month' || selectionType === 'year') {
var _date = new Date(Date.parse(date));
var userTimezoneOffset = _date.getTimezoneOffset() * 60000;
return new Date(_date.getTime() + userTimezoneOffset);
}
return new Date(Date.parse(date));
};
/**
* Creates groups from an array.
* @param arr - The array to group.
* @param numberOfGroups - Number of groups to create.
* @returns An array of grouped arrays.
*/
var createGroupsInArray = function (arr, numberOfGroups) {
var perGroup = Math.ceil(arr.length / numberOfGroups);
return Array.from({ length: numberOfGroups })
.fill('')
.map(function (_, i) { return arr.slice(i * perGroup, (i + 1) * perGroup); });
};
/**
* Adjusts the calendar date based on order and view type.
* @param calendarDate - The current calendar date.
* @param order - The order to adjust by.
* @param view - The current view type.
* @returns The adjusted Date object.
*/
var getCalendarDate = function (calendarDate, order, view) {
if (order !== 0 && view === 'days') {
return new Date(calendarDate.getFullYear(), calendarDate.getMonth() + order, 1);
}
if (order !== 0 && view === 'months') {
return new Date(calendarDate.getFullYear() + order, calendarDate.getMonth(), 1);
}
if (order !== 0 && view === 'years') {
return new Date(calendarDate.getFullYear() + 12 * order, calendarDate.getMonth(), 1);
}
return calendarDate;
};
/**
* Formats a date based on the selection type.
* @param date - The date to format.
* @param selectionType - The type of selection ('day', 'week', 'month', 'year').
* @returns A formatted date string or the original Date object.
*/
var getDateBySelectionType = function (date, selectionType) {
if (date === null) {
return null;
}
if (selectionType === 'week') {
return "".concat(date.getFullYear(), "W").concat(getWeekNumber(date));
}
if (selectionType === 'month') {
var monthNumber = "0".concat(date.getMonth() + 1).slice(-2);
return "".concat(date.getFullYear(), "-").concat(monthNumber);
}
if (selectionType === 'year') {
return "".concat(date.getFullYear());
}
return date;
};
/**
* Retrieves an array of month names based on locale and format.
* @param locale - The locale string (e.g., 'en-US').
* @param format - The format of the month names ('short' or 'long').
* @returns An array of month names.
*/
var getMonthsNames = function (locale, format) {
if (format === void 0) { format = 'short'; }
return Array.from({ length: 12 }, function (_, i) {
return new Date(2000, i, 1).toLocaleString(locale, { month: format });
});
};
/**
* Retrieves an array of selectable dates from the given element.
* @param element - The HTML element to search for selectable dates.
* @param selector - The CSS selector used to identify selectable dates. Defaults to 'tr[tabindex="0"], td[tabindex="0"]'.
* @returns An array of HTMLElements representing the selectable dates.
*/
var getSelectableDates = function (element, selector) {
if (selector === void 0) { selector = 'tr[tabindex="0"], td[tabindex="0"]'; }
return __spreadArray([], Element.prototype.querySelectorAll.call(element, selector), true);
};
/**
* Generates an array of years centered around a given year.
* @param year - The central year.
* @param range - The number of years before and after the central year.
* @returns An array of years.
*/
var getYears = function (year, range) {
if (range === void 0) { range = 6; }
return Array.from({ length: range * 2 }, function (_, i) { return year - range + i; });
};
/**
* Retrieves leading days (from the previous month) for a calendar view.
* @param year - The year.
* @param month - The month (0-11).
* @param firstDayOfWeek - The first day of the week (0-6, where 0 is Sunday).
* @returns An array of leading day objects.
*/
var getLeadingDays = function (year, month, firstDayOfWeek) {
// 0: sunday
// 1: monday
var dates = [];
var d = new Date(year, month);
var y = d.getFullYear();
var m = d.getMonth();
var firstWeekday = new Date(y, m, 1).getDay();
var leadingDays = 6 - (6 - firstWeekday) - firstDayOfWeek;
if (firstDayOfWeek) {
leadingDays = leadingDays < 0 ? 7 + leadingDays : leadingDays;
}
for (var i = leadingDays * -1; i < 0; i++) {
dates.push({
date: new Date(y, m, i + 1),
month: 'previous',
});
}
return dates;
};
/**
* Retrieves all days within a specific month.
* @param year - The year.
* @param month - The month (0-11).
* @returns An array of day objects.
*/
var getMonthDays = function (year, month) {
var dates = [];
var lastDay = new Date(year, month + 1, 0).getDate();
for (var i = 1; i <= lastDay; i++) {
dates.push({
date: new Date(year, month, i),
month: 'current',
});
}
return dates;
};
/**
* Retrieves trailing days (from the next month) for a calendar view.
* @param year - The year.
* @param month - The month (0-11).
* @param leadingDays - Array of leading day objects.
* @param monthDays - Array of current month day objects.
* @returns An array of trailing day objects.
*/
var getTrailingDays = function (year, month, leadingDays, monthDays) {
var dates = [];
var days = 42 - (leadingDays.length + monthDays.length);
for (var i = 1; i <= days; i++) {
dates.push({
date: new Date(year, month + 1, i),
month: 'next',
});
}
return dates;
};
/**
* Calculates the ISO week number for a given date.
* @param date - The date to calculate the week number for.
* @returns The ISO week number.
*/
var getWeekNumber = function (date) {
var tempDate = new Date(date.getTime());
tempDate.setHours(0, 0, 0, 0);
// Thursday in current week decides the year
tempDate.setDate(tempDate.getDate() + 3 - ((tempDate.getDay() + 6) % 7));
var week1 = new Date(tempDate.getFullYear(), 0, 4);
// Calculate full weeks to the date
var weekNumber = 1 + Math.round((tempDate.getTime() - week1.getTime()) / 86400000 / 7);
return weekNumber;
};
/**
* Retrieves detailed information about each week in a month for calendar rendering.
* @param year - The year.
* @param month - The month (0-11).
* @param firstDayOfWeek - The first day of the week (0-6, where 0 is Sunday).
* @returns An array of week objects containing week numbers and day details.
*/
var getMonthDetails = function (year, month, firstDayOfWeek) {
var daysPrevMonth = getLeadingDays(year, month, firstDayOfWeek);
var daysThisMonth = getMonthDays(year, month);
var daysNextMonth = getTrailingDays(year, month, daysPrevMonth, daysThisMonth);
var days = __spreadArray(__spreadArray(__spreadArray([], daysPrevMonth, true), daysThisMonth, true), daysNextMonth, true);
var weeks = [];
days.forEach(function (day, index) {
if (index % 7 === 0 || weeks.length === 0) {
weeks.push({
days: [],
});
}
if ((index + 1) % 7 === 0) {
weeks[weeks.length - 1].weekNumber = getWeekNumber(day.date);
}
weeks[weeks.length - 1].days.push(day);
});
return weeks;
};
/**
* Checks if a date is disabled based on the 'date' period type.
* @param date - The date to check.
* @param min - Minimum allowed date.
* @param max - Maximum allowed date.
* @param disabledDates - Criteria for disabled dates.
* @returns True if the date is disabled, false otherwise.
*/
var isDateDisabled = function (date, min, max, disabledDates) {
if (min && date < min) {
return true;
}
if (max && date > max) {
return true;
}
if (disabledDates === undefined) {
return false;
}
if (typeof disabledDates === 'function') {
return disabledDates(date);
}
if (disabledDates instanceof Date && isSameDateAs(date, disabledDates)) {
return true;
}
if (Array.isArray(disabledDates) && disabledDates) {
for (var _i = 0, disabledDates_1 = disabledDates; _i < disabledDates_1.length; _i++) {
var _date = disabledDates_1[_i];
if (typeof _date === 'function' && _date(date)) {
return true;
}
if (Array.isArray(_date) && isDateInRange(date, _date[0], _date[1])) {
return true;
}
if (_date instanceof Date && isSameDateAs(date, _date)) {
return true;
}
}
}
return false;
};
/**
* Checks if a date is within a specified range.
* @param date - The date to check.
* @param start - Start date of the range.
* @param end - End date of the range.
* @returns True if the date is within the range, false otherwise.
*/
var isDateInRange = function (date, start, end) {
var _date = removeTimeFromDate(date);
var _start = start ? removeTimeFromDate(start) : null;
var _end = end ? removeTimeFromDate(end) : null;
return !!(_start && _end && _start <= _date && _date <= _end);
};
/**
* Checks if a date is selected based on start and end dates.
* @param date - The date to check.
* @param start - Start date.
* @param end - End date.
* @returns True if the date is selected, false otherwise.
*/
var isDateSelected = function (date, start, end) {
if (start !== null && isSameDateAs(start, date)) {
return true;
}
if (end !== null && isSameDateAs(end, date)) {
return true;
}
return false;
};
/**
* Determines if any date within a range is disabled.
* @param startDate - Start date of the range.
* @param endDate - End date of the range.
* @param disabledDates - Criteria for disabled dates.
* @returns True if any date in the range is disabled, false otherwise.
*/
var isDisableDateInRange = function (startDate, endDate, disabledDates) {
if (startDate && endDate) {
var date = new Date(startDate);
var disabled = false;
while (date < endDate) {
date.setDate(date.getDate() + 1);
if (isDateDisabled(date, null, null, disabledDates)) {
disabled = true;
break;
}
}
return disabled;
}
return false;
};
/**
* Checks if a month is disabled based on the 'month' period type.
* @param date - The date representing the month to check.
* @param min - Minimum allowed date.
* @param max - Maximum allowed date.
* @param disabledDates - Criteria for disabled dates.
* @returns True if the month is disabled, false otherwise.
*/
var isMonthDisabled = function (date, min, max, disabledDates) {
var current = date.getFullYear() * 12 + date.getMonth();
var _min = min ? min.getFullYear() * 12 + min.getMonth() : null;
var _max = max ? max.getFullYear() * 12 + max.getMonth() : null;
if (_min && current < _min) {
return true;
}
if (_max && current > _max) {
return true;
}
if (disabledDates === undefined) {
return false;
}
var start = min ? Math.max(date.getTime(), min.getTime()) : date;
var end = max
? Math.min(date.getTime(), max.getTime())
: new Date(new Date().getFullYear(), 11, 31);
for (var currentDate = new Date(start); currentDate <= end; currentDate.setDate(currentDate.getDate() + 1)) {
if (!isDateDisabled(currentDate, min, max, disabledDates)) {
return false;
}
}
return false;
};
/**
* Checks if a month is selected based on start and end dates.
* @param date - The date representing the month.
* @param start - Start date.
* @param end - End date.
* @returns True if the month is selected, false otherwise.
*/
var isMonthSelected = function (date, start, end) {
var year = date.getFullYear();
var month = date.getMonth();
if (start !== null && year === start.getFullYear() && month === start.getMonth()) {
return true;
}
if (end !== null && year === end.getFullYear() && month === end.getMonth()) {
return true;
}
return false;
};
/**
* Checks if a month is within a specified range.
* @param date - The date representing the month.
* @param start - Start date.
* @param end - End date.
* @returns True if the month is within the range, false otherwise.
*/
var isMonthInRange = function (date, start, end) {
var year = date.getFullYear();
var month = date.getMonth();
var _start = start ? start.getFullYear() * 12 + start.getMonth() : null;
var _end = end ? end.getFullYear() * 12 + end.getMonth() : null;
var _date = year * 12 + month;
return !!(_start && _end && _start <= _date && _date <= _end);
};
/**
* Checks if two dates are the same calendar date.
* @param date - First date.
* @param date2 - Second date.
* @returns True if both dates are the same, false otherwise.
*/
var isSameDateAs = function (date, date2) {
if (date instanceof Date && date2 instanceof Date) {
return (date.getDate() === date2.getDate() &&
date.getMonth() === date2.getMonth() &&
date.getFullYear() === date2.getFullYear());
}
if (date === null && date2 === null) {
return true;
}
return false;
};
/**
* Checks if a date is today.
* @param date - The date to check.
* @returns True if the date is today, false otherwise.
*/
var isToday = function (date) {
var today = new Date();
return isSameDateAs(date, today);
};
/**
* Checks if a year is disabled based on the 'year' period type.
* @param date - The date representing the year to check.
* @param min - Minimum allowed date.
* @param max - Maximum allowed date.
* @param disabledDates - Criteria for disabled dates.
* @returns True if the year is disabled, false otherwise.
*/
var isYearDisabled = function (date, min, max, disabledDates) {
var year = date.getFullYear();
var minYear = min ? min.getFullYear() : null;
var maxYear = max ? max.getFullYear() : null;
if (minYear && year < minYear) {
return true;
}
if (maxYear && year > maxYear) {
return true;
}
if (disabledDates === undefined) {
return false;
}
var start = min ? Math.max(date.getTime(), min.getTime()) : date;
var end = max
? Math.min(date.getTime(), max.getTime())
: new Date(new Date().getFullYear(), 11, 31);
for (var currentDate = new Date(start); currentDate <= end; currentDate.setDate(currentDate.getDate() + 1)) {
if (!isDateDisabled(currentDate, min, max, disabledDates)) {
return false;
}
}
return false;
};
/**
* Checks if a year is selected based on start and end dates.
* @param date - The date representing the year.
* @param start - Start date.
* @param end - End date.
* @returns True if the year matches the start's or end's year, false otherwise.
*/
var isYearSelected = function (date, start, end) {
var year = date.getFullYear();
if (start !== null && year === start.getFullYear()) {
return true;
}
if (end !== null && year === end.getFullYear()) {
return true;
}
return false;
};
/**
* Checks if a year is within a specified range.
* @param date - The date representing the year.
* @param start - Start date.
* @param end - End date.
* @returns True if the year's value lies between start's year and end's year, false otherwise.
*/
var isYearInRange = function (date, start, end) {
var year = date.getFullYear();
var _start = start ? start.getFullYear() : null;
var _end = end ? end.getFullYear() : null;
return !!(_start && _end && _start <= year && year <= _end);
};
/**
* Removes the time component from a Date object.
* @param date - The original date.
* @returns A new Date object with the time set to 00:00:00.
*/
var removeTimeFromDate = function (date) {
var clearedDate = new Date(date);
clearedDate.setHours(0, 0, 0, 0);
return clearedDate;
};
/**
* Copies the time (hours, minutes, seconds, milliseconds) from one Date to another.
*
* @param {Date} target - The date whose time will be updated.
* @param {Date | null} source - The date to copy the time from.
* @returns {Date} A new Date instance with the date from `target` and time from `source`.
*/
var setTimeFromDate = function (target, source) {
if (!(source instanceof Date)) {
return target;
}
var result = new Date(target); // create a copy to avoid mutation
result.setHours(source.getHours(), source.getMinutes(), source.getSeconds(), source.getMilliseconds());
return result;
};
export { convertIsoWeekToDate, convertToDateObject, createGroupsInArray, getCalendarDate, getDateBySelectionType, getMonthDetails, getMonthsNames, getSelectableDates, getWeekNumber, getYears, isDateDisabled, isDateInRange, isDateSelected, isDisableDateInRange, isMonthDisabled, isMonthInRange, isMonthSelected, isSameDateAs, isToday, isYearDisabled, isYearInRange, isYearSelected, removeTimeFromDate, setTimeFromDate };
//# sourceMappingURL=utils.js.map