@trellixio/roaster-coffee
Version:
Beans' product component library
179 lines (176 loc) • 4.54 kB
JavaScript
;
const WEEK_LENGTH = 7;
const WEEKDAYS = [0, 1, 2, 3, 4, 5, 6];
function getWeeksForMonth(month, year) {
const firstOfMonth = new Date(year, month, 1);
const firstDayOfWeek = firstOfMonth.getDay();
const weeks = [[]];
let currentWeek = weeks[0];
let currentDate = firstOfMonth;
for (let i = 0; i < WEEKDAYS.indexOf(firstDayOfWeek); i += 1) {
currentWeek.push(null);
}
const fillMonth = () => {
if (currentWeek.length === WEEK_LENGTH) {
currentWeek = [];
weeks.push(currentWeek);
}
currentWeek.push(currentDate);
currentDate = new Date(year, month, currentDate.getDate() + 1);
if (currentDate.getMonth() === month) fillMonth();
};
fillMonth();
return weeks;
}
function isSameDay(day1, day2) {
return day1.getDate() === day2.getDate() && day1.getMonth() === day2.getMonth() && day1.getFullYear() === day2.getFullYear();
}
function dateIsSelected(day, date) {
if (day == null) {
return false;
}
return isSameDay(date, day);
}
function getNextDisplayMonth(month) {
if (month === 11) {
return 0;
}
return month + 1;
}
function getNextDisplayYear(month, year) {
if (month === 11) {
return year + 1;
}
return year;
}
function getPreviousDisplayMonth(month) {
if (month === 0) {
return 11;
}
return month - 1;
}
function getPreviousDisplayYear(month, year) {
if (month === 0) {
return year - 1;
}
return year;
}
function isDateAfter(date, dateToCompare) {
return date.getTime() > dateToCompare.getTime();
}
function isDateBefore(date, dateToCompare) {
return date.getTime() < dateToCompare.getTime();
}
function isDateDisabled(date, datesToCompare) {
return datesToCompare.some((dateToCompare) => {
return date.getTime() === dateToCompare.getTime();
});
}
function monthName(month) {
return [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
][month];
}
function weekdayName(weekday) {
return ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"][weekday];
}
const formatDate = (date, separator = "-", dateFormat = "YYYY MM DD") => {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
if (dateFormat === "DD MM YYYY") {
return `${day}${separator}${month}${separator}${year}`;
}
return `${year}${separator}${month}${separator}${day}`;
};
function isDateValid({ date, maxDate, minDate }) {
if (date == null) {
return false;
}
if (Number.isNaN(date.getTime())) {
return false;
}
if (maxDate && isDateAfter(date, maxDate)) {
return false;
}
if (minDate && isDateBefore(date, minDate)) {
return false;
}
return true;
}
function dateStringParser(dateString) {
const date = new Date(dateString);
if (Number.isNaN(date.getTime()) || !dateString) {
return null;
}
return date;
}
function getSeparatorIndex(dateFormat) {
if (dateFormat === "DD MM YYYY") return { first: 2, second: 5 };
return { first: 4, second: 7 };
}
function noop() {
}
function pickCalendarProps(props) {
const {
value,
month = (/* @__PURE__ */ new Date()).getMonth(),
year = (/* @__PURE__ */ new Date()).getFullYear(),
onInputError,
minDate,
maxDate,
disableSpecificDates,
dateFormat = "YYYY MM DD",
separator = "-",
defaultValue,
onChange = noop,
...others
} = props;
return {
calendarProps: {
value,
month,
maxDate,
minDate,
year,
dateFormat,
separator,
defaultValue,
disableSpecificDates,
onChange,
onInputError
},
others
};
}
exports.WEEKDAYS = WEEKDAYS;
exports.dateIsSelected = dateIsSelected;
exports.dateStringParser = dateStringParser;
exports.formatDate = formatDate;
exports.getNextDisplayMonth = getNextDisplayMonth;
exports.getNextDisplayYear = getNextDisplayYear;
exports.getPreviousDisplayMonth = getPreviousDisplayMonth;
exports.getPreviousDisplayYear = getPreviousDisplayYear;
exports.getSeparatorIndex = getSeparatorIndex;
exports.getWeeksForMonth = getWeeksForMonth;
exports.isDateAfter = isDateAfter;
exports.isDateBefore = isDateBefore;
exports.isDateDisabled = isDateDisabled;
exports.isDateValid = isDateValid;
exports.isSameDay = isSameDay;
exports.monthName = monthName;
exports.noop = noop;
exports.pickCalendarProps = pickCalendarProps;
exports.weekdayName = weekdayName;
//# sourceMappingURL=index.js.map