UNPKG

@acrool/js-utils

Version:

Common javascript utils methods for project development

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