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.
115 lines • 3.56 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(),
});
}
static compare(first, second) {
const a = toCalendarDay(first);
const b = toCalendarDay(second);
if (a.equalTo(b)) {
return 0;
}
return a.greaterThan(b) ? 1 : -1;
}
constructor(args) {
this._date = new Date(args.year, args.month, args.date ?? 1);
}
clone() {
return CalendarDay.from(this._date);
}
set(args) {
const year = args.year ?? this.year;
const month = args.month ?? this.month;
const date = args.date ?? this.date;
const temp = new Date(year, month, date);
if (date > 0 && temp.getMonth() !== month) {
return new CalendarDay({
year,
month,
date: new Date(year, month + 1, 0).getDate(),
});
}
return new CalendarDay({ year, month, 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;
}
greaterThanOrEqual(value) {
return this.timestamp >= toCalendarDay(value).timestamp;
}
lessThan(value) {
return this.timestamp < toCalendarDay(value).timestamp;
}
lessThanOrEqual(value) {
return this.timestamp <= toCalendarDay(value).timestamp;
}
toString() {
return `${this.native}`;
}
}
//# sourceMappingURL=model.js.map