dayjs-plugin-time-interval
Version:
A Dayjs plugin to add support for time intervals
105 lines (103 loc) • 4 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_duration = __toESM(require("dayjs/plugin/duration"));
var plugin = (_options, _dayjsClass, dayjsFactory) => {
if (!dayjsFactory.duration) {
dayjsFactory.extend(import_duration.default);
}
dayjsFactory.timeInterval = function(config) {
if (typeof config === "string") {
const [start, end] = config.split("/");
return new TimeInterval((0, import_dayjs.default)(start), (0, import_dayjs.default)(end));
}
return new TimeInterval(
"start" in config ? (0, import_dayjs.default)(config.start) : (0, import_dayjs.default)(config.end).add(
import_dayjs.default.isDuration(config.duration) ? config.duration : import_dayjs.default.duration(config.duration)
),
"end" in config ? (0, import_dayjs.default)(config.end) : (0, import_dayjs.default)(config.start).add(
import_dayjs.default.isDuration(config.duration) ? config.duration : import_dayjs.default.duration(config.duration)
)
);
};
dayjsFactory.isTimeInterval = function(t) {
return t instanceof TimeInterval;
};
};
var index_default = plugin;
var TimeInterval = class _TimeInterval {
constructor(start, end) {
this.start = start;
this.end = end;
}
get duration() {
return import_dayjs.default.duration(this.end.diff(this.start));
}
clone() {
return new _TimeInterval(this.start, this.end);
}
isValid(unit) {
return this.start.isBefore(this.end, unit);
}
isSame(other, unit) {
return this.start.isSame(other.start, unit) && this.end.isSame(other.end, unit);
}
overlaps(other, unit) {
return this.start.isBefore(other.end, unit) && other.start.isBefore(this.end, unit);
}
includes(d, unit) {
return this.end.isAfter(d, unit) && (this.start.isBefore(d, unit) || this.start.isSame(d, unit));
}
withStart(start) {
const newStart = typeof start === "function" ? start(this.start) : start;
return new _TimeInterval((0, import_dayjs.default)(newStart), this.end);
}
withEnd(end) {
const newEnd = typeof end === "function" ? end(this.end) : end;
return new _TimeInterval(this.start, (0, import_dayjs.default)(newEnd));
}
toJSON() {
return this.toISOString();
}
toISOString() {
return `${this.start.toISOString()}/${this.end.toISOString()}`;
}
format(formatStr) {
return {
start: this.start.format(formatStr),
end: this.end.format(formatStr)
};
}
};