UNPKG

@alessiofrittoli/web-utils

Version:
1 lines 10.2 kB
'use strict';var urlUtils=require('@alessiofrittoli/url-utils'),dateUtils=require('@alessiofrittoli/date-utils'),mathUtils=require('@alessiofrittoli/math-utils');var T=e=>typeof window<"u"?window.matchMedia(e).matches:false;var u=e=>new Map(e);var q=(e={})=>{let t=Math.max(100,e.width||600),n=Math.max(100,e.height||800),{url:r,context:s}=e,{features:a={}}=e,{left:o=window.screen.width/2-(t/2+10),top:c=window.screen.height/2-(n/2+50),...f}=a,m=u([["popup","yes"],["width",t.toString()],["height",n.toString()],["left",o.toString()],["top",c.toString()]]);Object.entries(f).map(([p,R])=>{m.set(p,R);});let x=Array.from(m).map(p=>p.join("=")).join(",");return window.open(r&&urlUtils.Url.format(r),s,x)};var v="(orientation:portrait)",_=()=>T(v);var ne=()=>process.env.NODE_ENV==="development",re=()=>process.env.NODE_ENV==="test"||process.env.JEST_WORKER_ID!==void 0,y=e=>e instanceof Promise,oe=e=>e instanceof Function&&e.constructor.name==="AsyncFunction",b=e=>typeof e=="string"||e instanceof String,j=e=>!!e&&e.constructor===Array,se=e=>j(e)&&e.length<=0,ie=e=>!!e&&e.constructor===Object,ae=e=>Object.keys(e).length===0,ce=e=>{try{JSON.parse(e);}catch{return false}return true},pe=e=>e instanceof NodeList,ue=e=>e instanceof Element,le=e=>window.TouchEvent&&e instanceof TouchEvent,ge=()=>typeof window<"u"&&"ontouchstart"in window&&typeof window.ontouchstart=="function"||typeof navigator<"u"&&navigator.maxTouchPoints>0||typeof navigator<"u"&&"msMaxTouchPoints"in navigator&&navigator.msMaxTouchPoints>0;var F=e=>e.trim().length>0,w=e=>!F(e),me=e=>/^[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,})+$/.test(e),xe=e=>/^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]{8,14}$/g.test(e),Te=e=>/^(ATU[0-9]{8}|BE[01][0-9]{9}|BG[0-9]{9,10}|HR[0-9]{11}|CY[A-Z0-9]{9}|CZ[0-9]{8,10}|DK[0-9]{8}|EE[0-9]{9}|FI[0-9]{8}|FR[0-9A-Z]{2}[0-9]{9}|DE[0-9]{9}|EL[0-9]{9}|HU[0-9]{8}|IE([0-9]{7}[A-Z]{1,2}|[0-9][A-Z][0-9]{5}[A-Z])|IT[0-9]{11}|LV[0-9]{11}|LT([0-9]{9}|[0-9]{12})|LU[0-9]{8}|MT[0-9]{8}|NL[0-9]{9}B[0-9]{2}|PL[0-9]{10}|PT[0-9]{9}|RO[0-9]{2,10}|SK[0-9]{10}|SI[0-9]{8}|ES[A-Z]([0-9]{8}|[0-9]{7}[A-Z])|SE[0-9]{12}|GB([0-9]{9}|[0-9]{12}|GD[0-4][0-9]{2}|HA[5-9][0-9]{2}))$/g.test(e.trim()),ye=(e,t=0)=>typeof t=="string"?t.trim().length<e:t<e,be=(e,t=0)=>typeof t=="string"?t.trim().length>e:t>e,we=(e,t,n=0)=>typeof n=="string"?n.trim().length>=e&&n.trim().length<=t:+n>=+e&&+n<=+t,he=(e,t)=>e===t,ke=e=>typeof e=="string"&&[/\bselect\b\s+.*?\bfrom\b/,/\bdelete\b\s+.*?\bfrom\b/,/\binsert\b\s+.*?\binto\b/,/\bupdate\b\s+.*?\bset\b/,/\bunion\b\s+.*?\bselect\b/,/\breplace\b\s+.*?\binto\b/,/\bcreate\b\s+.*?\b(table|view|index|database)\b/,/\bdrop\b\s+.*?\b(table|view|index|database)\b/,/\balter\b\s+.*?\b(table|view|index|database)\b/,/\brename\b\s+.*?\b(table|view|index|database)\b/,/;\s*--/].some(t=>t.test(e.toLowerCase()));var d=e=>e.replace(/[-\\^$*+?.()|[\]{}]/g,"\\$&");var k=e=>e.charAt(0).toUpperCase()+e.slice(1),K=e=>e.charAt(0).toLowerCase()+e.slice(1),E=e=>I(e).replace(/[-_\s](.)/g,(t,n)=>n.toUpperCase()),I=e=>e.replace(/\s+/g,"-").replace(/_/g,"-").replace(/([A-Z])/g,"-$1").replace(/[^a-zA-Z0-9-]/g,"-").replace(/--+/g,"-").toLowerCase(),l=e=>y(e)||typeof e=="function"?"":e instanceof Date&&dateUtils.isValidDate(e)?e.toISOString():e instanceof Map||e instanceof Headers?JSON.stringify(Array.from(e.entries())):typeof e=="object"?JSON.stringify(e):e?.toString()||"",g=e=>{if(e&&!(b(e)&&w(e))){if(mathUtils.isNumeric(e))return parseFloat(e);try{let t=new Date(e);return dateUtils.isValidDate(t)?t:JSON.parse(e)}catch{return e}}},Me=(e,t,n=t)=>{if(typeof e!="string")throw new TypeError("Input must be a string.");let r=n instanceof RegExp?n:new RegExp(`^[${d(n)}]?`);return e.replace(r,t)},ve=(e,t)=>{if(typeof e!="string")throw new TypeError("Input must be a string.");let n=t instanceof RegExp?t:new RegExp(`^[${d(t)}]+`);return e.replace(n,"")},je=(e,t,n=t)=>{if(typeof e!="string")throw new TypeError("Input must be a string.");let r=n instanceof RegExp?n:new RegExp(`[${d(n)}]?$`);return e.replace(r,t)},Fe=(e,t)=>{if(typeof e!="string")throw new TypeError("Input must be a string.");let n=t instanceof RegExp?t:new RegExp(`[${d(t)}]?$`);return e.replace(n,"")};var U=(r=>(r.Low="Low",r.Medium="Medium",r.High="High",r))(U||{}),D=(r=>(r.Strict="Strict",r.Lax="Lax",r.None="None",r))(D||{}),A=class e{static get(t){return e.fromListString(document.cookie).get(t)}static getAll(){return e.fromListString(document.cookie)}static set(t){let n=e.parse(t);return n.has("path")||n.set("path","/"),typeof document<"u"?(document.cookie=e.toString(n),n):false}static delete(t){return !!e.set({name:t,maxAge:0})}static parse(t){if(t instanceof Map)return t;let n=typeof t.expires<"u"?new Date(t.expires):void 0,r=u();return r.set("name",t.name).set("value",t.value),n&&r.set("expires",n),typeof t.maxAge<"u"&&r.set("maxAge",t.maxAge),t.path&&r.set("path",t.path),t.priority&&r.set("priority",t.priority),t.domain&&r.set("domain",t.domain),typeof t.httpOnly<"u"&&r.set("httpOnly",t.httpOnly),typeof t.secure<"u"&&r.set("secure",t.secure),t.sameSite&&r.set("sameSite",t.sameSite),typeof t.partitioned<"u"&&r.set("partitioned",t.partitioned),r}static toString(t){let n=e.parse(t),r=Array.from(n).filter(([o])=>o!=="name"&&o!=="value").filter(([,o])=>typeof o!="boolean"||!!o),s=n.get("name");return [[s,n.get("value")],...r].map(([o,c])=>(o=o!==s?k(o.toString()):o,o==="Expires"&&dateUtils.isValidDate(c)&&(c=c.toUTCString()),o==="MaxAge"&&(o="Max-Age"),[o,l(c)].join("="))).filter(Boolean).join(";")}static fromString(t){let[n,...r]=t.split(";");if(!n)return null;let[s,a]=n.split("="),o=r.map(c=>{let[f,m]=c.split("=");if(!f||!f.trim().length)return null;let x=K(E(f)),p=e.parseValue(m,x);return p==null?null:[x,p]}).filter(Boolean);return u([["name",s],["value",e.parseValue(a)],...o])}static fromListString(t){let n=u();return t.split("; ").map(r=>{let s=e.fromString(r);if(!s)return null;n.set(s.get("name"),s);}),n}static parseValue(t,n){if(n==="expires"&&t){let r=new Date(t);return dateUtils.isValidDate(r)?r:void 0}return n==="httpOnly"||n==="secure"||n==="partitioned"?t!=="false":g(t)}};var S=class e{static get(t){return g(localStorage.getItem(t)||void 0)}static set(t,n){let r=l(n);return n==null||!r?e.delete(t):localStorage.setItem(t,r)}static delete(t){return localStorage.removeItem(t)}static clear(){return localStorage.clear()}static key(t){return localStorage.key(t)}static getLength(){return localStorage.length}};var P=class e{static get(t){return g(sessionStorage.getItem(t)||void 0)}static set(t,n){let r=l(n);return n==null||!r?e.delete(t):sessionStorage.setItem(t,r)}static delete(t){return sessionStorage.removeItem(t)}static clear(){return sessionStorage.clear()}static key(t){return sessionStorage.key(t)}static getLength(){return sessionStorage.length}};var qe=e=>e.length<=0?e:[...new Set(e)],Qe=(e,t)=>{if(e.length<=0)return e;let n=new Map;return e.map(r=>{n.has(r[t])||n.set(r[t],r);}),Array.from(n.values())},Ye=e=>e.replace(/\s/g,"").split(",").filter(Boolean);function Xe(e,t){let n=t&&"size"in t&&t.size,r=t&&"count"in t&&t.count,s=r?Math.ceil(e.length/r):n||1,a=r||Math.ceil(e.length/s);return Array.from({length:a},(o,c)=>e.slice(c*s,c*s+s)).filter(o=>o.length>0)}var tt=async(e,t,n)=>{let s=await new Response(t,n).blob(),a=document.createElement("a");a.href=URL.createObjectURL(s),a.download=e,a.click(),URL.revokeObjectURL(a.href),a.remove();};var ot=(e=document.documentElement)=>{let{width:t}=e.getBoundingClientRect();e.style.overflow="hidden";let n=Math.max(0,e.clientWidth-t);e.style.paddingRight=`${n}px`,e.style.setProperty("--scrollbar-size",`${n}px`);},st=(e=document.documentElement)=>{e.style.removeProperty("overflow"),e.style.removeProperty("padding-right"),e.style.removeProperty("--scrollbar-size");};var ct=e=>!!e&&(C(e)||O(e)),C=e=>!!e&&(typeof e=="function"||typeof e=="object")&&e.constructor.name==="GeneratorFunction",O=e=>!!e&&(typeof e=="function"||typeof e=="object")&&e.constructor.name==="AsyncGeneratorFunction",pt=e=>!!e&&(G(e)||N(e)),G=e=>!!e&&typeof e=="object"&&C(e.constructor),N=e=>!!e&&typeof e=="object"&&O(e.constructor);var gt=e=>{let t={...e};for(let n in t)t[n]==null&&delete t[n];return t},ft=e=>Object.fromEntries(e.entries()),dt=e=>Object.assign(Object.create(Object.getPrototypeOf(e)),e),mt=(e,t)=>Object.keys(e).find(n=>e[n]===t);var yt=e=>new Promise(t=>setTimeout(t,e)),z=(e,...t)=>new Promise((n,r)=>{let s=()=>{setTimeout(async()=>{try{n(await e(...t));}catch(a){r(a);}},0);};if(typeof requestAnimationFrame!="function")return s();requestAnimationFrame(s);}),bt=e=>(...t)=>z(e,...t);exports.Cookie=A;exports.LocalStorage=S;exports.Priority=U;exports.SameSite=D;exports.SessionStorage=P;exports.addLeadingCharacter=Me;exports.addTrailingCharacter=je;exports.arrayObjectUnique=Qe;exports.arrayUnique=qe;exports.blockScroll=ot;exports.chunkInto=Xe;exports.cloneObject=dt;exports.deferCallback=bt;exports.deferTask=z;exports.downloadBlob=tt;exports.escapeRegExpCharSet=d;exports.filterObject=gt;exports.getMediaMatches=T;exports.getObjectKey=mt;exports.getTypedMap=u;exports.isArray=j;exports.isAsyncFunction=oe;exports.isAsyncGeneratorFunction=O;exports.isAsyncGeneratorObject=N;exports.isDefaultGeneratorFunction=C;exports.isDefaultGeneratorObject=G;exports.isDev=ne;exports.isEmpty=w;exports.isEmptyArray=se;exports.isEmptyObject=ae;exports.isGeneratorFunction=ct;exports.isGeneratorObject=pt;exports.isGreaterThan=be;exports.isInRange=we;exports.isJson=ce;exports.isLessThan=ye;exports.isNodeElement=ue;exports.isNodeList=pe;exports.isNotEmpty=F;exports.isObject=ie;exports.isPortrait=_;exports.isPromise=y;exports.isSQLStatement=ke;exports.isStrictEqual=he;exports.isString=b;exports.isTest=re;exports.isTouchDevice=ge;exports.isTouchEvent=le;exports.isValidEmail=me;exports.isValidPhoneNumber=xe;exports.isValidVat=Te;exports.lcFirst=K;exports.listToArray=Ye;exports.mapToObject=ft;exports.openBrowserPopUp=q;exports.parseValue=g;exports.portraitMediaQuery=v;exports.removeLeadingCharacter=ve;exports.removeTrailingCharacter=Fe;exports.restoreScroll=st;exports.sleep=yt;exports.stringifyValue=l;exports.toCamelCase=E;exports.toKebabCase=I;exports.ucFirst=k;