UNPKG

cross-env-plugins

Version:

A cross-environment JavaScript utility library with common tools and validation functions for Web and Node.js

465 lines (464 loc) 14.2 kB
function x(t) { const e = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], n = new Date(t).getDay(); return e[n]; } function P(t, e = /* @__PURE__ */ new Date()) { const n = new Date(e).getTime(), r = new Date(t).getTime(); if (isNaN(n) || isNaN(r)) throw new Error("Invalid start or end time format"); return Math.max(r - n, 0); } function D(t, e = 2) { return parseFloat(String(t)).toFixed(e).replace(/\d(?=(\d{3})+\.)/g, "$&,"); } function b(t) { const e = ["角", "分"], n = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"], r = [ ["元", "万", "亿"], ["", "拾", "佰", "仟"] ]; let o = t < 0 ? "负" : ""; t = Math.abs(t); let i = ""; for (let s = 0; s < e.length; s++) i += (n[Math.floor(t * 10 * Math.pow(10, s)) % 10] + e[s]).replace(/零./, ""); i = i || "整", t = Math.floor(t); for (let s = 0; s < r[0].length && t > 0; s++) { let c = ""; for (let a = 0; a < r[1].length && t > 0; a++) c = n[t % 10] + r[1][a] + c, t = Math.floor(t / 10); i = c.replace(/(零.)*零$/, "").replace(/^$/, "零") + r[0][s] + i; } return o + i.replace(/(零.)*零元/, "元").replace(/(零.)+/g, "零").replace(/^整$/, "零元整"); } function A(t = 6) { const e = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; let n = ""; for (let r = 0; r < t; r++) n += e.charAt(Math.floor(Math.random() * e.length)); return n; } function T(t, e) { let n; return (...r) => { n && clearTimeout(n), n = setTimeout(() => { t(...r); }, e); }; } function N(t, e) { let n = 0; return (...r) => { const o = (/* @__PURE__ */ new Date()).getTime(); o - n >= e && (t(...r), n = o); }; } function h(t) { if (typeof t != "object" || t === null) return t; const e = Array.isArray(t) ? [] : {}; for (const n in t) Object.prototype.hasOwnProperty.call(t, n) && (e[n] = h(t[n])); return e; } function C(t, e, n = !0) { for (let r in e) Object.prototype.hasOwnProperty.call(e, r) && (n || !(r in t)) && (t[r] = e[r]); return t; } function S(t) { return [...new Set(t)]; } function I(t) { let e = 0; const n = [ "[A-Z]", "[a-z]", "[0-9]", "[!@#$%^&*()_+\\-={};':\",.<>?`~\\[\\]\\\\/]|\\\\" ]; for (let r = 0; r < n.length; r++) new RegExp(n[r]).test(t) && e++; return e; } const $ = (t, e) => Math.floor(Math.random() * (e - t + 1)) + t, R = (t = /* @__PURE__ */ new Date(), e = "yyyy-MM-dd hh:mm:ss") => { const n = new Date(t), r = { "M+": n.getMonth() + 1, "d+": n.getDate(), "h+": n.getHours(), "m+": n.getMinutes(), "s+": n.getSeconds(), "q+": Math.floor((n.getMonth() + 3) / 3), S: n.getMilliseconds() }, o = e.match(/(y+)/); o && (e = e.replace(o[0], (n.getFullYear() + "").substring(4 - o[0].length))); for (const i in r) { const s = e.match(new RegExp("(" + i + ")")); s && (e = e.replace(s[0], s[0].length === 1 ? String(r[i]) : ("00" + r[i]).substring(("" + r[i]).length))); } return e; }, B = (t) => { if (typeof wx < "u") { const e = getCurrentPages(), n = e[e.length - 1]; return t ? n.options[t] || "" : n.options; } else { if (typeof window > "u") return t ? "" : {}; if (!t) { const r = {}, o = window.location.search.substring(1); return o && o.split("&").forEach((i) => { const [s, c] = i.split("="); s && (r[decodeURIComponent(s)] = decodeURIComponent(c || "")); }), r; } const e = new RegExp("(^|&)" + t + "=([^&]*)(&|$)"), n = window.location.search.substring(1).match(e); return n != null ? decodeURIComponent(n[2]) : ""; } }, E = (t = 16) => { const e = Array(26).fill(0).map((i, s) => String.fromCharCode(97 + s)), n = (/* @__PURE__ */ new Date()).getTime(); let r = String(n).substring(5).split(""); t > 26 && (r = String(n).split("")); const o = r.length; for (let i = o; i < t; i++) r.push(e[Math.floor(Math.random() * e.length)]); return r.sort(() => Math.random() - 0.5).join(""); }; function U() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(t) { const e = Math.random() * 16 | 0; return (t === "x" ? e : e & 3 | 8).toString(16); }); } function O(t) { return t && t.charAt(0).toUpperCase() + t.slice(1).toLowerCase(); } function F(t) { return t.replace(/_([a-z])/g, (e, n) => n.toUpperCase()); } function k(t) { return t.replace(/([A-Z])/g, "_$1").toLowerCase(); } function g(t, e = 1 / 0) { return e > 0 ? t.reduce((n, r) => n.concat(Array.isArray(r) ? g(r, e - 1) : [r]), []) : t.slice(); } function L(t, e) { return t.reduce((n, r) => { const o = typeof e == "function" ? e(r) : r[e]; return n[o] || (n[o] = []), n[o].push(r), n; }, {}); } function z(t, e = 1, n = 10) { const r = (e - 1) * n, o = r + n; return t.slice(r, o); } function Y(t) { const e = /* @__PURE__ */ new Date(), n = new Date(t), r = e.getTime() - n.getTime(), o = Math.floor(r / 1e3), i = Math.floor(o / 60), s = Math.floor(i / 60), c = Math.floor(s / 24), a = Math.floor(c / 30), f = Math.floor(c / 365); return f > 0 ? `${f}年前` : a > 0 ? `${a}个月前` : c > 0 ? `${c}天前` : s > 0 ? `${s}小时前` : i > 0 ? `${i}分钟前` : "刚刚"; } function j(t, e = /* @__PURE__ */ new Date()) { const n = new Date(t), r = new Date(e), o = Math.abs(r.getTime() - n.getTime()); return Math.floor(o / (1e3 * 60 * 60 * 24)); } function W(t, e = /* @__PURE__ */ new Date()) { const n = new Date(t), r = new Date(e); return n.getFullYear() === r.getFullYear() && n.getMonth() === r.getMonth() && n.getDate() === r.getDate(); } function X(t, e = 2) { if (t === 0) return "0 Bytes"; const n = 1024, r = e < 0 ? 0 : e, o = ["Bytes", "KB", "MB", "GB", "TB", "PB"], i = Math.floor(Math.log(t) / Math.log(n)); return parseFloat((t / Math.pow(n, i)).toFixed(r)) + " " + o[i]; } function Z(t) { return String(t).replace(/\B(?=(\d{3})+(?!\d))/g, ","); } function v(t, e = 2) { return (t * 100).toFixed(e) + "%"; } function H(t) { if (typeof btoa < "u") return btoa(unescape(encodeURIComponent(t))); if (typeof Buffer < "u") return Buffer.from(t, "utf8").toString("base64"); throw new Error("Base64 encoding not supported in this environment"); } function _(t) { if (typeof atob < "u") return decodeURIComponent(escape(atob(t))); if (typeof Buffer < "u") return Buffer.from(t, "base64").toString("utf8"); throw new Error("Base64 decoding not supported in this environment"); } function q(t) { return encodeURIComponent(t); } function J(t) { return decodeURIComponent(t); } function K(t, e, n = "...") { return !t || t.length <= e ? t : t.substring(0, e) + n; } function m(t, e) { if (t === e) return !0; if (t == null || e == null || typeof t != "object" || typeof e != "object") return !1; const n = Object.keys(t), r = Object.keys(e); if (n.length !== r.length) return !1; for (let o of n) if (!r.includes(o) || !m(t[o], e[o])) return !1; return !0; } function Q(t) { return Object.prototype.toString.call(t).slice(8, -1); } function u(t) { const e = t.toString(); if (e.indexOf(".") === -1 && e.indexOf("e") === -1) return 0; if (e.indexOf("e") !== -1) { const n = e.split("e"), r = n[0], o = parseInt(n[1], 10), i = r.indexOf(".") === -1 ? 0 : r.split(".")[1].length; return Math.max(0, i - o); } return e.split(".")[1].length; } function G(t, e, ...n) { const r = u(t), o = u(e), i = Math.max(r, o), s = Math.pow(10, i); let c = Math.round(t * s + e * s) / s; for (const a of n) { const f = u(c), p = u(a), d = Math.max(f, p), l = Math.pow(10, d); c = Math.round(c * l + a * l) / l; } return c; } function V(t, e, ...n) { const r = u(t), o = u(e), i = Math.max(r, o), s = Math.pow(10, i); let c = Math.round(t * s - e * s) / s; for (const a of n) { const f = u(c), p = u(a), d = Math.max(f, p), l = Math.pow(10, d); c = Math.round(c * l - a * l) / l; } return c; } function tt(t, e, ...n) { const r = u(t), o = u(e), i = Math.pow(10, r), s = Math.pow(10, o); let c = Math.round(t * i) * Math.round(e * s) / (i * s); for (const a of n) { const f = u(c), p = u(a), d = Math.pow(10, f), l = Math.pow(10, p); c = Math.round(c * d) * Math.round(a * l) / (d * l); } return c; } function et(t, e, ...n) { if (e === 0) throw new Error("Division by zero is not allowed"); const r = u(t), o = u(e), i = Math.pow(10, r), s = Math.pow(10, o); let c = Math.round(t * i) / Math.round(e * s); for (const a of n) { if (a === 0) throw new Error("Division by zero is not allowed"); const f = u(c), p = u(a), d = Math.pow(10, f), l = Math.pow(10, p); c = Math.round(c * d) / Math.round(a * l); } return c; } function nt(t, e) { if (!e || typeof e != "string") return !1; const n = { object: "Object", array: "Array", string: "String", number: "Number", boolean: "Boolean", function: "Function", date: "Date", regexp: "RegExp", regex: "RegExp", null: "Null", undefined: "Undefined", symbol: "Symbol", promise: "Promise", error: "Error", asyncfunction: "AsyncFunction", async: "AsyncFunction" }, r = e.toLowerCase(), o = n[r] || e, i = Object.prototype.toString.call(t).slice(8, -1); return r === "undefined" && t === void 0 ? !0 : r === "promise" ? i === "Promise" || t !== null && typeof t == "object" && typeof t.then == "function" && typeof t.catch == "function" : r === "function" ? i === "Function" || i === "AsyncFunction" : r === "date" ? i === "Date" && !isNaN(t.getTime()) : r === "number" ? i === "Number" && !isNaN(t) : i === o; } const rt = { random: $, formatTime: R, getDayOfWeek: x, getTimeUntilEnd: P, formatCurrency: D, convertToChinese: b, generateRandomCode: A, debounce: T, throttle: N, getUrlParam: B, deepClone: h, mergeObjects: C, uniqueArray: S, checkPwd: I, randomStr: E, generateUUID: U, capitalize: O, camelCase: F, snakeCase: k, flattenArray: g, groupBy: L, paginate: z, getRelativeTime: Y, getDaysDiff: j, isSameDay: W, formatFileSize: X, formatNumber: Z, toPercent: v, base64Encode: H, base64Decode: _, urlEncode: q, urlDecode: J, truncate: K, deepEqual: m, getType: Q, isType: nt, add: G, subtract: V, multiply: tt, divide: et }; function ot(t) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t); } function it(t) { return /^1\d{10}$/.test(t); } function y(t) { if (t.length !== 15) return t; const e = t.substring(0, 6) + "19" + t.substring(6), n = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], r = e.split("").reduce((i, s, c) => i + parseInt(s) * n[c], 0), o = "10X98765432".charAt(r % 11); return e + o; } function st(t) { if (!/^\d{15}$|^\d{17}(\d|X|x)$/.test(t)) return !1; let e = t; t.length === 15 && (e = y(t)); const n = e.substring(6, 14); return w(n) ? M(e) : !1; } function M(t) { const e = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], n = t.slice(0, 17).split("").reduce((o, i, s) => o + parseInt(i) * e[s], 0), r = "10X98765432".charAt(n % 11); return t.charAt(17).toUpperCase() === r; } function w(t) { const e = parseInt(t.substring(0, 4), 10), n = parseInt(t.substring(4, 6), 10), r = parseInt(t.substring(6, 8), 10), o = new Date(e, n - 1, r); return o.getFullYear() === e && o.getMonth() === n - 1 && o.getDate() === r; } function ct(t) { try { return new URL(t), !0; } catch { return !1; } } function at(t) { return /^(\d{1,3}\.){3}\d{1,3}$/.test(t) ? t.split(".").every((n) => { const r = parseInt(n, 10); return r >= 0 && r <= 255; }) : !1; } function ut(t) { if (!/^\d{13,19}$/.test(t)) return !1; let e = 0, n = !1; for (let r = t.length - 1; r >= 0; r--) { let o = parseInt(t[r], 10); n && (o *= 2, o > 9 && (o -= 9)), e += o, n = !n; } return e % 10 === 0; } function lt(t) { return /^[\u4e00-\u9fa5]{2,4}$/.test(t); } function ft(t) { return /^[1-9]\d{5}$/.test(t); } function dt(t) { return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{4,5}[A-Z0-9挂学警港澳]$/.test(t); } function pt(t) { if (!/^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/.test(t)) return !1; const e = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28], n = "0123456789ABCDEFGHJKLMNPQRTUWXY"; let r = 0; for (let i = 0; i < 17; i++) { const s = t[i], c = n.indexOf(s); if (c === -1) return !1; r += c * e[i]; } const o = (31 - r % 31) % 31; return t[17] === n[o]; } function ht(t) { return !isNaN(Number(t)) && !isNaN(parseFloat(String(t))); } function gt(t) { return Number.isInteger(Number(t)); } function mt(t) { return /^-?\d+\.\d+$/.test(String(t)); } function yt(t) { return !!(t == null || typeof t == "string" && t.trim() === "" || Array.isArray(t) && t.length === 0 || typeof t == "object" && Object.keys(t).length === 0); } function Mt(t, e, n) { const r = t.length; return r >= e && r <= n; } function wt(t) { return /^[1-9]\d*$/.test(String(t)); } function xt(t) { return /^\d+$/.test(String(t)); } function Pt(t) { return /^[\u4e00-\u9fa5]+$/.test(t); } function Dt(t) { return /^[a-zA-Z]+$/.test(t); } const bt = { isEmail: ot, isMobile: it, validateIDCard: st, checkCode: M, isValidDate: w, convertTo18Digits: y, isURL: ct, isIP: at, isBankCard: ut, isChineseName: lt, isPostalCode: ft, isLicensePlate: dt, isUnifiedSocialCreditCode: pt, isNumber: ht, isInteger: gt, isDecimal: mt, isEmpty: yt, isLength: Mt, isPositiveInteger: wt, isNonNegativeInteger: xt, isChinese: Pt, isEnglish: Dt }; (function(t, e) { typeof define == "function" && define.amd ? define([], e) : typeof exports == "object" && typeof module < "u" ? module.exports = e() : t.crossEnvPlugins = e(); })(typeof self < "u" ? self : typeof globalThis < "u" ? globalThis : typeof global < "u" ? global : {}, function() { return { utils: rt, valids: bt }; }); export { rt as utils, bt as valids };