UNPKG

common-utils-y

Version:

my common utils lib

2 lines (1 loc) 5.2 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 A(t){return typeof t=="number"}function y(t){return typeof t=="string"}function b(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 H(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=new Set;const r=t.filter(a=>{const o=a[n];return e.has(o)?!1:(e.add(o),!0)});return e=null,r}function N(t,n,e="children"){const r=c(t)?t:[t];function a(o,u){var l;if(u.push(o),n(o))return u;if(((l=o[e])==null?void 0:l.length)>0)for(const X of o[e]){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 p={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},C=Object.keys(p);function q(t,n=C){return c(n)?n.reduce((e,r)=>e.replace(new RegExp(r,"g"),p[r]),t):t}const h={"&":"\\26 ","<":"\\3C ",">":"\\3E ",'"':"\\22 ","'":"\\27 "},j=Object.keys(h);function F(t,n=j){return c(n)?n.reduce((e,r)=>e.replace(new RegExp(r,"g"),h[r]),t):t}function I(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 R(t,n="-"){return t.split(n).map((r,a)=>a===0?r:m(r)).join("")}function U(t,n){const e=Math.pow(10,Math.max(f(t),f(n)));return(t*e+n*e)/e}function B(t,n){const e=Math.pow(10,Math.max(f(t),f(n)));return(t*e-n*e)/e}function L(t,n){const e=Math.pow(10,f(t)),r=Math.pow(10,f(n));return t*e*(n*r)/(e*r)}function O(t,n){const e=Math.pow(10,f(t)),r=Math.pow(10,f(n));return t*e/(n*r)}function f(t){const n=String(t),e=n.indexOf(".");return e===-1?0:n.length-e-1}function _(...t){return t.length?t.reduce((n,e)=>L(n,e)):NaN}function k(...t){return t.length?t.reduce((n,e)=>U(n,e)):NaN}function v(...t){return t.length?t.reduce((n,e)=>B(n,e)):NaN}function P(...t){return t.length?t.reduce((n,e)=>O(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 W(t){const e=t.toString().split(".");return e[0]=e[0].replace(/\B(?=(\d{3})+(?!\d))/g,","),e.join(".")}function s(t,n=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=new Set;return n.set(t,r),t.forEach(a=>{r.add(s(a,n))}),r}if(t instanceof Map){const r=new Map;return n.set(t,r),t.forEach((a,o)=>{r.set(s(o,n),s(a,n))}),r}if(Array.isArray(t)){const r=[];return n.set(t,r),t.forEach(a=>{r.push(s(a,n))}),r}const e={};n.set(t,e);for(const r in t)t.hasOwnProperty(r)&&(e[r]=s(t[r],n));return e}function z(t){return s(t)}function J(t,n=10){let e=null;return function(...r){const a=this;return e&&clearTimeout(e),e=setTimeout(()=>{t.apply(a,r)},n),()=>{clearTimeout(e)}}}function Q(t,n,e=!0,r=!1){let a=0,o=null;return function(...u){const l=Date.now();!e&&a===0&&(a=l),o&&(clearTimeout(o),o=null),l-a>=n?(a=l,t.apply(this,u)):r&&(o=setTimeout(()=>{a=e?Date.now():0,t.apply(this,u)},n-(l-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,n){var e;return(e=g[t])==null?void 0:e.call(g,n)}function K(t,n="yyyy-MM-dd"){if(!S(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=>G(e)?e.replace("{","").replace("}",""):String(Y(e,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=P,i.escapeCSS=F,i.escapeHTML=q,i.escapeJS=I,i.findPathInTree=N,i.formatDate=K,i.getRandomInt=V,i.getUniqueArray=H,i.isArray=c,i.isBigInt=T,i.isBoolean=d,i.isDate=M,i.isEmpty=D,i.isFunction=b,i.isNumber=A,i.isString=y,i.isValidDate=S,i.multiply=_,i.subtract=v,i.throttle=Q,i.toCamelStyle=R,i.uniqueArray=E,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});