@hitachivantara/uikit-react-core
Version:
UI Kit Core React components.
254 lines (253 loc) • 9.51 kB
JavaScript
import { capitalize } from "../utils/helpers.js";
/**
* Returns the number of days in the month given a month and year.
*
* @param month - Number of the month (1 to 12).
* @param year - Number of the year.
* @returns The number of days in a month for the received year.
*/
var getMonthDays = (month, year) => new Date(year, month, 0).getDate();
/**
* Gets the week day of the first day of a given month and year.
* From 0 (Sunday) to 6 (Saturday).
*
* @param month - Number of the month (1 to 12).
* @param year - Number of the year.
* @returns The zero indexed week day where 0 is Sunday (0 to 6).
*/
var getMonthFirstWeekday = (month, year) => new Date(year, month - 1, 1).getDay();
/**
* Creates a `Date` instance in UTC timezone.
*
* @param year - The year of the date.
* @param monthIndex - The zero indexed month of the year (0 to 11).
* @param day - The day of the month.
* @param hour - The hour of the day.
* @returns A `Date` instance in UTC timezone.
*/
var makeUTCDate = (year, monthIndex, day, hour = 1) => new Date(Date.UTC(year, monthIndex, day, hour));
/**
* Checks if the received date is a valid date.
*
* @param date - The date to be validated.
* @returns A flag stating if the date is valid or not.
*/
var isDate = (date) => Object.prototype.toString.call(date) === "[object Date]" && !Number.isNaN(date.valueOf());
var isDateRangeProp = (date) => "startDate" in date;
/**
* Checks if two dates are in the same month and year.
*
* @param date1 - First date.
* @param date2 - Second date.
* @returns A flag stating if the dates are in the same month and year or not.
*/
var isSameMonth = (date1, date2) => {
if (!(isDate(date1) && isDate(date2))) return false;
return date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear();
};
/**
* Checks if two dates are on the same day.
*
* @param date1 - First date.
* @param date2 - Second date.
* @returns A flag stating if the dates are in the same day or not.
*/
var isSameDay = (date1, date2) => {
if (!(isDate(date1) && isDate(date2))) return false;
return date1.getDate() === date2.getDate() && date1.getMonth() === date2.getMonth() && date1.getFullYear() === date2.getFullYear();
};
/**
* Formats the received date using the ISO format (YYYY-MM-DD).
*
* @param date - The date to be formatted.
* @returns The formatted date in ISO format.
*/
var getDateISO = (date) => {
const d = new Date(date);
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
};
/**
* Returns an object with the previous month taking also into consideration the year.
* For example the previous month of January 2000 will be December 1999.
*
* @param month - Number of the month.
* @param year - Number of the year.
* @returns Object with new month and year defined.
*/
var getPreviousMonth = (month, year) => {
return {
month: month > 1 ? month - 1 : 12,
year: month > 1 ? year : year - 1
};
};
/**
* Returns an object with the next month taking also into consideration the year.
* For example the next month of December 2000 will be January 2001.
*
* @param month - Number of the month.
* @param year - Number of the year.
* @returns Object with new month and year defined.
*/
var getNextMonth = (month, year) => {
return {
month: month < 12 ? month + 1 : 1,
year: month < 12 ? year : year + 1
};
};
/**
* Returns a list with the names of all the months localized in the received locale and representation value.
*
* @param locale - The locale to be applied to the Intl format.
* @param representationValue - The representation value for the month.
* @returns An array with all the months names.
*/
var getMonthNamesList = (locale, representationValue = "long") => {
const options = {
month: representationValue,
timeZone: "UTC"
};
return [...Array(12).keys()].map((index) => {
const auxDate = makeUTCDate(1970, index, 1);
return capitalize(Intl.DateTimeFormat(locale, options).format(auxDate));
});
};
/**
* Returns a list with the names of all the weekdays localized in the received locale and representation value.
*
* @param locale - The locale to be applied.
* @returns An array with all the weekday names.
*/
var getWeekdayNamesList = (locale) => {
const formatter = new Intl.DateTimeFormat(locale, {
weekday: "narrow",
timeZone: "UTC"
});
return [...Array(7).keys()].map((index) => {
return formatter.format(makeUTCDate(1970, 0, 4 + index));
});
};
/**
* Returns the name of the month for the supplied month localized in the received locale and representation value.
*
* @param date - The date from which the month name is extracted.
* @param locale - The locale to be applied to the Intl format.
* @param representationValue - The locale to be applied to the Intl format.
* @returns The name of the month.
*/
var getMonthName = (date, locale, representationValue = "long") => new Intl.DateTimeFormat(locale, { month: representationValue }).format(date);
/**
* Formats the received date according to the user's locale & Design System specifications.
*
* @param date - UTC date to be formatted.
* @param locale - The locale to be applied to the Intl format.
* @returns The formatted date as a string.
*/
var getFormattedDate = (date, locale) => {
return new Intl.DateTimeFormat(locale, { dateStyle: "medium" }).format(date);
};
/**
* Creates an array of 42 days. The complete current month and enough days from the previous and next months to fill
* the 42 positions.
*
* @param month - The number of the month (1 to 12).
* @param year - The number of the year.
* @returns The array of dates.
*/
var createDatesArray = (month, year) => {
const monthDays = getMonthDays(month, year);
const daysFromPrevMonth = getMonthFirstWeekday(month, year);
const daysFromNextMonth = 42 - (daysFromPrevMonth + monthDays);
const prevMonthYear = getPreviousMonth(month, year);
const nextMonthYear = getNextMonth(month, year);
const prevMonthDays = getMonthDays(prevMonthYear.month, prevMonthYear.year);
const prevMonthDates = [...Array(daysFromPrevMonth).keys()].map((index) => {
const day = index + 1 + (prevMonthDays - daysFromPrevMonth);
return new Date(prevMonthYear.year, prevMonthYear.month - 1, day);
});
const currentMonthDates = [...Array(monthDays).keys()].map((index) => {
const day = index + 1;
return new Date(year, month - 1, day);
});
const nextMonthDates = [...Array(daysFromNextMonth).keys()].map((index) => {
const day = index + 1;
return new Date(nextMonthYear.year, nextMonthYear.month - 1, day);
});
return [
...prevMonthDates,
...currentMonthDates,
...nextMonthDates
];
};
var isRange = (date) => date != null && typeof date === "object" && "startDate" in date;
/**
* Checks if the date falls within a specified date range.
*
* @param date - The date to be evaluated.
* @param dateRange - Provided selection range.
* @returns - True if the date falls within the range, false otherwise.
*/
var dateInProvidedValueRange = (date, dateRange) => {
if (!isRange(dateRange) || !dateRange?.endDate) return false;
const { startDate, endDate } = dateRange;
const modStartDate = getDateISO(startDate);
const modEndDate = getDateISO(endDate);
const convertedDate = getDateISO(date ?? /* @__PURE__ */ new Date());
return convertedDate >= modStartDate && convertedDate <= modEndDate;
};
var checkIfDateIsDisabled = (date, minimumDate, maximumDate) => {
if (!minimumDate && !maximumDate) return false;
const modStartDate = minimumDate && getDateISO(minimumDate);
const modEndDate = maximumDate && getDateISO(maximumDate);
const convertedDate = getDateISO(date ?? /* @__PURE__ */ new Date());
return modStartDate !== void 0 && convertedDate < modStartDate || modEndDate !== void 0 && convertedDate > modEndDate;
};
/**
* gets the *localized* date formatter that's editable via input
* @example getEditableDateFormatter("pt") // "DD/MM/YYYY"
*/
function getEditableDateFormatter(locale) {
return new Intl.DateTimeFormat(locale, {
year: "numeric",
month: "2-digit",
day: "2-digit"
});
}
function getStringFromDate(date, locale) {
return getEditableDateFormatter(locale).format(date);
}
/**
* attempts to format a localized `dateString`
* @returns `Date` or `null` if parsing fails
* @example parseDateString("04/06/2020", "pt")
*/
function parseDateString(dateString, locale) {
const dateParts = dateString.split(/\D+/).map(Number);
if (dateParts.length !== 3) return null;
if (!dateParts.every(Boolean)) return null;
const formatOrder = getEditableDateFormatter(locale).formatToParts(new Date(2020, 4, 4)).filter((part) => [
"year",
"month",
"day"
].includes(part.type)).map((part) => part.type);
const dateObject = {
year: 2020,
month: 4,
day: 4
};
formatOrder.forEach((type, index) => {
dateObject[type] = dateParts[index];
});
return new Date(dateObject.year, dateObject.month - 1, dateObject.day);
}
function getLocaleDateFormat(locale) {
const formatter = getEditableDateFormatter(locale);
const getPartType = (part) => {
if (part.type === "year") return "YYYY";
if (part.type === "month") return "MM";
if (part.type === "day") return "DD";
return part.value;
};
return formatter.formatToParts(new Date(2020, 4, 4)).reduce((acc, part) => acc + getPartType(part), "");
}
//#endregion
export { checkIfDateIsDisabled, createDatesArray, dateInProvidedValueRange, getFormattedDate, getLocaleDateFormat, getMonthName, getMonthNamesList, getStringFromDate, getWeekdayNamesList, isDate, isDateRangeProp, isRange, isSameDay, isSameMonth, parseDateString };