igniteui-webcomponents
Version:
Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.
94 lines • 2.84 kB
JavaScript
export const daysInWeek = 7;
const millisecondsInDay = 86400000;
export function toCalendarDay(date) {
return date instanceof Date ? CalendarDay.from(date) : date;
}
function checkRollover(original, modified) {
return original.date !== modified.date ? modified.set({ date: 0 }) : modified;
}
export class CalendarDay {
static get today() {
return CalendarDay.from(new Date());
}
static from(date) {
return new CalendarDay({
year: date.getFullYear(),
month: date.getMonth(),
date: date.getDate(),
});
}
constructor(args) {
this._date = new Date(args.year, args.month, args.date ?? 1);
}
clone() {
return CalendarDay.from(this._date);
}
set(args) {
return new CalendarDay({
year: args.year ?? this.year,
month: args.month ?? this.month,
date: args.date ?? this.date,
});
}
add(unit, value) {
const result = this.clone();
switch (unit) {
case 'year':
result._date.setFullYear(result.year + value);
return checkRollover(this, result);
case 'quarter':
result._date.setMonth(result.month + 3 * value);
return checkRollover(this, result);
case 'month':
result._date.setMonth(result.month + value);
return checkRollover(this, result);
case 'week':
result._date.setDate(result.date + 7 * value);
return result;
case 'day':
result._date.setDate(result.date + value);
return result;
default:
throw new Error('Invalid interval');
}
}
get day() {
return this._date.getDay();
}
get year() {
return this._date.getFullYear();
}
get month() {
return this._date.getMonth();
}
get date() {
return this._date.getDate();
}
get timestamp() {
return this._date.getTime();
}
get week() {
const firstDay = new CalendarDay({ year: this.year, month: 0 }).timestamp;
const currentDay = (this.timestamp - firstDay + millisecondsInDay) / millisecondsInDay;
return Math.ceil(currentDay / daysInWeek);
}
get native() {
return new Date(this._date);
}
get weekend() {
return this.day < 1 || this.day > 5;
}
equalTo(value) {
return this.timestamp === toCalendarDay(value).timestamp;
}
greaterThan(value) {
return this.timestamp > toCalendarDay(value).timestamp;
}
lessThan(value) {
return this.timestamp < toCalendarDay(value).timestamp;
}
toString() {
return `${this.native}`;
}
}
//# sourceMappingURL=model.js.map