@helpscout/hsds-react
Version:
React component library for Help Scout's Design System
232 lines (193 loc) • 7.26 kB
JavaScript
exports.__esModule = true;
exports.isJSDate = isJSDate;
exports.getJSDateFromString = getJSDateFromString;
exports.getHumanReadableDate = getHumanReadableDate;
exports.isToday = isToday;
exports.isMonthInThePast = isMonthInThePast;
exports.isInsideRange = isInsideRange;
exports.getValidDateTimeString = getValidDateTimeString;
exports.getLeadingDays = getLeadingDays;
exports.calculateNumberOfLeadingDays = calculateNumberOfLeadingDays;
exports.getTrailingDays = getTrailingDays;
exports.getTotalDaysSlots = getTotalDaysSlots;
exports.getActiveYearRange = getActiveYearRange;
exports.getNavigatorButtonLabel = getNavigatorButtonLabel;
var _Datepicker = require("./Datepicker.constants");
/**
* Check if something is JS Date Object
* @param {*} someObject the item to check
*
* @return {boolean} Whether someObject is a JS Date Object
*/
function isJSDate(someObject) {
return someObject && typeof someObject.getMonth === 'function';
}
/**
* Convert a valid date string to a JS Date Object, bypass if the string is already a Date Object
* @param {string} someDateString
*
* @return {Object} a Date object
*/
function getJSDateFromString(someDateString) {
if (!someDateString) return null;
if (isJSDate(someDateString)) return someDateString;
return new Date(someDateString);
}
/**
* Convert a date string in the form of '2021-09-27' to 'September 27, 2021'
* @param {string} date The date to convert
* @returns string
*/
function getHumanReadableDate(date) {
var d = new Date(date);
return _Datepicker.MONTHS[d.getUTCMonth()] + " " + d.getUTCDate() + ", " + d.getUTCFullYear();
}
/**
* Check if a Date is today
* @param {Object} someDate Date object to check
*
* @return {boolean} true if the provided Date object is today
*/
function isToday(someDate) {
var today = new Date();
return someDate.getDate() === today.getDate() && someDate.getMonth() === today.getMonth() && someDate.getFullYear() === today.getFullYear();
}
/**
* Check whether the month in a given Date is in the past
* @param {Object} someDate Date object to check
*
* @return {boolean} true if the provided Date's month is in the past
*/
function isMonthInThePast(someDate) {
var today = new Date();
var thisYear = today.getFullYear();
var someYear = someDate.getFullYear();
if (someYear === thisYear) {
return someDate.getMonth() < today.getMonth();
}
return someYear < thisYear;
}
function isInsideRange(_ref) {
var check = _ref.check,
to = _ref.to,
from = _ref.from;
return check.getTime() <= to.getTime() && check.getTime() >= from.getTime();
}
/**
* Extracts a string in the form of `YYYY-MM-DD`
* @param {Object} someDate Date object to get the string from
*
* @return {string} date string in the form of `YYYY-MM-DD`
*/
function getValidDateTimeString(someDate) {
if (!someDate) return '';
var dateToConvert = getJSDateFromString(someDate);
var y = "" + dateToConvert.getFullYear();
var m = "" + (dateToConvert.getMonth() + 1).toString().padStart(2, '0');
var d = "" + dateToConvert.getDate().toString().padStart(2, '0');
return y + "-" + m + "-" + d;
}
/**
* In order to fill up the calendar, get the dates from the previous month in relation to the active month
* @param {Object} someDate Date object to check
* @param {number} startDay Which day the week starts with (Sunday 0, Monday 1, etc)
*
* @return {Object[]} Array of Date Objects
*/
function getLeadingDays(someDate, startDay) {
if (startDay === void 0) {
startDay = 1;
}
var leadingDays = [];
var year = someDate.getFullYear();
var month = someDate.getMonth();
var firstOfTheMonthWeekday = new Date(year, month, 1).getDay();
var days = calculateNumberOfLeadingDays(startDay, firstOfTheMonthWeekday);
for (var i = (days - 1) * -1; i <= 0; i++) {
leadingDays.push({
date: new Date(year, month, i),
dayLabel: new Date(year, month, i).getDate(),
leading: true
});
}
return leadingDays;
}
/**
* Get the number of leading dates needed based on the selected start day of the week
* and which day of the week the first of the month falls on
* @param {number} startDay Which day the week starts with (Sunday 0, Monday 1, etc)
* @param {number} firstOfTheMonthWeekday The weekday that is the first of the month
*
* @return {number} The amount of leading days needed to fill the start of the monthly calendar
*/
function calculateNumberOfLeadingDays(startDay, firstOfTheMonthWeekday) {
return startDay > firstOfTheMonthWeekday ? 7 + firstOfTheMonthWeekday - startDay : firstOfTheMonthWeekday - startDay;
}
/**
* In order to fill up the calendar, get the dates from the next month in relation to the active month
* @param {Object} someDate Date object to check
* @param {Object[]} leadingDays The "leading" days taken from the previous month (see `getLeadingDays`)
* @param {Object[]} allMonthDays The current month days as given by datepicker hooks
*
* @return {Object[]} Array of Date Objects
*/
function getTrailingDays(someDate, leadingDays, allMonthDays) {
var trailingDays = [];
var someDateCopy = new Date(someDate.toString()); // avoid date mutation below
var monthDays = allMonthDays.filter(function (day) {
return typeof day === 'object';
}); // If you need to have a fixed height on the calendar, set `totalDaySlots` to 42
var totalDaySlots = getTotalDaysSlots(monthDays.length + leadingDays.length);
var days = totalDaySlots - (leadingDays.length + monthDays.length);
var nextMonth = new Date(someDateCopy.setMonth(someDate.getMonth() + 1, 1));
var year = someDate.getFullYear();
for (var i = 1; i <= days; i++) {
trailingDays.push({
date: new Date(year, nextMonth.getMonth(), i),
dayLabel: new Date(year, nextMonth.getMonth(), i).getDate(),
trailing: true
});
}
return trailingDays;
}
/**
* Determine how many trailing days we need to fill up the calendar
* @param {number} numberOfDays
*
* @return {number} number of trailing days that fill up the calendar
*/
function getTotalDaysSlots(numberOfDays) {
if (numberOfDays === 28) return 28;
if (numberOfDays > 35) return 42;
return 35;
}
/**
* Get an array of 12 years that surround a given year to fill up the Period Calendar in "multi year" mode
* @param {number|string} activeYear the year to put in the middle of the grid (index 4 of 12)
* @param {boolean} isInitial is the range generated for the first time
*
* @return {number[]} Range of years
*/
function getActiveYearRange(activeYear, isInitial) {
var RANGE = 12;
var middleIndex = 4;
var startRange = isInitial ? Number.parseInt(activeYear) - middleIndex : Number.parseInt(activeYear) - RANGE;
var yearRange = [];
for (var index = 0; index < RANGE; index++) {
yearRange.push(startRange + index);
}
return yearRange;
}
function getNavigatorButtonLabel(navigationLevel, direction) {
switch (navigationLevel) {
case _Datepicker.NAVIGATION_LEVELS.MONTH_BY_MONTH:
return direction + " month";
case _Datepicker.NAVIGATION_LEVELS.YEAR_BY_YEAR:
return direction + " year";
case _Datepicker.NAVIGATION_LEVELS.YEAR_RANGES:
return direction + " years";
default:
return '';
}
}
;