UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

363 lines (362 loc) 15 kB
import { p as ut } from "../common-2EJV7dbr.js"; function ct(s) { return s && s.__esModule && Object.prototype.hasOwnProperty.call(s, "default") ? s.default : s; } var q = { exports: {} }, ft = q.exports, tt; function ht() { return tt || (tt = 1, function(s, h) { (function(m, M) { s.exports = M(); })(ft, function() { var m = 1e3, M = 6e4, O = 36e5, v = "millisecond", y = "second", b = "minute", S = "hour", Y = "day", N = "week", g = "month", G = "quarter", k = "year", j = "date", X = "Invalid Date", st = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/, at = /\[([^\]]+)]|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, it = { name: "en", weekdays: "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"), months: "January_February_March_April_May_June_July_August_September_October_November_December".split("_"), ordinal: function(a) { var n = ["th", "st", "nd", "rd"], t = a % 100; return "[" + a + (n[(t - 20) % 10] || n[t] || n[0]) + "]"; } }, z = function(a, n, t) { var r = String(a); return !r || r.length >= n ? a : "" + Array(n + 1 - r.length).join(t) + a; }, ot = { s: z, z: function(a) { var n = -a.utcOffset(), t = Math.abs(n), r = Math.floor(t / 60), e = t % 60; return (n <= 0 ? "+" : "-") + z(r, 2, "0") + ":" + z(e, 2, "0"); }, m: function a(n, t) { if (n.date() < t.date()) return -a(t, n); var r = 12 * (t.year() - n.year()) + (t.month() - n.month()), e = n.clone().add(r, g), i = t - e < 0, o = n.clone().add(r + (i ? -1 : 1), g); return +(-(r + (t - e) / (i ? e - o : o - e)) || 0); }, a: function(a) { return a < 0 ? Math.ceil(a) || 0 : Math.floor(a); }, p: function(a) { return { M: g, y: k, w: N, d: Y, D: j, h: S, m: b, s: y, ms: v, Q: G }[a] || String(a || "").toLowerCase().replace(/s$/, ""); }, u: function(a) { return a === void 0; } }, L = "en", H = {}; H[L] = it; var E = "$isDayjsObject", P = function(a) { return a instanceof J || !(!a || !a[E]); }, V = function a(n, t, r) { var e; if (!n) return L; if (typeof n == "string") { var i = n.toLowerCase(); H[i] && (e = i), t && (H[i] = t, e = i); var o = n.split("-"); if (!e && o.length > 1) return a(o[0]); } else { var c = n.name; H[c] = n, e = c; } return !r && e && (L = e), e || !r && L; }, d = function(a, n) { if (P(a)) return a.clone(); var t = typeof n == "object" ? n : {}; return t.date = a, t.args = arguments, new J(t); }, u = ot; u.l = V, u.i = P, u.w = function(a, n) { return d(a, { locale: n.$L, utc: n.$u, x: n.$x, $offset: n.$offset }); }; var J = function() { function a(t) { this.$L = V(t.locale, null, !0), this.parse(t), this.$x = this.$x || t.x || {}, this[E] = !0; } var n = a.prototype; return n.parse = function(t) { this.$d = function(r) { var e = r.date, i = r.utc; if (e === null) return /* @__PURE__ */ new Date(NaN); if (u.u(e)) return /* @__PURE__ */ new Date(); if (e instanceof Date) return new Date(e); if (typeof e == "string" && !/Z$/i.test(e)) { var o = e.match(st); if (o) { var c = o[2] - 1 || 0, f = (o[7] || "0").substring(0, 3); return i ? new Date(Date.UTC(o[1], c, o[3] || 1, o[4] || 0, o[5] || 0, o[6] || 0, f)) : new Date(o[1], c, o[3] || 1, o[4] || 0, o[5] || 0, o[6] || 0, f); } } return new Date(e); }(t), this.init(); }, n.init = function() { var t = this.$d; this.$y = t.getFullYear(), this.$M = t.getMonth(), this.$D = t.getDate(), this.$W = t.getDay(), this.$H = t.getHours(), this.$m = t.getMinutes(), this.$s = t.getSeconds(), this.$ms = t.getMilliseconds(); }, n.$utils = function() { return u; }, n.isValid = function() { return this.$d.toString() !== X; }, n.isSame = function(t, r) { var e = d(t); return this.startOf(r) <= e && e <= this.endOf(r); }, n.isAfter = function(t, r) { return d(t) < this.startOf(r); }, n.isBefore = function(t, r) { return this.endOf(r) < d(t); }, n.$g = function(t, r, e) { return u.u(t) ? this[r] : this.set(e, t); }, n.unix = function() { return Math.floor(this.valueOf() / 1e3); }, n.valueOf = function() { return this.$d.getTime(); }, n.startOf = function(t, r) { var e = this, i = !!u.u(r) || r, o = u.p(t), c = function(W, D) { var _ = u.w(e.$u ? Date.UTC(e.$y, D, W) : new Date(e.$y, D, W), e); return i ? _ : _.endOf(Y); }, f = function(W, D) { return u.w(e.toDate()[W].apply(e.toDate("s"), (i ? [0, 0, 0, 0] : [23, 59, 59, 999]).slice(D)), e); }, l = this.$W, $ = this.$M, p = this.$D, x = "set" + (this.$u ? "UTC" : ""); switch (o) { case k: return i ? c(1, 0) : c(31, 11); case g: return i ? c(1, $) : c(0, $ + 1); case N: var T = this.$locale().weekStart || 0, C = (l < T ? l + 7 : l) - T; return c(i ? p - C : p + (6 - C), $); case Y: case j: return f(x + "Hours", 0); case S: return f(x + "Minutes", 1); case b: return f(x + "Seconds", 2); case y: return f(x + "Milliseconds", 3); default: return this.clone(); } }, n.endOf = function(t) { return this.startOf(t, !1); }, n.$set = function(t, r) { var e, i = u.p(t), o = "set" + (this.$u ? "UTC" : ""), c = (e = {}, e[Y] = o + "Date", e[j] = o + "Date", e[g] = o + "Month", e[k] = o + "FullYear", e[S] = o + "Hours", e[b] = o + "Minutes", e[y] = o + "Seconds", e[v] = o + "Milliseconds", e)[i], f = i === Y ? this.$D + (r - this.$W) : r; if (i === g || i === k) { var l = this.clone().set(j, 1); l.$d[c](f), l.init(), this.$d = l.set(j, Math.min(this.$D, l.daysInMonth())).$d; } else c && this.$d[c](f); return this.init(), this; }, n.set = function(t, r) { return this.clone().$set(t, r); }, n.get = function(t) { return this[u.p(t)](); }, n.add = function(t, r) { var e, i = this; t = Number(t); var o = u.p(r), c = function($) { var p = d(i); return u.w(p.date(p.date() + Math.round($ * t)), i); }; if (o === g) return this.set(g, this.$M + t); if (o === k) return this.set(k, this.$y + t); if (o === Y) return c(1); if (o === N) return c(7); var f = (e = {}, e[b] = M, e[S] = O, e[y] = m, e)[o] || 1, l = this.$d.getTime() + t * f; return u.w(l, this); }, n.subtract = function(t, r) { return this.add(-1 * t, r); }, n.format = function(t) { var r = this, e = this.$locale(); if (!this.isValid()) return e.invalidDate || X; var i = t || "YYYY-MM-DDTHH:mm:ssZ", o = u.z(this), c = this.$H, f = this.$m, l = this.$M, $ = e.weekdays, p = e.months, x = e.meridiem, T = function(D, _, I, Z) { return D && (D[_] || D(r, i)) || I[_].slice(0, Z); }, C = function(D) { return u.s(c % 12 || 12, D, "0"); }, W = x || function(D, _, I) { var Z = D < 12 ? "AM" : "PM"; return I ? Z.toLowerCase() : Z; }; return i.replace(at, function(D, _) { return _ || function(I) { switch (I) { case "YY": return String(r.$y).slice(-2); case "YYYY": return u.s(r.$y, 4, "0"); case "M": return l + 1; case "MM": return u.s(l + 1, 2, "0"); case "MMM": return T(e.monthsShort, l, p, 3); case "MMMM": return T(p, l); case "D": return r.$D; case "DD": return u.s(r.$D, 2, "0"); case "d": return String(r.$W); case "dd": return T(e.weekdaysMin, r.$W, $, 2); case "ddd": return T(e.weekdaysShort, r.$W, $, 3); case "dddd": return $[r.$W]; case "H": return String(c); case "HH": return u.s(c, 2, "0"); case "h": return C(1); case "hh": return C(2); case "a": return W(c, f, !0); case "A": return W(c, f, !1); case "m": return String(f); case "mm": return u.s(f, 2, "0"); case "s": return String(r.$s); case "ss": return u.s(r.$s, 2, "0"); case "SSS": return u.s(r.$ms, 3, "0"); case "Z": return o; } return null; }(D) || o.replace(":", ""); }); }, n.utcOffset = function() { return 15 * -Math.round(this.$d.getTimezoneOffset() / 15); }, n.diff = function(t, r, e) { var i, o = this, c = u.p(r), f = d(t), l = (f.utcOffset() - this.utcOffset()) * M, $ = this - f, p = function() { return u.m(o, f); }; switch (c) { case k: i = p() / 12; break; case g: i = p(); break; case G: i = p() / 3; break; case N: i = ($ - l) / 6048e5; break; case Y: i = ($ - l) / 864e5; break; case S: i = $ / O; break; case b: i = $ / M; break; case y: i = $ / m; break; default: i = $; } return e ? i : u.a(i); }, n.daysInMonth = function() { return this.endOf(g).$D; }, n.$locale = function() { return H[this.$L]; }, n.locale = function(t, r) { if (!t) return this.$L; var e = this.clone(), i = V(t, r, !0); return i && (e.$L = i), e; }, n.clone = function() { return u.w(this.$d, this); }, n.toDate = function() { return new Date(this.valueOf()); }, n.toJSON = function() { return this.isValid() ? this.toISOString() : null; }, n.toISOString = function() { return this.$d.toISOString(); }, n.toString = function() { return this.$d.toUTCString(); }, a; }(), K = J.prototype; return d.prototype = K, [["$ms", v], ["$s", y], ["$m", b], ["$H", S], ["$W", Y], ["$M", g], ["$y", k], ["$D", j]].forEach(function(a) { K[a[1]] = function(n) { return this.$g(n, a[0], a[1]); }; }), d.extend = function(a, n) { return a.$i || (a(n, J, d), a.$i = !0), d; }, d.locale = V, d.isDayjs = P, d.unix = function(a) { return d(1e3 * a); }, d.en = H[L], d.Ls = H, d.p = {}, d; }); }(q)), q.exports; } var dt = ht(); const A = /* @__PURE__ */ ct(dt); function pt(s) { if (!s || !s.trim()) return ""; const h = A(s); if (!h.isValid()) return ""; const m = B(s), M = h.format("HH:mm"); return `${m} ${M}`; } function B(s) { if (!s || !s.trim()) return ""; const h = A(s); return h.isValid() ? A().isSame(h, "year") ? h.format("MM/DD") : h.format("YYYY/MM/DD") : ""; } function vt(s, h) { const m = [s, h].filter((y) => y !== "" && y && y !== "0000-00-00"); if (m.length === 1 || m[0] === m[1]) return B(m[0]); const M = A(s), O = A(h), v = { shortDate: "MM/DD", fullDate: "YYYY/MM/DD" }; return M.isSame(h, "year") ? [B(s), O.format(v.shortDate)].join(" - ") : [M.format(v.fullDate), O.format(v.fullDate)].join(" - "); } const lt = (s, h = !1) => { const m = h ? Math.floor(s / 60 / 60 / 24) : 0, M = Math.floor(h ? s / 60 / 60 % 24 : s / 3600), O = Math.floor(s % 3600 / 60), v = s % 60; return { days: m, hours: M, minutes: O, seconds: v }; }, Yt = (s, h = !1) => { const { hours: m, minutes: M, seconds: O } = lt(s, !1), v = [m, M, O]; let y = 0; return v.reduce((b, S, Y) => y >= Y && h && S === 0 ? (y = Y, b) : [...b, ut(S, 2)], []).join(":"); }, gt = (s, h = !1) => { const m = s / 1e3; return h ? Math.floor(m) : m; }, mt = (s) => s * 60 * 1e3, $t = (s) => mt(s * 60), wt = (s) => $t(s * 24), w = A(), U = w.day(0), Q = U.subtract(7, "day"), R = U.subtract(14, "day"), F = w.set("date", 1), et = F.subtract(1, "month"), nt = F.subtract(2, "month"), rt = w.set("month", 0).set("date", 1), Mt = w.subtract(6, "month"), Ot = { today: w, thisWeek: U, lastWeek: Q, twoWeekAgo: R, thisMonth: F, halfYearAgo: Mt }, bt = { date: "YYYY/MM/DD", dateDash: "YYYY-MM-DD", dateTime: "YYYY/MM/DD HH:mm:ss", shortDateTime: "YYYY/MM/DD HH:mm", shortTime: "HH:mm", time: "HH:mm:ss" }; var Dt = /* @__PURE__ */ ((s) => (s.today = "today", s.yesterday = "yesterday", s.thisWeek = "thisWeek", s.lastWeek = "lastWeek", s.twoWeekAgo = "twoWeekAgo", s.thisMonth = "thisMonth", s.lastMonth = "lastMonth", s.twoMonth = "twoMonth", s.thisHalfYear = "thisHalfYear", s.thisYear = "thisYear", s))(Dt || {}); const St = { today: { startDate: w, endDate: w }, yesterday: { startDate: w.subtract(1, "day"), endDate: w.subtract(1, "day") }, thisWeek: { startDate: U, endDate: U.add(6, "day") }, lastWeek: { startDate: Q, endDate: Q.add(6, "day") }, twoWeekAgo: { startDate: R, endDate: R.add(6, "day") }, thisMonth: { startDate: F, endDate: F.endOf("month") }, lastMonth: { startDate: et, endDate: et.endOf("month") }, twoMonth: { startDate: nt, endDate: nt.endOf("month") }, thisHalfYear: { startDate: w.subtract(6, "month"), endDate: w }, thisYear: { startDate: rt, endDate: rt.endOf("year") } }; export { Dt as EDayKey, Ot as dateUtils, St as dayList, wt as dayToMillisecond, bt as format, Yt as formatSecondToString, lt as formatTotalSeconds, $t as hourToMillisecond, gt as millisecondToSeconds, mt as minuteToMillisecond, vt as rangeSimpleDate, B as simpleDate, pt as simpleDateTime };