UNPKG

common-utils-y

Version:

my common utils lib

2 lines (1 loc) 5.41 kB
(function(i,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(i=typeof globalThis<"u"?globalThis:i||self,d(i.CommonUtils_y={}))})(this,function(i){"use strict";function d(t){return typeof t=="boolean"}function b(t){return typeof t=="number"}function p(t){return typeof t=="string"}function A(t){return typeof t=="function"}function T(t){return typeof t=="bigint"}function c(t){return Array.isArray(t)}function D(t){return t==null}function E(t){if(!c(t))throw new Error("method uniqueArray need param array");return Array.from(new Set(t))}function C(t,e){if(!c(t))throw new Error("method getUniqueArray need first param array");if(!p(e))throw new Error("method getUniqueArray need second param string");let n=new Set;const r=t.filter(a=>{const o=a[e];return n.has(o)?!1:(n.add(o),!0)});return n=null,r}function H(t,e,n="children"){const r=c(t)?t:[t];function a(o,u){var s;if(u.push(o),e(o))return u;if(((s=o[n])==null?void 0:s.length)>0)for(const X of o[n]){const w=a(X,[...u]);if(w)return w}return u.pop(),null}for(const o of r){const u=a(o,[]);if(u)return u}return[]}const y={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},N=Object.keys(y);function L(t,e=N){return c(e)?e.reduce((n,r)=>n.replace(new RegExp(r,"g"),y[r]),t):t}const h={"&":"\\26 ","<":"\\3C ",">":"\\3E ",'"':"\\22 ","'":"\\27 "},q=Object.keys(h);function P(t,e=q){return c(e)?e.reduce((n,r)=>n.replace(new RegExp(r,"g"),h[r]),t):t}function j(t){return t.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(/"/g,'\\"').replace(/`/g,"\\`").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}function m(t){return t.charAt(0).toUpperCase()+t.slice(1)}function F(t,e="-"){return t.split(e).map((r,a)=>a===0?r:m(r)).join("")}function I(t,e){const n=Math.pow(10,Math.max(l(t),l(e)));return(Math.round(t*n)+Math.round(e*n))/n}function R(t,e){const n=Math.pow(10,Math.max(l(t),l(e)));return(Math.round(t*n)-Math.round(e*n))/n}function U(t,e){const n=Math.pow(10,l(t)),r=Math.pow(10,l(e));return Math.round(t*n)*Math.round(e*r)/(n*r)}function B(t,e){const n=l(t),r=l(e),a=Math.max(n,r),o=Math.pow(10,a);return Math.round(t*o)/Math.round(e*o)}function l(t){const n=t.toString().toLowerCase().split("e"),r=n[0],a=n.length>1?parseInt(n[1],10):0,u=r.split(".")[1]||"";return a<0?Math.abs(a)+u.length:a>0?Math.max(0,u.length-a):u.length}function _(...t){return t.length?t.reduce((e,n)=>U(e,n)):NaN}function k(...t){return t.length?t.reduce((e,n)=>I(e,n)):NaN}function v(...t){return t.length?t.reduce((e,n)=>R(e,n)):NaN}function O(...t){return t.length?t.reduce((e,n)=>B(e,n)):NaN}function V(t,e){const n=Math.ceil(Math.min(t,e)),r=Math.floor(Math.max(t,e));return Math.floor(Math.random()*(r-n+1))+n}function W(t){const n=t.toString().split(".");return n[0]=n[0].replace(/\B(?=(\d{3})+(?!\d))/g,","),n.join(".")}function f(t,e=new WeakMap){if(t===null||typeof t!="object")return t;if(e.has(t))return e.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=new Set;return e.set(t,r),t.forEach(a=>{r.add(f(a,e))}),r}if(t instanceof Map){const r=new Map;return e.set(t,r),t.forEach((a,o)=>{r.set(f(o,e),f(a,e))}),r}if(Array.isArray(t)){const r=[];return e.set(t,r),t.forEach(a=>{r.push(f(a,e))}),r}const n={};e.set(t,n);for(const r in t)t.hasOwnProperty(r)&&(n[r]=f(t[r],e));return n}function z(t){return f(t)}function J(t,e=10){let n=null;return function(...r){const a=this;return n&&clearTimeout(n),n=setTimeout(()=>{t.apply(a,r)},e),()=>{clearTimeout(n)}}}function Q(t,e,n=!0,r=!1){let a=0,o=null;return function(...u){const s=Date.now();!n&&a===0&&(a=s),o&&(clearTimeout(o),o=null),s-a>=e?(a=s,t.apply(this,u)):r&&(o=setTimeout(()=>{a=n?Date.now():0,t.apply(this,u)},e-(s-a)))}}function M(t){return t instanceof Date}function S(t){return M(t)&&!Number.isNaN(t.getTime())}const g={yyyy:t=>t.getFullYear(),yy:t=>String(t.getFullYear()).slice(-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"),H:t=>t.getHours(),hh:t=>String(t.getHours()%12||12).padStart(2,"0"),h:t=>t.getHours()%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),w:t=>["日","一","二","三","四","五","六"][t.getDay()]};function Y(t,e){var n;return(n=g[t])==null?void 0:n.call(g,e)}function K(t,e="yyyy-MM-dd"){if(!S(t))throw new TypeError("第一个参数必须合法是 Date 对象");return e.replace(/\{?yyyy\}?|\{?yy\}?|\{?MM\}?|\{?M|\}?\{?dd\}?|\{?d\}?|\{?HH\}?|\{?H\}?|\{?hh\}?|\{?h\}?|\{?mm\}?|\{?m\}?|\{?ss\}?|\{?s\}?|\{?a\}?|\{?Q\}?|\{?w\}?/g,n=>G(n)?n.replace("{","").replace("}",""):String(Y(n,t)))}function G(t){return t.startsWith("{")&&t.endsWith("}")}i.add=k,i.addCommas=W,i.capitalizeFirstLetter=m,i.debounce=J,i.deepCopy=z,i.divide=O,i.escapeCSS=P,i.escapeHTML=L,i.escapeJS=j,i.findPathInTree=H,i.formatDate=K,i.getRandomInt=V,i.getUniqueArray=C,i.isArray=c,i.isBigInt=T,i.isBoolean=d,i.isDate=M,i.isEmpty=D,i.isFunction=A,i.isNumber=b,i.isString=p,i.isValidDate=S,i.multiply=_,i.subtract=v,i.throttle=Q,i.toCamelStyle=F,i.uniqueArray=E,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});