UNPKG

@3mo/date-time-fields

Version:

Date time fields let people select dates, date-ranges, and times.

64 lines (63 loc) 3.46 kB
export class FieldDateTimePrecision { static get all() { return FieldDateTimePrecision._all; } static parse(value) { return !value ? undefined : this._all.find(precision => precision.key === value); } constructor(value, key) { this.value = value; this.key = key; FieldDateTimePrecision._all.push(this); } get formatOptions() { return { year: 'numeric', month: this < FieldDateTimePrecision.Month ? undefined : '2-digit', day: this < FieldDateTimePrecision.Day ? undefined : '2-digit', hour: this < FieldDateTimePrecision.Hour ? undefined : '2-digit', minute: this < FieldDateTimePrecision.Minute ? undefined : '2-digit', second: this < FieldDateTimePrecision.Second ? undefined : '2-digit', hourCycle: 'h23', }; } getRange(date) { const start = date.with({ year: date.year, month: this < FieldDateTimePrecision.Month ? 1 : date.month, day: this < FieldDateTimePrecision.Day ? 1 : date.day, hour: this < FieldDateTimePrecision.Hour ? 0 : date.hour, minute: this < FieldDateTimePrecision.Minute ? 0 : date.minute, second: this < FieldDateTimePrecision.Second ? 0 : date.second, }); return new DateTimeRange(start, start.add({ [`${this.key}s`]: 1 }).subtract({ seconds: 1 })); } equals(left, right) { return left.year === right.year && (this < FieldDateTimePrecision.Month || left.month === right.month) && (this < FieldDateTimePrecision.Day || left.day === right.day) && (this < FieldDateTimePrecision.Hour || left.hour === right.hour) && (this < FieldDateTimePrecision.Minute || left.minute === right.minute) && (this < FieldDateTimePrecision.Second || left.second === right.second); } isSmallerThan(left, right) { return left.year < right.year || (this >= FieldDateTimePrecision.Month && left.year === right.year && left.month < right.month) || (this >= FieldDateTimePrecision.Day && left.year === right.year && left.month === right.month && left.day < right.day) || (this >= FieldDateTimePrecision.Hour && left.year === right.year && left.month === right.month && left.day === right.day && left.hour < right.hour) || (this >= FieldDateTimePrecision.Minute && left.year === right.year && left.month === right.month && left.day === right.day && left.hour === right.hour && left.minute < right.minute) || (this >= FieldDateTimePrecision.Second && left.year === right.year && left.month === right.month && left.day === right.day && left.hour === right.hour && left.minute === right.minute && left.second < right.second); } valueOf() { return this.value; } toString() { return this.key; } } FieldDateTimePrecision._all = new Array(); FieldDateTimePrecision.Year = new FieldDateTimePrecision(1, 'year'); FieldDateTimePrecision.Month = new FieldDateTimePrecision(2, 'month'); // static readonly Week = new FieldDateTimePrecision(3, 'week') FieldDateTimePrecision.Day = new FieldDateTimePrecision(4, 'day'); FieldDateTimePrecision.Hour = new FieldDateTimePrecision(5, 'hour'); FieldDateTimePrecision.Minute = new FieldDateTimePrecision(6, 'minute'); FieldDateTimePrecision.Second = new FieldDateTimePrecision(7, 'second');