common-utils-y
Version:
my common utils lib
316 lines (315 loc) • 8.69 kB
JavaScript
function O(e) {
return typeof e == "boolean";
}
function j(e) {
return typeof e == "number";
}
function F(e) {
return typeof e == "string";
}
function B(e) {
return typeof e == "function";
}
function k(e) {
return typeof e == "bigint";
}
function y(e) {
return Array.isArray(e);
}
function I(e) {
return e == null;
}
function E() {
return typeof window == "object" && typeof document == "object" && typeof document.body == "object";
}
const S = E();
S || console.warn("this is not browser env!!!");
let p = "requestFullscreen", g = "exitFullscreen";
const s = S ? document : { body: {} };
s.body.requestFullscreen ? p = "requestFullscreen" : s.body.webkitRequestFullscreen ? p = "webkitRequestFullscreen" : s.body.mozRequestFullScreen ? p = "mozRequestFullScreen" : s.body.msRequestFullscreen && (p = "msRequestFullscreen");
s.exitFullscreen ? g = "exitFullscreen" : s.webkitExitFullscreen ? g = "webkitExitFullscreen" : s.mozCancelFullScreen ? g = "mozCancelFullScreen" : s.msExitFullscreen && (g = "msExitFullscreen");
function W(e, n) {
var t;
return (t = e == null ? void 0 : e[p]) == null ? void 0 : t.call(e, n);
}
function _() {
var e;
return (e = s[g]) == null ? void 0 : e.call(s);
}
function U() {
return s.fullscreenElement || s.webkitFullscreenElement || s.mozFullScreenElement || s.msFullscreenElement;
}
const a = /* @__PURE__ */ new WeakMap(), m = new ResizeObserver((e) => {
e.forEach((n) => {
const { target: t, contentBoxSize: r, contentRect: o, borderBoxSize: i, devicePixelContentBoxSize: u } = n, l = a.get(t);
l == null || l.forEach((d) => {
d == null || d({ contentBoxSize: r, contentRect: o, borderBoxSize: i, devicePixelContentBoxSize: u });
});
});
});
function L(e, n) {
a.has(e) || a.set(e, /* @__PURE__ */ new Set()), a.get(e).add(n), m.observe(e);
}
function P(e, n) {
const t = a.get(e);
if (t) {
if (n) {
t.delete(n), t.size || (a.delete(e), m.unobserve(e));
return;
}
a.delete(e), m.unobserve(e);
}
}
function V(e) {
if (!y(e))
throw new Error("method uniqueArray need param array");
return Array.from(new Set(e));
}
function Q(e, n) {
if (!y(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 = y(e) ? e : [e];
function o(i, u) {
var l;
if (u.push(i), n(i))
return u;
if (((l = i[t]) == null ? void 0 : l.length) > 0)
for (const d of i[t]) {
const w = o(d, [...u]);
if (w)
return w;
}
return u.pop(), null;
}
for (const i of r) {
const u = o(i, []);
if (u)
return u;
}
return [];
}
const M = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
}, x = Object.keys(M);
function J(e, n = x) {
return y(n) ? n.reduce((t, r) => t.replace(new RegExp(r, "g"), M[r]), e) : e;
}
const b = {
"&": "\\26 ",
"<": "\\3C ",
">": "\\3E ",
'"': "\\22 ",
"'": "\\27 "
}, R = Object.keys(b);
function K(e, n = R) {
return y(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(c(e), c(n)));
return (e * t + n * t) / t;
}
function z(e, n) {
const t = Math.pow(10, Math.max(c(e), c(n)));
return (e * t - n * t) / t;
}
function N(e, n) {
const t = Math.pow(10, c(e)), r = Math.pow(10, c(n));
return e * t * (n * r) / (t * r);
}
function v(e, n) {
const t = Math.pow(10, c(e)), r = Math.pow(10, c(n));
return e * t / (n * r);
}
function c(e) {
const n = String(e), t = n.indexOf(".");
return t === -1 ? 0 : n.length - t - 1;
}
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(...u) {
const l = Date.now();
!t && o === 0 && (o = l), i && (clearTimeout(i), i = null), l - o >= n ? (o = l, e.apply(this, u)) : r && (i = setTimeout(() => {
o = t ? Date.now() : 0, e.apply(this, u);
}, n - (l - o)));
};
}
function H(e) {
return e instanceof Date;
}
function D(e) {
return H(e) && !Number.isNaN(e.getTime());
}
const h = {
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 T(e, n) {
var t;
return (t = h[e]) == null ? void 0 : t.call(h, n);
}
function ue(e, n = "yyyy-MM-dd") {
if (!D(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) => C(t) ? t.replace("{", "").replace("}", "") : String(T(t, e)));
}
function C(e) {
return e.startsWith("{") && e.endsWith("}");
}
export {
S as IS_BROWSER,
$ as add,
re as addCommas,
L as addResizeObserve,
q as capitalizeFirstLetter,
ie as debounce,
oe as deepCopy,
te as divide,
W as enterFullscreen,
K as escapeCSS,
J as escapeHTML,
G as escapeJS,
_ as exitFullscreen,
Y as findPathInTree,
ue as formatDate,
U as getFullscreenElement,
ne as getRandomInt,
Q as getUniqueArray,
y as isArray,
k as isBigInt,
O as isBoolean,
E as isBrowser,
H as isDate,
I as isEmpty,
B as isFunction,
j as isNumber,
F as isString,
D as isValidDate,
Z as multiply,
P as removeResizeObserve,
ee as subtract,
se as throttle,
X as toCamelStyle,
V as uniqueArray
};