@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
87 lines (86 loc) • 2.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateInterval = void 0;
const localDate_1 = require("./localDate");
/**
* Class that supports ISO8601 "Time interval" standard that looks like `2022-02-24/2022-03-30`.
*
* @experimental
*/
class DateInterval {
start;
end;
constructor(start, end) {
this.start = start;
this.end = end;
}
static of(start, end) {
return new DateInterval((0, localDate_1.localDate)(start), (0, localDate_1.localDate)(end));
}
/**
* Parses string like `2022-02-24/2023-03-30` into a DateInterval.
*/
static parse(d) {
if (d instanceof DateInterval)
return d;
const [start, end] = d.split('/');
if (!end || !start) {
throw new Error(`Cannot parse "${d}" into DateInterval`);
}
return new DateInterval((0, localDate_1.localDate)(start), (0, localDate_1.localDate)(end));
}
isSame(d) {
return this.cmp(d) === 0;
}
isBefore(d, inclusive = false) {
const r = this.cmp(d);
return r === -1 || (r === 0 && inclusive);
}
isSameOrBefore(d) {
return this.cmp(d) <= 0;
}
isAfter(d, inclusive = false) {
const r = this.cmp(d);
return r === 1 || (r === 0 && inclusive);
}
isSameOrAfter(d) {
return this.cmp(d) >= 0;
}
/**
* Ranges of DateInterval (start, end) are INCLUSIVE.
*/
includes(d, incl = '[]') {
d = (0, localDate_1.localDate)(d);
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
return d.isAfter(this.start, incl[0] === '[') && d.isBefore(this.end, incl[1] === ']');
}
intersects(int, inclusive = true) {
const $int = DateInterval.parse(int);
const incl = inclusive ? '[]' : '()';
return this.includes($int.start, incl) || this.includes($int.end, incl);
}
/**
* DateIntervals compare by start date.
* If it's the same - then by end date.
*/
cmp(d) {
d = DateInterval.parse(d);
return this.start.compare(d.start) || this.end.compare(d.end);
}
getDays(incl = '[]') {
return localDate_1.localDate.range(this.start, this.end, incl, 1, 'day');
}
/**
* Returns an array of LocalDates that are included in the interval.
*/
range(incl = '[]', step = 1, stepUnit = 'day') {
return localDate_1.localDate.range(this.start, this.end, incl, step, stepUnit);
}
toString() {
return [this.start, this.end].join('/');
}
toJSON() {
return this.toString();
}
}
exports.DateInterval = DateInterval;