UNPKG

common-utils-y

Version:

my common utils lib

322 lines (321 loc) 8.96 kB
function j(e) { return typeof e == "boolean"; } function B(e) { return typeof e == "number"; } function F(e) { return typeof e == "string"; } function O(e) { return typeof e == "function"; } function L(e) { return typeof e == "bigint"; } function h(e) { return Array.isArray(e); } function P(e) { return e == null; } function x() { return typeof window == "object" && typeof document == "object" && typeof document.body == "object"; } const w = x(); w || console.warn("this is not browser env!!!"); let p = "requestFullscreen", g = "exitFullscreen"; const u = w ? document : { body: {} }; u.body.requestFullscreen ? p = "requestFullscreen" : u.body.webkitRequestFullscreen ? p = "webkitRequestFullscreen" : u.body.mozRequestFullScreen ? p = "mozRequestFullScreen" : u.body.msRequestFullscreen && (p = "msRequestFullscreen"); u.exitFullscreen ? g = "exitFullscreen" : u.webkitExitFullscreen ? g = "webkitExitFullscreen" : u.mozCancelFullScreen ? g = "mozCancelFullScreen" : u.msExitFullscreen && (g = "msExitFullscreen"); function k(e, n) { var t; return (t = e == null ? void 0 : e[p]) == null ? void 0 : t.call(e, n); } function I() { var e; return (e = u[g]) == null ? void 0 : e.call(u); } function W() { return u.fullscreenElement || u.webkitFullscreenElement || u.mozFullScreenElement || u.msFullscreenElement; } const c = /* @__PURE__ */ new WeakMap(), m = new ResizeObserver((e) => { e.forEach((n) => { const { target: t, contentBoxSize: r, contentRect: o, borderBoxSize: i, devicePixelContentBoxSize: s } = n, a = c.get(t); a == null || a.forEach((d) => { d == null || d({ contentBoxSize: r, contentRect: o, borderBoxSize: i, devicePixelContentBoxSize: s }); }); }); }); function _(e, n) { c.has(e) || c.set(e, /* @__PURE__ */ new Set()), c.get(e).add(n), m.observe(e); } function U(e, n) { const t = c.get(e); if (t) { if (n) { t.delete(n), t.size || (c.delete(e), m.unobserve(e)); return; } c.delete(e), m.unobserve(e); } } function V(e) { if (!h(e)) throw new Error("method uniqueArray need param array"); return Array.from(new Set(e)); } function Q(e, n) { if (!h(e)) throw new Error("method getUniqueArray need first param array"); if (!F(n)) throw new Error("method getUniqueArray need second param string"); let t = /* @__PURE__ */ new Set(); const r = e.filter((o) => { const i = o[n]; return t.has(i) ? !1 : (t.add(i), !0); }); return t = null, r; } function Y(e, n, t = "children") { const r = h(e) ? e : [e]; function o(i, s) { var a; if (s.push(i), n(i)) return s; if (((a = i[t]) == null ? void 0 : a.length) > 0) for (const d of i[t]) { const M = o(d, [...s]); if (M) return M; } return s.pop(), null; } for (const i of r) { const s = o(i, []); if (s) return s; } return []; } const S = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }, E = Object.keys(S); function J(e, n = E) { return h(n) ? n.reduce((t, r) => t.replace(new RegExp(r, "g"), S[r]), e) : e; } const b = { "&": "\\26 ", "<": "\\3C ", ">": "\\3E ", '"': "\\22 ", "'": "\\27 " }, R = Object.keys(b); function K(e, n = R) { return h(n) ? n.reduce((t, r) => t.replace(new RegExp(r, "g"), b[r]), e) : e; } function G(e) { return e.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/"/g, '\\"').replace(/`/g, "\\`").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"); } function q(e) { return e.charAt(0).toUpperCase() + e.slice(1); } function X(e, n = "-") { return e.split(n).map((r, o) => o === 0 ? r : q(r)).join(""); } function A(e, n) { const t = Math.pow( 10, Math.max(l(e), l(n)) ); return (Math.round(e * t) + Math.round(n * t)) / t; } function z(e, n) { const t = Math.pow( 10, Math.max(l(e), l(n)) ); return (Math.round(e * t) - Math.round(n * t)) / t; } function N(e, n) { const t = Math.pow(10, l(e)), r = Math.pow(10, l(n)); return Math.round(e * t) * Math.round(n * r) / (t * r); } function v(e, n) { const t = l(e), r = l(n), o = Math.max(t, r), i = Math.pow(10, o); return Math.round(e * i) / Math.round(n * i); } function l(e) { const t = e.toString().toLowerCase().split("e"), r = t[0], o = t.length > 1 ? parseInt(t[1], 10) : 0, s = r.split(".")[1] || ""; return o < 0 ? Math.abs(o) + s.length : o > 0 ? Math.max(0, s.length - o) : s.length; } function Z(...e) { return e.length ? e.reduce((n, t) => N(n, t)) : NaN; } function $(...e) { return e.length ? e.reduce((n, t) => A(n, t)) : NaN; } function ee(...e) { return e.length ? e.reduce((n, t) => z(n, t)) : NaN; } function te(...e) { return e.length ? e.reduce((n, t) => v(n, t)) : NaN; } function ne(e, n) { const t = Math.ceil(Math.min(e, n)), r = Math.floor(Math.max(e, n)); return Math.floor(Math.random() * (r - t + 1)) + t; } function re(e) { const t = e.toString().split("."); return t[0] = t[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","), t.join("."); } function f(e, n = /* @__PURE__ */ new WeakMap()) { if (e === null || typeof e != "object") return e; if (n.has(e)) return n.get(e); if (e instanceof Date) return new Date(e.getTime()); if (e instanceof RegExp) return new RegExp(e.source, e.flags); if (e instanceof Set) { const r = /* @__PURE__ */ new Set(); return n.set(e, r), e.forEach((o) => { r.add(f(o, n)); }), r; } if (e instanceof Map) { const r = /* @__PURE__ */ new Map(); return n.set(e, r), e.forEach((o, i) => { r.set(f(i, n), f(o, n)); }), r; } if (Array.isArray(e)) { const r = []; return n.set(e, r), e.forEach((o) => { r.push(f(o, n)); }), r; } const t = {}; n.set(e, t); for (const r in e) e.hasOwnProperty(r) && (t[r] = f(e[r], n)); return t; } function oe(e) { return f(e); } function ie(e, n = 10) { let t = null; return function(...r) { const o = this; return t && clearTimeout(t), t = setTimeout(() => { e.apply(o, r); }, n), () => { clearTimeout(t); }; }; } function se(e, n, t = !0, r = !1) { let o = 0, i = null; return function(...s) { const a = Date.now(); !t && o === 0 && (o = a), i && (clearTimeout(i), i = null), a - o >= n ? (o = a, e.apply(this, s)) : r && (i = setTimeout(() => { o = t ? Date.now() : 0, e.apply(this, s); }, n - (a - o))); }; } function H(e) { return e instanceof Date; } function C(e) { return H(e) && !Number.isNaN(e.getTime()); } const y = { yyyy: (e) => e.getFullYear(), // 年份(4位) yy: (e) => String(e.getFullYear()).slice(-2), // 年份(2位) MM: (e) => String(e.getMonth() + 1).padStart(2, "0"), // 月份(补零) M: (e) => e.getMonth() + 1, // 月份(不补零) dd: (e) => String(e.getDate()).padStart(2, "0"), // 日期(补零) d: (e) => e.getDate(), // 日期(不补零) HH: (e) => String(e.getHours()).padStart(2, "0"), // 小时(24小时制,补零) H: (e) => e.getHours(), // 小时(24小时制,不补零) hh: (e) => String(e.getHours() % 12 || 12).padStart(2, "0"), // 小时(12小时制,补零) h: (e) => e.getHours() % 12 || 12, // 小时(12小时制,不补零) mm: (e) => String(e.getMinutes()).padStart(2, "0"), // 分钟(补零) m: (e) => e.getMinutes(), // 分钟(不补零) ss: (e) => String(e.getSeconds()).padStart(2, "0"), // 秒(补零) s: (e) => e.getSeconds(), // 秒(不补零) a: (e) => e.getHours() < 12 ? "上午" : "下午", // 上午/下午 Q: (e) => Math.floor((e.getMonth() + 3) / 3), // 季度(1-4) w: (e) => ["日", "一", "二", "三", "四", "五", "六"][e.getDay()] // 星期 }; function D(e, n) { var t; return (t = y[e]) == null ? void 0 : t.call(y, n); } function ue(e, n = "yyyy-MM-dd") { if (!C(e)) throw new TypeError("第一个参数必须合法是 Date 对象"); return n.replace(/\{?yyyy\}?|\{?yy\}?|\{?MM\}?|\{?M|\}?\{?dd\}?|\{?d\}?|\{?HH\}?|\{?H\}?|\{?hh\}?|\{?h\}?|\{?mm\}?|\{?m\}?|\{?ss\}?|\{?s\}?|\{?a\}?|\{?Q\}?|\{?w\}?/g, (t) => T(t) ? t.replace("{", "").replace("}", "") : String(D(t, e))); } function T(e) { return e.startsWith("{") && e.endsWith("}"); } export { w as IS_BROWSER, $ as add, re as addCommas, _ as addResizeObserve, q as capitalizeFirstLetter, ie as debounce, oe as deepCopy, te as divide, k as enterFullscreen, K as escapeCSS, J as escapeHTML, G as escapeJS, I as exitFullscreen, Y as findPathInTree, ue as formatDate, W as getFullscreenElement, ne as getRandomInt, Q as getUniqueArray, h as isArray, L as isBigInt, j as isBoolean, x as isBrowser, H as isDate, P as isEmpty, O as isFunction, B as isNumber, F as isString, C as isValidDate, Z as multiply, U as removeResizeObserve, ee as subtract, se as throttle, X as toCamelStyle, V as uniqueArray };