UNPKG

common-utils-y

Version:

my common utils lib

2 lines (1 loc) 6.7 kB
(function(i,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(i=typeof globalThis<"u"?globalThis:i||self,y(i.CommonUtils_y={}))})(this,function(i){"use strict";function y(e){return typeof e=="boolean"}function T(e){return typeof e=="number"}function b(e){return typeof e=="string"}function C(e){return typeof e=="function"}function N(e){return typeof e=="bigint"}function d(e){return Array.isArray(e)}function D(e){return e==null}function F(){return typeof window=="object"&&typeof document=="object"&&typeof document.body=="object"}const S=F();S||console.warn("this is not browser env!!!");let m="requestFullscreen",p="exitFullscreen";const s=S?document:{body:{}};s.body.requestFullscreen?m="requestFullscreen":s.body.webkitRequestFullscreen?m="webkitRequestFullscreen":s.body.mozRequestFullScreen?m="mozRequestFullScreen":s.body.msRequestFullscreen&&(m="msRequestFullscreen"),s.exitFullscreen?p="exitFullscreen":s.webkitExitFullscreen?p="webkitExitFullscreen":s.mozCancelFullScreen?p="mozCancelFullScreen":s.msExitFullscreen&&(p="msExitFullscreen");function H(e,n){var t;return(t=e==null?void 0:e[m])==null?void 0:t.call(e,n)}function O(){var e;return(e=s[p])==null?void 0:e.call(s)}function B(){return s.fullscreenElement||s.webkitFullscreenElement||s.mozFullScreenElement||s.msFullscreenElement}const f=new WeakMap,w=new ResizeObserver(e=>{e.forEach(n=>{const{target:t,contentBoxSize:r,contentRect:o,borderBoxSize:u,devicePixelContentBoxSize:l}=n,a=f.get(t);a==null||a.forEach(h=>{h==null||h({contentBoxSize:r,contentRect:o,borderBoxSize:u,devicePixelContentBoxSize:l})})})});function j(e,n){f.has(e)||f.set(e,new Set),f.get(e).add(n),w.observe(e)}function I(e,n){const t=f.get(e);if(t){if(n){t.delete(n),t.size||(f.delete(e),w.unobserve(e));return}f.delete(e),w.unobserve(e)}}function _(e){if(!d(e))throw new Error("method uniqueArray need param array");return Array.from(new Set(e))}function U(e,n){if(!d(e))throw new Error("method getUniqueArray need first param array");if(!b(n))throw new Error("method getUniqueArray need second param string");let t=new Set;const r=e.filter(o=>{const u=o[n];return t.has(u)?!1:(t.add(u),!0)});return t=null,r}function W(e,n,t="children"){const r=d(e)?e:[e];function o(u,l){var a;if(l.push(u),n(u))return l;if(((a=u[t])==null?void 0:a.length)>0)for(const h of u[t]){const z=o(h,[...l]);if(z)return z}return l.pop(),null}for(const u of r){const l=o(u,[]);if(l)return l}return[]}const E={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"},k=Object.keys(E);function L(e,n=k){return d(n)?n.reduce((t,r)=>t.replace(new RegExp(r,"g"),E[r]),e):e}const R={"&":"\\26 ","<":"\\3C ",">":"\\3E ",'"':"\\22 ","'":"\\27 "},P=Object.keys(R);function V(e,n=P){return d(n)?n.reduce((t,r)=>t.replace(new RegExp(r,"g"),R[r]),e):e}function J(e){return e.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(/"/g,'\\"').replace(/`/g,"\\`").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}function A(e){return e.charAt(0).toUpperCase()+e.slice(1)}function Q(e,n="-"){return e.split(n).map((r,o)=>o===0?r:A(r)).join("")}function Y(e,n){const t=Math.pow(10,Math.max(c(e),c(n)));return(e*t+n*t)/t}function K(e,n){const t=Math.pow(10,Math.max(c(e),c(n)));return(e*t-n*t)/t}function G(e,n){const t=Math.pow(10,c(e)),r=Math.pow(10,c(n));return e*t*(n*r)/(t*r)}function X(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)=>G(n,t)):NaN}function $(...e){return e.length?e.reduce((n,t)=>Y(n,t)):NaN}function x(...e){return e.length?e.reduce((n,t)=>K(n,t)):NaN}function ee(...e){return e.length?e.reduce((n,t)=>X(n,t)):NaN}function te(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 ne(e){const t=e.toString().split(".");return t[0]=t[0].replace(/\B(?=(\d{3})+(?!\d))/g,","),t.join(".")}function g(e,n=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=new Set;return n.set(e,r),e.forEach(o=>{r.add(g(o,n))}),r}if(e instanceof Map){const r=new Map;return n.set(e,r),e.forEach((o,u)=>{r.set(g(u,n),g(o,n))}),r}if(Array.isArray(e)){const r=[];return n.set(e,r),e.forEach(o=>{r.push(g(o,n))}),r}const t={};n.set(e,t);for(const r in e)e.hasOwnProperty(r)&&(t[r]=g(e[r],n));return t}function re(e){return g(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 oe(e,n,t=!0,r=!1){let o=0,u=null;return function(...l){const a=Date.now();!t&&o===0&&(o=a),u&&(clearTimeout(u),u=null),a-o>=n?(o=a,e.apply(this,l)):r&&(u=setTimeout(()=>{o=t?Date.now():0,e.apply(this,l)},n-(a-o)))}}function q(e){return e instanceof Date}function v(e){return q(e)&&!Number.isNaN(e.getTime())}const M={yyyy:e=>e.getFullYear(),yy:e=>String(e.getFullYear()).slice(-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"),H:e=>e.getHours(),hh:e=>String(e.getHours()%12||12).padStart(2,"0"),h:e=>e.getHours()%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),w:e=>["日","一","二","三","四","五","六"][e.getDay()]};function ue(e,n){var t;return(t=M[e])==null?void 0:t.call(M,n)}function se(e,n="yyyy-MM-dd"){if(!v(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=>le(t)?t.replace("{","").replace("}",""):String(ue(t,e)))}function le(e){return e.startsWith("{")&&e.endsWith("}")}i.IS_BROWSER=S,i.add=$,i.addCommas=ne,i.addResizeObserve=j,i.capitalizeFirstLetter=A,i.debounce=ie,i.deepCopy=re,i.divide=ee,i.enterFullscreen=H,i.escapeCSS=V,i.escapeHTML=L,i.escapeJS=J,i.exitFullscreen=O,i.findPathInTree=W,i.formatDate=se,i.getFullscreenElement=B,i.getRandomInt=te,i.getUniqueArray=U,i.isArray=d,i.isBigInt=N,i.isBoolean=y,i.isBrowser=F,i.isDate=q,i.isEmpty=D,i.isFunction=C,i.isNumber=T,i.isString=b,i.isValidDate=v,i.multiply=Z,i.removeResizeObserve=I,i.subtract=x,i.throttle=oe,i.toCamelStyle=Q,i.uniqueArray=_,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});