UNPKG

@alessiofrittoli/web-utils

Version:
1 lines 9.78 kB
import {Url}from'@alessiofrittoli/url-utils';import {isValidDate}from'@alessiofrittoli/date-utils';import {isNumeric}from'@alessiofrittoli/math-utils';var y=e=>typeof window<"u"?window.matchMedia(e).matches:false;var l=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:c={}}=e,{left:o=window.screen.width/2-(t/2+10),top:p=window.screen.height/2-(n/2+50),...d}=c,x=l([["popup","yes"],["width",t.toString()],["height",n.toString()],["left",o.toString()],["top",p.toString()]]);Object.entries(d).map(([u,M])=>{x.set(u,M);});let T=Array.from(x).map(u=>u.join("=")).join(",");return window.open(r&&Url.format(r),s,T)};var j="(orientation:portrait)",ee=()=>y(j);var re=()=>process.env.NODE_ENV==="development",oe=()=>process.env.NODE_ENV==="test"||process.env.JEST_WORKER_ID!==void 0,b=e=>e instanceof Promise,se=e=>e instanceof Function&&e.constructor.name==="AsyncFunction",w=e=>typeof e=="string"||e instanceof String,F=e=>!!e&&e.constructor===Array,ie=e=>F(e)&&e.length<=0,ae=e=>!!e&&e.constructor===Object,ce=e=>Object.keys(e).length===0,pe=e=>{try{JSON.parse(e);}catch{return false}return true},ue=e=>e instanceof NodeList,le=e=>e instanceof Element,ge=e=>window.TouchEvent&&e instanceof TouchEvent,fe=()=>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 L=e=>e.trim().length>0,h=e=>!L(e),xe=e=>/^[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z]{2,})+$/.test(e),Te=e=>/^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]{8,14}$/g.test(e),ye=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()),be=(e,t=0)=>typeof t=="string"?t.trim().length<e:t<e,we=(e,t=0)=>typeof t=="string"?t.trim().length>e:t>e,he=(e,t,n=0)=>typeof n=="string"?n.trim().length>=e&&n.trim().length<=t:+n>=+e&&+n<=+t,ke=(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 m=e=>e.replace(/[-\\^$*+?.()|[\]{}]/g,"\\$&");var K=e=>e.charAt(0).toUpperCase()+e.slice(1),E=e=>e.charAt(0).toLowerCase()+e.slice(1),V=e=>U(e).replace(/[-_\s](.)/g,(t,n)=>n.toUpperCase()),U=e=>e.replace(/\s+/g,"-").replace(/_/g,"-").replace(/([A-Z])/g,"-$1").replace(/[^a-zA-Z0-9-]/g,"-").replace(/--+/g,"-").toLowerCase(),g=e=>b(e)||typeof e=="function"?"":e instanceof Date&&isValidDate(e)?e.toISOString():e instanceof Map||e instanceof Headers?JSON.stringify(Array.from(e.entries())):typeof e=="object"?JSON.stringify(e):e?.toString()||"",f=e=>{if(e&&!(w(e)&&h(e))){if(isNumeric(e))return parseFloat(e);try{let t=new Date(e);return isValidDate(t)?t:JSON.parse(e)}catch{return e}}},ve=(e,t,n=t)=>{if(typeof e!="string")throw new TypeError("Input must be a string.");let r=n instanceof RegExp?n:new RegExp(`^[${m(n)}]?`);return e.replace(r,t)},je=(e,t)=>{if(typeof e!="string")throw new TypeError("Input must be a string.");let n=t instanceof RegExp?t:new RegExp(`^[${m(t)}]+`);return e.replace(n,"")},Fe=(e,t,n=t)=>{if(typeof e!="string")throw new TypeError("Input must be a string.");let r=n instanceof RegExp?n:new RegExp(`[${m(n)}]?$`);return e.replace(r,t)},Le=(e,t)=>{if(typeof e!="string")throw new TypeError("Input must be a string.");let n=t instanceof RegExp?t:new RegExp(`[${m(t)}]?$`);return e.replace(n,"")};var D=(r=>(r.Low="Low",r.Medium="Medium",r.High="High",r))(D||{}),G=(r=>(r.Strict="Strict",r.Lax="Lax",r.None="None",r))(G||{}),S=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=l();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,p])=>(o=o!==s?K(o.toString()):o,o==="Expires"&&isValidDate(p)&&(p=p.toUTCString()),o==="MaxAge"&&(o="Max-Age"),[o,g(p)].join("="))).filter(Boolean).join(";")}static fromString(t){let[n,...r]=t.split(";");if(!n)return null;let[s,c]=n.split("="),o=r.map(p=>{let[d,x]=p.split("=");if(!d||!d.trim().length)return null;let T=E(V(d)),u=e.parseValue(x,T);return u==null?null:[T,u]}).filter(Boolean);return l([["name",s],["value",e.parseValue(c)],...o])}static fromListString(t){let n=l();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 isValidDate(r)?r:void 0}return n==="httpOnly"||n==="secure"||n==="partitioned"?t!=="false":f(t)}};var P=class e{static get(t){return f(localStorage.getItem(t)||void 0)}static set(t,n){let r=g(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 C=class e{static get(t){return f(sessionStorage.getItem(t)||void 0)}static set(t,n){let r=g(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)],Ye=(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())},Xe=e=>e.replace(/\s/g,"").split(",").filter(Boolean);function _e(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,c=r||Math.ceil(e.length/s);return Array.from({length:c},(o,p)=>e.slice(p*s,p*s+s)).filter(o=>o.length>0)}var nt=async(e,t,n)=>{let s=await new Response(t,n).blob(),c=document.createElement("a");c.href=URL.createObjectURL(s),c.download=e,c.click(),URL.revokeObjectURL(c.href),c.remove();};var st=(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`);},it=(e=document.documentElement)=>{e.style.removeProperty("overflow"),e.style.removeProperty("padding-right"),e.style.removeProperty("--scrollbar-size");};var pt=e=>!!e&&(O(e)||R(e)),O=e=>!!e&&(typeof e=="function"||typeof e=="object")&&e.constructor.name==="GeneratorFunction",R=e=>!!e&&(typeof e=="function"||typeof e=="object")&&e.constructor.name==="AsyncGeneratorFunction",ut=e=>!!e&&(N(e)||z(e)),N=e=>!!e&&typeof e=="object"&&O(e.constructor),z=e=>!!e&&typeof e=="object"&&R(e.constructor);var ft=e=>{let t={...e};for(let n in t)t[n]==null&&delete t[n];return t},dt=e=>Object.fromEntries(e.entries()),mt=e=>Object.assign(Object.create(Object.getPrototypeOf(e)),e),xt=(e,t)=>Object.keys(e).find(n=>e[n]===t);var bt=e=>new Promise(t=>setTimeout(t,e)),B=(e,...t)=>new Promise((n,r)=>{let s=()=>{setTimeout(async()=>{try{n(await e(...t));}catch(c){r(c);}},0);};if(typeof requestAnimationFrame!="function")return s();requestAnimationFrame(s);}),wt=e=>(...t)=>B(e,...t);export{S as Cookie,P as LocalStorage,D as Priority,G as SameSite,C as SessionStorage,ve as addLeadingCharacter,Fe as addTrailingCharacter,Ye as arrayObjectUnique,Qe as arrayUnique,st as blockScroll,_e as chunkInto,mt as cloneObject,wt as deferCallback,B as deferTask,nt as downloadBlob,m as escapeRegExpCharSet,ft as filterObject,y as getMediaMatches,xt as getObjectKey,l as getTypedMap,F as isArray,se as isAsyncFunction,R as isAsyncGeneratorFunction,z as isAsyncGeneratorObject,O as isDefaultGeneratorFunction,N as isDefaultGeneratorObject,re as isDev,h as isEmpty,ie as isEmptyArray,ce as isEmptyObject,pt as isGeneratorFunction,ut as isGeneratorObject,we as isGreaterThan,he as isInRange,pe as isJson,be as isLessThan,le as isNodeElement,ue as isNodeList,L as isNotEmpty,ae as isObject,ee as isPortrait,b as isPromise,Ke as isSQLStatement,ke as isStrictEqual,w as isString,oe as isTest,fe as isTouchDevice,ge as isTouchEvent,xe as isValidEmail,Te as isValidPhoneNumber,ye as isValidVat,E as lcFirst,Xe as listToArray,dt as mapToObject,Q as openBrowserPopUp,f as parseValue,j as portraitMediaQuery,je as removeLeadingCharacter,Le as removeTrailingCharacter,it as restoreScroll,bt as sleep,g as stringifyValue,V as toCamelCase,U as toKebabCase,K as ucFirst};