UNPKG

common-utils-y

Version:

my common utils lib

268 lines (267 loc) 6.83 kB
function T(t) { return typeof t == "boolean"; } function C(t) { return typeof t == "number"; } function h(t) { return typeof t == "string"; } function q(t) { return typeof t == "function"; } function j(t) { return typeof t == "bigint"; } function l(t) { return Array.isArray(t); } function F(t) { return t == null; } function R(t) { if (!l(t)) throw new Error("method uniqueArray need param array"); return Array.from(new Set(t)); } function k(t, n) { if (!l(t)) throw new Error("method getUniqueArray need first param array"); if (!h(n)) throw new Error("method getUniqueArray need second param string"); let e = /* @__PURE__ */ new Set(); const r = t.filter((i) => { const o = i[n]; return e.has(o) ? !1 : (e.add(o), !0); }); return e = null, r; } function I(t, n, e = "children") { const r = l(t) ? t : [t]; function i(o, a) { var s; if (a.push(o), n(o)) return a; if (((s = o[e]) == null ? void 0 : s.length) > 0) for (const y of o[e]) { const p = i(y, [...a]); if (p) return p; } return a.pop(), null; } for (const o of r) { const a = i(o, []); if (a) return a; } return []; } const g = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }, M = Object.keys(g); function O(t, n = M) { return l(n) ? n.reduce((e, r) => e.replace(new RegExp(r, "g"), g[r]), t) : t; } const d = { "&": "\\26 ", "<": "\\3C ", ">": "\\3E ", '"': "\\22 ", "'": "\\27 " }, m = Object.keys(d); function U(t, n = m) { return l(n) ? n.reduce((e, r) => e.replace(new RegExp(r, "g"), d[r]), t) : t; } function _(t) { return t.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/"/g, '\\"').replace(/`/g, "\\`").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"); } function w(t) { return t.charAt(0).toUpperCase() + t.slice(1); } function B(t, n = "-") { return t.split(n).map((r, i) => i === 0 ? r : w(r)).join(""); } function S(t, n) { const e = Math.pow(10, Math.max(u(t), u(n))); return (t * e + n * e) / e; } function A(t, n) { const e = Math.pow(10, Math.max(u(t), u(n))); return (t * e - n * e) / e; } function E(t, n) { const e = Math.pow(10, u(t)), r = Math.pow(10, u(n)); return t * e * (n * r) / (e * r); } function x(t, n) { const e = Math.pow(10, u(t)), r = Math.pow(10, u(n)); return t * e / (n * r); } function u(t) { const n = String(t), e = n.indexOf("."); return e === -1 ? 0 : n.length - e - 1; } function L(...t) { return t.length ? t.reduce((n, e) => E(n, e)) : NaN; } function V(...t) { return t.length ? t.reduce((n, e) => S(n, e)) : NaN; } function W(...t) { return t.length ? t.reduce((n, e) => A(n, e)) : NaN; } function v(...t) { return t.length ? t.reduce((n, e) => x(n, e)) : NaN; } function P(t, n) { const e = Math.ceil(Math.min(t, n)), r = Math.floor(Math.max(t, n)); return Math.floor(Math.random() * (r - e + 1)) + e; } function Q(t) { const e = t.toString().split("."); return e[0] = e[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","), e.join("."); } function f(t, n = /* @__PURE__ */ new WeakMap()) { if (t === null || typeof t != "object") return t; if (n.has(t)) return n.get(t); if (t instanceof Date) return new Date(t.getTime()); if (t instanceof RegExp) return new RegExp(t.source, t.flags); if (t instanceof Set) { const r = /* @__PURE__ */ new Set(); return n.set(t, r), t.forEach((i) => { r.add(f(i, n)); }), r; } if (t instanceof Map) { const r = /* @__PURE__ */ new Map(); return n.set(t, r), t.forEach((i, o) => { r.set(f(o, n), f(i, n)); }), r; } if (Array.isArray(t)) { const r = []; return n.set(t, r), t.forEach((i) => { r.push(f(i, n)); }), r; } const e = {}; n.set(t, e); for (const r in t) t.hasOwnProperty(r) && (e[r] = f(t[r], n)); return e; } function Y(t) { return f(t); } function z(t, n = 10) { let e = null; return function(...r) { const i = this; return e && clearTimeout(e), e = setTimeout(() => { t.apply(i, r); }, n), () => { clearTimeout(e); }; }; } function J(t, n, e = !0, r = !1) { let i = 0, o = null; return function(...a) { const s = Date.now(); !e && i === 0 && (i = s), o && (clearTimeout(o), o = null), s - i >= n ? (i = s, t.apply(this, a)) : r && (o = setTimeout(() => { i = e ? Date.now() : 0, t.apply(this, a); }, n - (s - i))); }; } function D(t) { return t instanceof Date; } function H(t) { return D(t) && !Number.isNaN(t.getTime()); } const c = { yyyy: (t) => t.getFullYear(), // 年份(4位) yy: (t) => String(t.getFullYear()).slice(-2), // 年份(2位) MM: (t) => String(t.getMonth() + 1).padStart(2, "0"), // 月份(补零) M: (t) => t.getMonth() + 1, // 月份(不补零) dd: (t) => String(t.getDate()).padStart(2, "0"), // 日期(补零) d: (t) => t.getDate(), // 日期(不补零) HH: (t) => String(t.getHours()).padStart(2, "0"), // 小时(24小时制,补零) H: (t) => t.getHours(), // 小时(24小时制,不补零) hh: (t) => String(t.getHours() % 12 || 12).padStart(2, "0"), // 小时(12小时制,补零) h: (t) => t.getHours() % 12 || 12, // 小时(12小时制,不补零) mm: (t) => String(t.getMinutes()).padStart(2, "0"), // 分钟(补零) m: (t) => t.getMinutes(), // 分钟(不补零) ss: (t) => String(t.getSeconds()).padStart(2, "0"), // 秒(补零) s: (t) => t.getSeconds(), // 秒(不补零) a: (t) => t.getHours() < 12 ? "上午" : "下午", // 上午/下午 Q: (t) => Math.floor((t.getMonth() + 3) / 3), // 季度(1-4) w: (t) => ["日", "一", "二", "三", "四", "五", "六"][t.getDay()] // 星期 }; function N(t, n) { var e; return (e = c[t]) == null ? void 0 : e.call(c, n); } function K(t, n = "yyyy-MM-dd") { if (!H(t)) 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, (e) => b(e) ? e.replace("{", "").replace("}", "") : String(N(e, t))); } function b(t) { return t.startsWith("{") && t.endsWith("}"); } export { V as add, Q as addCommas, w as capitalizeFirstLetter, z as debounce, Y as deepCopy, v as divide, U as escapeCSS, O as escapeHTML, _ as escapeJS, I as findPathInTree, K as formatDate, P as getRandomInt, k as getUniqueArray, l as isArray, j as isBigInt, T as isBoolean, D as isDate, F as isEmpty, q as isFunction, C as isNumber, h as isString, H as isValidDate, L as multiply, W as subtract, J as throttle, B as toCamelStyle, R as uniqueArray };