common-utils-y
Version:
my common utils lib
274 lines (273 loc) • 7.09 kB
JavaScript
function T(t) {
return typeof t == "boolean";
}
function C(t) {
return typeof t == "number";
}
function y(t) {
return typeof t == "string";
}
function L(t) {
return typeof t == "function";
}
function q(t) {
return typeof t == "bigint";
}
function c(t) {
return Array.isArray(t);
}
function P(t) {
return t == null;
}
function j(t) {
if (!c(t))
throw new Error("method uniqueArray need param array");
return Array.from(new Set(t));
}
function F(t, n) {
if (!c(t))
throw new Error("method getUniqueArray need first param array");
if (!y(n))
throw new Error("method getUniqueArray need second param string");
let e = /* @__PURE__ */ new Set();
const r = t.filter((o) => {
const a = o[n];
return e.has(a) ? !1 : (e.add(a), !0);
});
return e = null, r;
}
function R(t, n, e = "children") {
const r = c(t) ? t : [t];
function o(a, i) {
var s;
if (i.push(a), n(a))
return i;
if (((s = a[e]) == null ? void 0 : s.length) > 0)
for (const h of a[e]) {
const p = o(h, [...i]);
if (p)
return p;
}
return i.pop(), null;
}
for (const a of r) {
const i = o(a, []);
if (i)
return i;
}
return [];
}
const g = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
}, M = Object.keys(g);
function k(t, n = M) {
return c(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 I(t, n = m) {
return c(n) ? n.reduce((e, r) => e.replace(new RegExp(r, "g"), d[r]), t) : t;
}
function U(t) {
return t.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/"/g, '\\"').replace(/`/g, "\\`").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
}
function S(t) {
return t.charAt(0).toUpperCase() + t.slice(1);
}
function _(t, n = "-") {
return t.split(n).map((r, o) => o === 0 ? r : S(r)).join("");
}
function w(t, n) {
const e = Math.pow(
10,
Math.max(u(t), u(n))
);
return (Math.round(t * e) + Math.round(n * e)) / e;
}
function A(t, n) {
const e = Math.pow(
10,
Math.max(u(t), u(n))
);
return (Math.round(t * e) - Math.round(n * e)) / e;
}
function b(t, n) {
const e = Math.pow(10, u(t)), r = Math.pow(10, u(n));
return Math.round(t * e) * Math.round(n * r) / (e * r);
}
function x(t, n) {
const e = u(t), r = u(n), o = Math.max(e, r), a = Math.pow(10, o);
return Math.round(t * a) / Math.round(n * a);
}
function u(t) {
const e = t.toString().toLowerCase().split("e"), r = e[0], o = e.length > 1 ? parseInt(e[1], 10) : 0, i = r.split(".")[1] || "";
return o < 0 ? Math.abs(o) + i.length : o > 0 ? Math.max(0, i.length - o) : i.length;
}
function B(...t) {
return t.length ? t.reduce((n, e) => b(n, e)) : NaN;
}
function O(...t) {
return t.length ? t.reduce((n, e) => w(n, e)) : NaN;
}
function V(...t) {
return t.length ? t.reduce((n, e) => A(n, e)) : NaN;
}
function W(...t) {
return t.length ? t.reduce((n, e) => x(n, e)) : NaN;
}
function v(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 l(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((o) => {
r.add(l(o, n));
}), r;
}
if (t instanceof Map) {
const r = /* @__PURE__ */ new Map();
return n.set(t, r), t.forEach((o, a) => {
r.set(l(a, n), l(o, n));
}), r;
}
if (Array.isArray(t)) {
const r = [];
return n.set(t, r), t.forEach((o) => {
r.push(l(o, n));
}), r;
}
const e = {};
n.set(t, e);
for (const r in t)
t.hasOwnProperty(r) && (e[r] = l(t[r], n));
return e;
}
function Y(t) {
return l(t);
}
function z(t, n = 10) {
let e = null;
return function(...r) {
const o = this;
return e && clearTimeout(e), e = setTimeout(() => {
t.apply(o, r);
}, n), () => {
clearTimeout(e);
};
};
}
function J(t, n, e = !0, r = !1) {
let o = 0, a = null;
return function(...i) {
const s = Date.now();
!e && o === 0 && (o = s), a && (clearTimeout(a), a = null), s - o >= n ? (o = s, t.apply(this, i)) : r && (a = setTimeout(() => {
o = e ? Date.now() : 0, t.apply(this, i);
}, n - (s - o)));
};
}
function E(t) {
return t instanceof Date;
}
function D(t) {
return E(t) && !Number.isNaN(t.getTime());
}
const f = {
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 H(t, n) {
var e;
return (e = f[t]) == null ? void 0 : e.call(f, n);
}
function K(t, n = "yyyy-MM-dd") {
if (!D(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) => N(e) ? e.replace("{", "").replace("}", "") : String(H(e, t)));
}
function N(t) {
return t.startsWith("{") && t.endsWith("}");
}
export {
O as add,
Q as addCommas,
S as capitalizeFirstLetter,
z as debounce,
Y as deepCopy,
W as divide,
I as escapeCSS,
k as escapeHTML,
U as escapeJS,
R as findPathInTree,
K as formatDate,
v as getRandomInt,
F as getUniqueArray,
c as isArray,
q as isBigInt,
T as isBoolean,
E as isDate,
P as isEmpty,
L as isFunction,
C as isNumber,
y as isString,
D as isValidDate,
B as multiply,
V as subtract,
J as throttle,
_ as toCamelStyle,
j as uniqueArray
};