@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
130 lines (128 loc) • 3.57 kB
JavaScript
import {
seconds
} from "./chunk-JQQGQCYH.mjs";
import {
isDate
} from "./chunk-ADJAEGCT.mjs";
import {
choose,
tryTo
} from "./chunk-XAIHCZT4.mjs";
import {
Value
} from "./chunk-WSBULPUZ.mjs";
import {
ifDefined
} from "./chunk-JSON7A4X.mjs";
import {
isA
} from "./chunk-ZHXKBOK2.mjs";
import {
use
} from "./chunk-ZPNFXK7Y.mjs";
import {
isDefined,
isNumber,
isString
} from "./chunk-DEJ7A5PY.mjs";
// src/domain/DateTime.ts
import { DateTime as LuxonDateTime, Settings } from "luxon";
Settings.defaultZone = "utc";
var DateTime = class _DateTime extends Value {
luxon;
constructor(value, format) {
const luxon = choose(value).type(isString, (v) => format ? LuxonDateTime.fromFormat(v, format, { setZone: true }) : LuxonDateTime.fromISO(v, { setZone: true })).type(isNumber, (v) => LuxonDateTime.fromMillis(v)).type(isDate, (v) => LuxonDateTime.fromJSDate(v)).type(isDateTime, (v) => v.luxon).else(value instanceof LuxonDateTime ? value : LuxonDateTime.fromISO(void 0));
super(luxon.toISO() ?? void 0);
this.luxon = luxon;
}
static get now() {
return new _DateTime(LuxonDateTime.utc());
}
get isValid() {
return isDefined(this.value) && this.utc.isValid;
}
/**
* @deprecated Deprecated in favor for DateTime.from as that also accepts locales and another DateTime
*/
get fromNow() {
return this.from();
}
get utc() {
return this.luxon.setZone("utc");
}
from(dateOrLocale, maybeLocale) {
return use(
(isString(dateOrLocale) ? dateOrLocale : maybeLocale) ?? "en",
(locale) => ifDefined(
isA(dateOrLocale) ? dateOrLocale : void 0,
(d) => this.utc.setLocale(locale).toRelative({ base: d.utc }),
() => this.utc.setLocale(locale).toRelative()
)
) ?? "";
}
isAfter(dt2) {
return this.utc > dt2.utc;
}
isBefore(dt2) {
return this.utc < dt2.utc;
}
equals(dt2) {
return this.utc.hasSame(dt2.utc, "millisecond");
}
add(n, unit = "day") {
return new _DateTime(this.luxon.plus(isNumber(n) ? { [unit]: n } : n));
}
subtract(n, unit = "day") {
return new _DateTime(this.luxon.minus(isNumber(n) ? { [unit]: n } : n));
}
diff(other, unit = "day", opts) {
return Math[opts?.rounding ?? "floor"](this.utc.diff(other.utc).as(unit));
}
startOf(unit = "day") {
return new _DateTime(this.luxon.startOf(unit));
}
endOf(unit = "day") {
return new _DateTime(this.luxon.endOf(unit));
}
isWeekend() {
return this.luxon.isWeekend;
}
withZone(zone) {
return new _DateTime(this.luxon.setZone(zone));
}
toString() {
return this.value ?? "";
}
toJSON() {
return this.utc.toISO();
}
toFormat(format) {
return this.luxon.toFormat(format);
}
toLocale(locale = "nl-NL", format = "D") {
return this.luxon.setLocale(locale).toFormat(format);
}
toFull(locale) {
return this.toLocale(locale, "DDD");
}
toDate() {
return this.isValid ? this.utc.toJSDate() : void 0;
}
toEpoch() {
return this.luxon.toMillis();
}
ago(end = _DateTime.now) {
return seconds.toText(end.diff(this, "second"));
}
withClock(clock) {
return tryTo(() => [this.toDate(), clock.toDate()]).map(([td, cd]) => new Date(td.getFullYear(), td.getMonth(), td.getDate(), cd.getHours(), cd.getMinutes(), cd.getSeconds())).map((d) => new _DateTime(d)).value;
}
};
var isDateTime = (dt2) => isDefined(dt2) && dt2 instanceof DateTime;
var dt = (dt2) => new DateTime(dt2);
export {
DateTime,
isDateTime,
dt
};
//# sourceMappingURL=chunk-BMZN3WKC.mjs.map