@leancodepl/api-date-dayjs
Version:
Day.js integration for API date type conversion
400 lines (399 loc) • 17.4 kB
JavaScript
export * from "@leancodepl/api-date";
import H from "dayjs";
import { parseApiTimeSpan as dt } from "@leancodepl/api-date-utils";
function K(i) {
return i && i.__esModule && Object.prototype.hasOwnProperty.call(i, "default") ? i.default : i;
}
var N = { exports: {} }, lt = N.exports, rt;
function mt() {
return rt || (rt = 1, (function(i, U) {
(function(Y, d) {
i.exports = d();
})(lt, (function() {
var Y = { LTS: "h:mm:ss A", LT: "h:mm A", L: "MM/DD/YYYY", LL: "MMMM D, YYYY", LLL: "MMMM D, YYYY h:mm A", LLLL: "dddd, MMMM D, YYYY h:mm A" }, d = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g, S = /\d/, O = /\d\d/, M = /\d\d?/, g = /\d*[^-_:/,()\s\d]+/, c = {}, C = function(r) {
return (r = +r) + (r > 68 ? 1900 : 2e3);
}, v = function(r) {
return function(e) {
this[r] = +e;
};
}, F = [/[+-]\d\d:?(\d\d)?|Z/, function(r) {
(this.zone || (this.zone = {})).offset = (function(e) {
if (!e || e === "Z") return 0;
var f = e.match(/([+-]|\d\d)/g), o = 60 * f[1] + (+f[2] || 0);
return o === 0 ? 0 : f[0] === "+" ? -o : o;
})(r);
}], T = function(r) {
var e = c[r];
return e && (e.indexOf ? e : e.s.concat(e.f));
}, k = function(r, e) {
var f, o = c.meridiem;
if (o) {
for (var y = 1; y <= 24; y += 1) if (r.indexOf(o(y, 0, e)) > -1) {
f = y > 12;
break;
}
} else f = r === (e ? "pm" : "PM");
return f;
}, L = { A: [g, function(r) {
this.afternoon = k(r, !1);
}], a: [g, function(r) {
this.afternoon = k(r, !0);
}], Q: [S, function(r) {
this.month = 3 * (r - 1) + 1;
}], S: [S, function(r) {
this.milliseconds = 100 * +r;
}], SS: [O, function(r) {
this.milliseconds = 10 * +r;
}], SSS: [/\d{3}/, function(r) {
this.milliseconds = +r;
}], s: [M, v("seconds")], ss: [M, v("seconds")], m: [M, v("minutes")], mm: [M, v("minutes")], H: [M, v("hours")], h: [M, v("hours")], HH: [M, v("hours")], hh: [M, v("hours")], D: [M, v("day")], DD: [O, v("day")], Do: [g, function(r) {
var e = c.ordinal, f = r.match(/\d+/);
if (this.day = f[0], e) for (var o = 1; o <= 31; o += 1) e(o).replace(/\[|\]/g, "") === r && (this.day = o);
}], w: [M, v("week")], ww: [O, v("week")], M: [M, v("month")], MM: [O, v("month")], MMM: [g, function(r) {
var e = T("months"), f = (T("monthsShort") || e.map((function(o) {
return o.slice(0, 3);
}))).indexOf(r) + 1;
if (f < 1) throw new Error();
this.month = f % 12 || f;
}], MMMM: [g, function(r) {
var e = T("months").indexOf(r) + 1;
if (e < 1) throw new Error();
this.month = e % 12 || e;
}], Y: [/[+-]?\d+/, v("year")], YY: [O, function(r) {
this.year = C(r);
}], YYYY: [/\d{4}/, v("year")], Z: F, ZZ: F };
function n(r) {
var e, f;
e = r, f = c && c.formats;
for (var o = (r = e.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (function(l, h, m) {
var $ = m && m.toUpperCase();
return h || f[m] || Y[m] || f[$].replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (function(p, x, A) {
return x || A.slice(1);
}));
}))).match(d), y = o.length, D = 0; D < y; D += 1) {
var u = o[D], s = L[u], t = s && s[0], a = s && s[1];
o[D] = a ? { regex: t, parser: a } : u.replace(/^\[|\]$/g, "");
}
return function(l) {
for (var h = {}, m = 0, $ = 0; m < y; m += 1) {
var p = o[m];
if (typeof p == "string") $ += p.length;
else {
var x = p.regex, A = p.parser, Z = l.slice($), b = x.exec(Z)[0];
A.call(h, b), l = l.replace(b, "");
}
}
return (function(P) {
var j = P.afternoon;
if (j !== void 0) {
var w = P.hours;
j ? w < 12 && (P.hours += 12) : w === 12 && (P.hours = 0), delete P.afternoon;
}
})(h), h;
};
}
return function(r, e, f) {
f.p.customParseFormat = !0, r && r.parseTwoDigitYear && (C = r.parseTwoDigitYear);
var o = e.prototype, y = o.parse;
o.parse = function(D) {
var u = D.date, s = D.utc, t = D.args;
this.$u = s;
var a = t[1];
if (typeof a == "string") {
var l = t[2] === !0, h = t[3] === !0, m = l || h, $ = t[2];
h && ($ = t[2]), c = this.$locale(), !l && $ && (c = f.Ls[$]), this.$d = (function(Z, b, P, j) {
try {
if (["x", "X"].indexOf(b) > -1) return new Date((b === "X" ? 1e3 : 1) * Z);
var w = n(b)(Z), R = w.year, z = w.month, at = w.day, ut = w.hours, ft = w.minutes, ct = w.seconds, ht = w.milliseconds, tt = w.zone, st = w.week, W = /* @__PURE__ */ new Date(), Q = at || (R || z ? 1 : W.getDate()), X = R || W.getFullYear(), E = 0;
R && !z || (E = z > 0 ? z - 1 : W.getMonth());
var q, J = ut || 0, V = ft || 0, B = ct || 0, G = ht || 0;
return tt ? new Date(Date.UTC(X, E, Q, J, V, B, G + 60 * tt.offset * 1e3)) : P ? new Date(Date.UTC(X, E, Q, J, V, B, G)) : (q = new Date(X, E, Q, J, V, B, G), st && (q = j(q).week(st).toDate()), q);
} catch {
return /* @__PURE__ */ new Date("");
}
})(u, a, s, f), this.init(), $ && $ !== !0 && (this.$L = this.locale($).$L), m && u != this.format(a) && (this.$d = /* @__PURE__ */ new Date("")), c = {};
} else if (a instanceof Array) for (var p = a.length, x = 1; x <= p; x += 1) {
t[1] = a[x - 1];
var A = f.apply(this, t);
if (A.isValid()) {
this.$d = A.$d, this.$L = A.$L, this.init();
break;
}
x === p && (this.$d = /* @__PURE__ */ new Date(""));
}
else y.call(this, D);
};
};
}));
})(N)), N.exports;
}
var $t = mt();
const it = /* @__PURE__ */ K($t);
H.extend(it);
function Ht(i) {
if (i)
return H(i, "HH:mm:ss.SSS");
}
function Ot(i) {
if (i)
return i.format("HH:mm:ss.SSS");
}
function Lt(i) {
if (i)
return H(i);
}
var I = { exports: {} }, vt = I.exports, nt;
function pt() {
return nt || (nt = 1, (function(i, U) {
(function(Y, d) {
i.exports = d();
})(vt, (function() {
var Y = "minute", d = /[+-]\d\d(?::?\d\d)?/g, S = /([+-]|\d\d)/g;
return function(O, M, g) {
var c = M.prototype;
g.utc = function(n) {
var r = { date: n, utc: !0, args: arguments };
return new M(r);
}, c.utc = function(n) {
var r = g(this.toDate(), { locale: this.$L, utc: !0 });
return n ? r.add(this.utcOffset(), Y) : r;
}, c.local = function() {
return g(this.toDate(), { locale: this.$L, utc: !1 });
};
var C = c.parse;
c.parse = function(n) {
n.utc && (this.$u = !0), this.$utils().u(n.$offset) || (this.$offset = n.$offset), C.call(this, n);
};
var v = c.init;
c.init = function() {
if (this.$u) {
var n = this.$d;
this.$y = n.getUTCFullYear(), this.$M = n.getUTCMonth(), this.$D = n.getUTCDate(), this.$W = n.getUTCDay(), this.$H = n.getUTCHours(), this.$m = n.getUTCMinutes(), this.$s = n.getUTCSeconds(), this.$ms = n.getUTCMilliseconds();
} else v.call(this);
};
var F = c.utcOffset;
c.utcOffset = function(n, r) {
var e = this.$utils().u;
if (e(n)) return this.$u ? 0 : e(this.$offset) ? F.call(this) : this.$offset;
if (typeof n == "string" && (n = (function(D) {
D === void 0 && (D = "");
var u = D.match(d);
if (!u) return null;
var s = ("" + u[0]).match(S) || ["-", 0, 0], t = s[0], a = 60 * +s[1] + +s[2];
return a === 0 ? 0 : t === "+" ? a : -a;
})(n), n === null)) return this;
var f = Math.abs(n) <= 16 ? 60 * n : n, o = this;
if (r) return o.$offset = f, o.$u = n === 0, o;
if (n !== 0) {
var y = this.$u ? this.toDate().getTimezoneOffset() : -1 * this.utcOffset();
(o = this.local().add(f + y, Y)).$offset = f, o.$x.$localOffset = y;
} else o = this.utc();
return o;
};
var T = c.format;
c.format = function(n) {
var r = n || (this.$u ? "YYYY-MM-DDTHH:mm:ss[Z]" : "");
return T.call(this, r);
}, c.valueOf = function() {
var n = this.$utils().u(this.$offset) ? 0 : this.$offset + (this.$x.$localOffset || this.$d.getTimezoneOffset());
return this.$d.valueOf() - 6e4 * n;
}, c.isUTC = function() {
return !!this.$u;
}, c.toISOString = function() {
return this.toDate().toISOString();
}, c.toString = function() {
return this.toDate().toUTCString();
};
var k = c.toDate;
c.toDate = function(n) {
return n === "s" && this.$offset ? g(this.format("YYYY-MM-DD HH:mm:ss:SSS")).toDate() : k.call(this);
};
var L = c.diff;
c.diff = function(n, r, e) {
if (n && this.$u === n.$u) return L.call(this, n, r, e);
var f = this.local(), o = g(n).local();
return L.call(f, o, r, e);
};
};
}));
})(I)), I.exports;
}
var Mt = pt();
const Dt = /* @__PURE__ */ K(Mt);
H.extend(Dt);
function Yt(i) {
return i.clone().utc(!0);
}
function bt(i) {
if (i)
return Yt(i).startOf("day").format("YYYY-MM-DD");
}
H.extend(it);
function kt(i) {
if (i)
return H(i, "YYYY-MM-DDTHH:mm:ss.SSSZ");
}
function At(i) {
if (i)
return i.format("YYYY-MM-DDTHH:mm:ss.SSSZ");
}
var _ = { exports: {} }, gt = _.exports, et;
function yt() {
return et || (et = 1, (function(i, U) {
(function(Y, d) {
i.exports = d();
})(gt, (function() {
var Y, d, S = 1e3, O = 6e4, M = 36e5, g = 864e5, c = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g, C = 31536e6, v = 2628e6, F = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/, T = { years: C, months: v, days: g, hours: M, minutes: O, seconds: S, milliseconds: 1, weeks: 6048e5 }, k = function(u) {
return u instanceof y;
}, L = function(u, s, t) {
return new y(u, t, s.$l);
}, n = function(u) {
return d.p(u) + "s";
}, r = function(u) {
return u < 0;
}, e = function(u) {
return r(u) ? Math.ceil(u) : Math.floor(u);
}, f = function(u) {
return Math.abs(u);
}, o = function(u, s) {
return u ? r(u) ? { negative: !0, format: "" + f(u) + s } : { negative: !1, format: "" + u + s } : { negative: !1, format: "" };
}, y = (function() {
function u(t, a, l) {
var h = this;
if (this.$d = {}, this.$l = l, t === void 0 && (this.$ms = 0, this.parseFromMilliseconds()), a) return L(t * T[n(a)], this);
if (typeof t == "number") return this.$ms = t, this.parseFromMilliseconds(), this;
if (typeof t == "object") return Object.keys(t).forEach((function(p) {
h.$d[n(p)] = t[p];
})), this.calMilliseconds(), this;
if (typeof t == "string") {
var m = t.match(F);
if (m) {
var $ = m.slice(2).map((function(p) {
return p != null ? Number(p) : 0;
}));
return this.$d.years = $[0], this.$d.months = $[1], this.$d.weeks = $[2], this.$d.days = $[3], this.$d.hours = $[4], this.$d.minutes = $[5], this.$d.seconds = $[6], this.calMilliseconds(), this;
}
}
return this;
}
var s = u.prototype;
return s.calMilliseconds = function() {
var t = this;
this.$ms = Object.keys(this.$d).reduce((function(a, l) {
return a + (t.$d[l] || 0) * T[l];
}), 0);
}, s.parseFromMilliseconds = function() {
var t = this.$ms;
this.$d.years = e(t / C), t %= C, this.$d.months = e(t / v), t %= v, this.$d.days = e(t / g), t %= g, this.$d.hours = e(t / M), t %= M, this.$d.minutes = e(t / O), t %= O, this.$d.seconds = e(t / S), t %= S, this.$d.milliseconds = t;
}, s.toISOString = function() {
var t = o(this.$d.years, "Y"), a = o(this.$d.months, "M"), l = +this.$d.days || 0;
this.$d.weeks && (l += 7 * this.$d.weeks);
var h = o(l, "D"), m = o(this.$d.hours, "H"), $ = o(this.$d.minutes, "M"), p = this.$d.seconds || 0;
this.$d.milliseconds && (p += this.$d.milliseconds / 1e3, p = Math.round(1e3 * p) / 1e3);
var x = o(p, "S"), A = t.negative || a.negative || h.negative || m.negative || $.negative || x.negative, Z = m.format || $.format || x.format ? "T" : "", b = (A ? "-" : "") + "P" + t.format + a.format + h.format + Z + m.format + $.format + x.format;
return b === "P" || b === "-P" ? "P0D" : b;
}, s.toJSON = function() {
return this.toISOString();
}, s.format = function(t) {
var a = t || "YYYY-MM-DDTHH:mm:ss", l = { Y: this.$d.years, YY: d.s(this.$d.years, 2, "0"), YYYY: d.s(this.$d.years, 4, "0"), M: this.$d.months, MM: d.s(this.$d.months, 2, "0"), D: this.$d.days, DD: d.s(this.$d.days, 2, "0"), H: this.$d.hours, HH: d.s(this.$d.hours, 2, "0"), m: this.$d.minutes, mm: d.s(this.$d.minutes, 2, "0"), s: this.$d.seconds, ss: d.s(this.$d.seconds, 2, "0"), SSS: d.s(this.$d.milliseconds, 3, "0") };
return a.replace(c, (function(h, m) {
return m || String(l[h]);
}));
}, s.as = function(t) {
return this.$ms / T[n(t)];
}, s.get = function(t) {
var a = this.$ms, l = n(t);
return l === "milliseconds" ? a %= 1e3 : a = l === "weeks" ? e(a / T[l]) : this.$d[l], a || 0;
}, s.add = function(t, a, l) {
var h;
return h = a ? t * T[n(a)] : k(t) ? t.$ms : L(t, this).$ms, L(this.$ms + h * (l ? -1 : 1), this);
}, s.subtract = function(t, a) {
return this.add(t, a, !0);
}, s.locale = function(t) {
var a = this.clone();
return a.$l = t, a;
}, s.clone = function() {
return L(this.$ms, this);
}, s.humanize = function(t) {
return Y().add(this.$ms, "ms").locale(this.$l).fromNow(!t);
}, s.valueOf = function() {
return this.asMilliseconds();
}, s.milliseconds = function() {
return this.get("milliseconds");
}, s.asMilliseconds = function() {
return this.as("milliseconds");
}, s.seconds = function() {
return this.get("seconds");
}, s.asSeconds = function() {
return this.as("seconds");
}, s.minutes = function() {
return this.get("minutes");
}, s.asMinutes = function() {
return this.as("minutes");
}, s.hours = function() {
return this.get("hours");
}, s.asHours = function() {
return this.as("hours");
}, s.days = function() {
return this.get("days");
}, s.asDays = function() {
return this.as("days");
}, s.weeks = function() {
return this.get("weeks");
}, s.asWeeks = function() {
return this.as("weeks");
}, s.months = function() {
return this.get("months");
}, s.asMonths = function() {
return this.as("months");
}, s.years = function() {
return this.get("years");
}, s.asYears = function() {
return this.as("years");
}, u;
})(), D = function(u, s, t) {
return u.add(s.years() * t, "y").add(s.months() * t, "M").add(s.days() * t, "d").add(s.hours() * t, "h").add(s.minutes() * t, "m").add(s.seconds() * t, "s").add(s.milliseconds() * t, "ms");
};
return function(u, s, t) {
Y = t, d = t().$utils(), t.duration = function(h, m) {
var $ = t.locale();
return L(h, { $l: $ }, m);
}, t.isDuration = k;
var a = s.prototype.add, l = s.prototype.subtract;
s.prototype.add = function(h, m) {
return k(h) ? D(this, h, 1) : a.bind(this)(h, m);
}, s.prototype.subtract = function(h, m) {
return k(h) ? D(this, h, -1) : l.bind(this)(h, m);
};
};
}));
})(_)), _.exports;
}
var St = yt();
const ot = /* @__PURE__ */ K(St);
H.extend(ot);
function Ct(i) {
if (!i)
return;
const U = dt(i), Y = U.sign === "-", d = H.duration(U.values);
return Y ? H.duration(-d.asMilliseconds()) : d;
}
H.extend(ot);
function Ut(i) {
if (!i)
return;
const U = i.asMilliseconds() < 0, Y = H.duration(Math.abs(i.asMilliseconds())), d = Math.floor(Y.asDays());
let S = "";
return U && (S += "-"), d > 0 && (S += `${d}.`), S += Y.format("HH:mm:ss.SSS"), S;
}
export {
Yt as dropLocalOffset,
Lt as fromApiDate,
kt as fromApiDateTimeOffset,
Ht as fromApiTime,
Ct as fromApiTimeSpan,
bt as toApiDate,
At as toApiDateTimeOffset,
Ot as toApiTime,
Ut as toApiTimeSpan
};