@trellixio/roaster-coffee
Version:
Beans' product component library
161 lines (160 loc) • 4.96 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
const WEEK_LENGTH = 7;
export const WEEKDAYS = [0, 1, 2, 3, 4, 5, 6];
export 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;
}
export function isSameDay(day1, day2) {
return (day1.getDate() === day2.getDate() &&
day1.getMonth() === day2.getMonth() &&
day1.getFullYear() === day2.getFullYear());
}
export function dateIsSelected(day, date) {
if (day == null) {
return false;
}
return isSameDay(date, day);
}
export function getNextDisplayMonth(month) {
if (month === 11) {
return 0;
}
return month + 1;
}
export function getNextDisplayYear(month, year) {
if (month === 11) {
return year + 1;
}
return year;
}
export function getPreviousDisplayMonth(month) {
if (month === 0) {
return 11;
}
return month - 1;
}
export function getPreviousDisplayYear(month, year) {
if (month === 0) {
return year - 1;
}
return year;
}
export function isDateAfter(date, dateToCompare) {
return date.getTime() > dateToCompare.getTime();
}
export function isDateBefore(date, dateToCompare) {
return date.getTime() < dateToCompare.getTime();
}
export function isDateDisabled(date, datesToCompare) {
return datesToCompare.some((dateToCompare) => {
return date.getTime() === dateToCompare.getTime();
});
}
export function monthName(month) {
return [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
][month];
}
export function weekdayName(weekday) {
return ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'][weekday];
}
export const parseDate = (date) => `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date
.getDate()
.toString()
.padStart(2, '0')}`;
export 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}`;
};
export 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;
}
export function dateStringParser(dateString) {
const date = new Date(dateString);
if (Number.isNaN(date.getTime()) || !dateString) {
return null;
}
return date;
}
export function getSeparatorIndex(dateFormat) {
if (dateFormat === 'DD MM YYYY')
return { first: 2, second: 5 };
return { first: 4, second: 7 };
}
export function noop() { }
export function pickCalendarProps(props) {
const { value, month = new Date().getMonth(), year = new Date().getFullYear(), onInputError, minDate, maxDate, disableSpecificDates, dateFormat = 'YYYY MM DD', separator = '-', defaultValue, onChange = noop } = props, others = __rest(props, ["value", "month", "year", "onInputError", "minDate", "maxDate", "disableSpecificDates", "dateFormat", "separator", "defaultValue", "onChange"]);
return {
calendarProps: {
value,
month,
maxDate,
minDate,
year,
dateFormat,
separator,
defaultValue,
disableSpecificDates,
onChange,
onInputError,
},
others,
};
}