UNPKG

@vavt/util

Version:
183 lines (182 loc) 6.1 kB
const M = (e) => { const n = typeof e; return n !== "function" && n !== "object" || e === null; }, k = (e) => { const n = e.flags === "" ? void 0 : e.flags; return new RegExp(e.source, n); }, u = (e, n = /* @__PURE__ */ new WeakMap()) => { if (e === null || M(e)) return e; if (n.has(e)) return n.get(e); if (e instanceof RegExp) return k(e); if (e instanceof Date) return new Date(e.getTime()); if (e instanceof Function) return e; if (e instanceof Map) { const r = /* @__PURE__ */ new Map(); return n.set(e, r), e.forEach((o, c) => { r.set(c, u(o, n)); }), r; } if (e instanceof Set) { const r = /* @__PURE__ */ new Set(); n.set(e, r); for (const o of e) r.add(u(o, n)); return r; } if (Array.isArray(e)) { const r = []; return n.set(e, r), e.forEach((o) => { r.push(u(o, n)); }), r; } const t = {}; n.set(e, t); for (const r in e) Object.prototype.hasOwnProperty.call(e, r) && (t[r] = u(e[r], n)); return t; }, x = (e, n = 200) => { let t = 0; return (...r) => new Promise((o) => { t && (clearTimeout(t), o("cancel")), t = window.setTimeout(() => { e.apply(void 0, r), t = 0, o("done"); }, n); }); }, L = (e, n = { _blank: !0, nofollow: !0 }) => { const t = document.createElement("a"); t.href = e, n._blank && (t.target = "_blank"), n.nofollow && (t.rel = "noopener noreferrer"), t.click(); }, F = (e, n = "") => { const t = document.createElement("a"); t.setAttribute("href", e), t.setAttribute("download", n), t.click(); }, R = () => { let e = -1; return (t, r, o, c = 100) => { const s = () => { o && (typeof c == "number" ? setTimeout(o, c) : o()); }; e !== -1 && (cancelAnimationFrame(e), s()); let i = t.scrollTop; const l = () => { e = -1; const a = r - i; i = i + a / 5, Math.abs(a) < 1 ? (t.scrollTo(0, r), s()) : (t.scrollTo(0, i), e = requestAnimationFrame(l)); }; e = requestAnimationFrame(l); }; }, O = R(), $ = (e, n = 200) => { let t = 0, r = null; const o = (c) => { t === 0 && (t = c), c - t >= n ? (e.apply(void 0, r), r = null, t = 0) : window.requestAnimationFrame(o); }; return (...c) => { r === null && window.requestAnimationFrame(o), r = c; }; }, U = (e, n = !0) => { if (!e) return {}; const t = {}; return e.replace(/^\?/, "").split("&").forEach((r) => { const [o, c] = r.split("="); if (o) { const s = n ? decodeURIComponent(o) : o, i = n ? decodeURIComponent(c) : c; t[s] !== void 0 ? (Array.isArray(t[s]) || (t[s] = [t[s]]), t[s].push(i)) : t[s] = i; } }), t; }, H = (e, n = !0) => e ? Object.keys(e).map((t) => { const r = n ? encodeURIComponent(t) : t; if (Array.isArray(e[t])) return e[t].map((o) => { const c = n ? encodeURIComponent(o) : o; return `${r}=${c}`; }).join("&"); { const o = n ? encodeURIComponent(e[t]) : e[t]; return `${r}=${o}`; } }).join("&") : "", N = (e, n, t = "asc") => e.sort((r, o) => { const c = n(r), s = n(o); return c === void 0 && s !== void 0 || c === null && s !== void 0 ? t === "asc" ? -1 : 1 : c == null && s === void 0 || s == null ? t === "asc" ? 1 : -1 : S(c) && S(s) ? t == "asc" ? c - s : s - c : t == "asc" ? String(c).localeCompare(String(s), void 0, { numeric: !0 }) : String(s).localeCompare(String(c), void 0, { numeric: !0 }); }), P = (e) => { const n = (t) => { const { scrollHeight: r, scrollWidth: o, offsetHeight: c, offsetWidth: s, scrollLeft: i, scrollTop: l } = e, a = t.x, h = t.y, A = (w) => { const d = l + h - w.y, m = i + a - w.x, E = r - c, T = o - s, p = {}; m >= 0 && m <= T && (p.left = m), d >= 0 && d <= E && (p.top = d), e.scroll(p); }; document.addEventListener("mousemove", A); const g = () => { document.removeEventListener("mousemove", A), document.removeEventListener("mouseup", g); }; document.addEventListener("mouseup", g); }; return e.addEventListener("mousedown", n), () => { e.removeEventListener("mousedown", n); }; }, W = () => `${Date.now().toString(36)}${Math.random().toString(36).substring(2)}`, S = (e) => { if (e == null || ["boolean", "symbol"].includes(typeof e) || /^\s*$/.test(e.toString())) return !1; const n = Number(e); return !isNaN(n) && isFinite(n); }, f = (e) => e !== null && typeof e == "object" && !Array.isArray(e), _ = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]), C = (e) => !_.has(e), v = (e, n, t = {}) => { if (Array.isArray(e) && Array.isArray(n)) return y(e, n, t); const { excludeKeys: r } = t, o = e, c = n; for (const s of Object.keys(c)) { if (!C(s)) continue; const i = c[s], l = o[s]; r && r(s) ? o[s] = i : Array.isArray(i) && Array.isArray(l) ? o[s] = y(l, i, t) : f(i) && f(l) ? o[s] = v( l, i, t ) : o[s] = i; } return e; }, y = (e, n, t) => { const r = e.slice(); return n.forEach((o, c) => { const s = r[c]; Array.isArray(o) && Array.isArray(s) ? r[c] = y(s, o, t) : f(o) && f(s) ? r[c] = v( s, o, t ) : r[c] = o; }), r; }, K = (e, n = document.body) => { let t = e.offsetParent, r = e.offsetTop, o = e.offsetLeft; for (; t !== null && t !== n; ) r += t.offsetTop, o += t.offsetLeft, t = t.offsetParent; return { offsetTop: r, offsetLeft: o }; }, V = (e = {}) => { const { forceScreenWidth: n = !1, threshold: t = 768, fallbackToScreenWidth: r = !0 } = e, o = navigator.userAgent, c = /android|ipad|iphone|ipod|windows phone|blackberry/i.test(o), s = "ontouchstart" in window || navigator.maxTouchPoints > 0, i = window.matchMedia(`(max-width: ${t}px)`).matches; return n ? i : c && s ? !0 : r ? i : !1; }; export { R as createSmoothScroll, x as debounce, u as deepClone, v as deepMerge, F as download, P as draggingScroll, K as getRootOffset, V as isMobile, S as isNumber, M as isPrimitive, L as linkTo, H as objToSearch, N as objectSort, W as randomId, U as searchToObj, O as smoothScroll, $ as throttle };