UNPKG

@gez/date-time-kit

Version:

107 lines (106 loc) 4.52 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); class Granularity { constructor() { __publicField(this, "has", (v) => this.map.has(v)); __publicField(this, "idx", ((...v) => v.length === 1 ? this.map.get(v[0]) : v.map((g) => this.map.get(g)))); __publicField(this, "_fromIdx", (idx) => { if (idx < 0 || idx >= this.list.length) throw new Error( "".concat(this.constructor.name, ".getByIdx: index out of range: ").concat(idx) ); return this.list[idx]; }); __publicField(this, "fromIdx", ((...idx) => idx.length === 1 ? this._fromIdx(idx[0]) : idx.map((i) => this._fromIdx(i)))); __publicField(this, "cp", (a, b) => this.idx(a) - this.idx(b)); __publicField(this, "lt", (a, b) => this.cp(a, b) < 0); __publicField(this, "lte", (a, b) => this.cp(a, b) <= 0); __publicField(this, "gt", (a, b) => this.cp(a, b) > 0); __publicField(this, "gte", (a, b) => this.cp(a, b) >= 0); __publicField(this, "minmax", (v1, v2) => this.lt(v1, v2) ? [v1, v2] : [v2, v1]); __publicField(this, "minmaxIdx", (v1, v2) => this.idx(...this.minmax(v1, v2))); __publicField(this, "min", (...args) => { if (args.length === 0) throw new Error( "".concat(this.constructor.name, ".min needs at least one argument") ); return args.reduce( (prev, curr) => this.cp(prev, curr) <= 0 ? prev : curr ); }); __publicField(this, "max", (...args) => { if (args.length === 0) throw new Error( "".concat(this.constructor.name, ".max needs at least one argument") ); return args.reduce( (prev, curr) => this.cp(prev, curr) >= 0 ? prev : curr ); }); __publicField(this, "clamp", (min, val, max, rule = "min") => { [min, max] = this.minmax(min, max); if (!this.has(val)) return rule === "min" ? min : max; return this.min(this.max(min, val), max); }); } } class _DateGranularity extends Granularity { constructor() { super(...arguments); __publicField(this, "year", "year"); __publicField(this, "month", "month"); __publicField(this, "day", "day"); __publicField(this, "list", [this.day, this.month, this.year]); __publicField(this, "map", new Map(this.list.map((v, idx) => [v, idx]))); } } export const dateGranHelper = new _DateGranularity(); class _TimeGranularity extends Granularity { constructor() { super(...arguments); __publicField(this, "hour", "hour"); __publicField(this, "minute", "minute"); __publicField(this, "second", "second"); __publicField(this, "millisecond", "millisecond"); __publicField(this, "list", [ this.millisecond, this.second, this.minute, this.hour ]); __publicField(this, "map", new Map(this.list.map((v, idx) => [v, idx]))); } } export const timeGranHelper = new _TimeGranularity(); class _DateTimeGranularity extends Granularity { constructor() { super(...arguments); __publicField(this, "year", dateGranHelper.year); __publicField(this, "month", dateGranHelper.month); __publicField(this, "day", dateGranHelper.day); __publicField(this, "hour", timeGranHelper.hour); __publicField(this, "minute", timeGranHelper.minute); __publicField(this, "second", timeGranHelper.second); __publicField(this, "millisecond", timeGranHelper.millisecond); __publicField(this, "list", [ ...timeGranHelper.list, ...dateGranHelper.list ]); __publicField(this, "map", new Map(this.list.map((v, idx) => [v, idx]))); __publicField(this, "toTimeGran", (gran) => this.clamp(this.hour, gran, this.millisecond)); __publicField(this, "toDateGran", (gran) => this.clamp(this.year, gran, this.day)); __publicField(this, "isTimeGran", (gran) => timeGranHelper.has(gran)); __publicField(this, "isDateGran", (gran) => dateGranHelper.has(gran)); } } export const dateTimeGranHelper = new _DateTimeGranularity(); export const granHelper = { date: dateGranHelper, time: timeGranHelper, dateTime: dateTimeGranHelper, toDateGran: dateTimeGranHelper.toDateGran, toTimeGran: dateTimeGranHelper.toTimeGran, isTimeGran: dateTimeGranHelper.isTimeGran, isDateGran: dateTimeGranHelper.isDateGran };