UNPKG

@alessiofrittoli/web-utils

Version:
1 lines 3.56 kB
import {isValidDate}from'@alessiofrittoli/date-utils';import {isNumeric}from'@alessiofrittoli/math-utils';var x=t=>t instanceof Promise;var b=t=>typeof t=="string"||t instanceof String;var A=t=>t.trim().length>0,T=t=>!A(t);var k=t=>t.charAt(0).toUpperCase()+t.slice(1),w=t=>t.charAt(0).toLowerCase()+t.slice(1),K=t=>R(t).replace(/[-_\s](.)/g,(e,n)=>n.toUpperCase()),R=t=>t.replace(/\s+/g,"-").replace(/_/g,"-").replace(/([A-Z])/g,"-$1").replace(/[^a-zA-Z0-9-]/g,"-").replace(/--+/g,"-").toLowerCase(),g=t=>x(t)||typeof t=="function"?"":t instanceof Date&&isValidDate(t)?t.toISOString():t instanceof Map||t instanceof Headers?JSON.stringify(Array.from(t.entries())):typeof t=="object"?JSON.stringify(t):t?.toString()||"",p=t=>{if(t&&!(b(t)&&T(t))){if(isNumeric(t))return parseFloat(t);try{let e=new Date(t);return isValidDate(e)?e:JSON.parse(t)}catch{return t}}};var f=t=>new Map(t);var M=(r=>(r.Low="Low",r.Medium="Medium",r.High="High",r))(M||{}),L=(r=>(r.Strict="Strict",r.Lax="Lax",r.None="None",r))(L||{}),V=class t{static get(e){return t.fromListString(document.cookie).get(e)}static getAll(){return t.fromListString(document.cookie)}static set(e){let n=t.parse(e);return n.has("path")||n.set("path","/"),typeof document<"u"?(document.cookie=t.toString(n),n):false}static delete(e){return !!t.set({name:e,maxAge:0})}static parse(e){if(e instanceof Map)return e;let n=typeof e.expires<"u"?new Date(e.expires):void 0,r=f();return r.set("name",e.name).set("value",e.value),n&&r.set("expires",n),typeof e.maxAge<"u"&&r.set("maxAge",e.maxAge),e.path&&r.set("path",e.path),e.priority&&r.set("priority",e.priority),e.domain&&r.set("domain",e.domain),typeof e.httpOnly<"u"&&r.set("httpOnly",e.httpOnly),typeof e.secure<"u"&&r.set("secure",e.secure),e.sameSite&&r.set("sameSite",e.sameSite),typeof e.partitioned<"u"&&r.set("partitioned",e.partitioned),r}static toString(e){let n=t.parse(e),r=Array.from(n).filter(([s])=>s!=="name"&&s!=="value").filter(([,s])=>typeof s!="boolean"||!!s),o=n.get("name");return [[o,n.get("value")],...r].map(([s,c])=>(s=s!==o?k(s.toString()):s,s==="Expires"&&isValidDate(c)&&(c=c.toUTCString()),s==="MaxAge"&&(s="Max-Age"),[s,g(c)].join("="))).filter(Boolean).join(";")}static fromString(e){let[n,...r]=e.split(";");if(!n)return null;let[o,d]=n.split("="),s=r.map(c=>{let[u,S]=c.split("=");if(!u||!u.trim().length)return null;let l=w(K(u)),m=t.parseValue(S,l);return m==null?null:[l,m]}).filter(Boolean);return f([["name",o],["value",t.parseValue(d)],...s])}static fromListString(e){let n=f();return e.split("; ").map(r=>{let o=t.fromString(r);if(!o)return null;n.set(o.get("name"),o);}),n}static parseValue(e,n){if(n==="expires"&&e){let r=new Date(e);return isValidDate(r)?r:void 0}return n==="httpOnly"||n==="secure"||n==="partitioned"?e!=="false":p(e)}};var h=class t{static get(e){return p(localStorage.getItem(e)||void 0)}static set(e,n){let r=g(n);return n==null||!r?t.delete(e):localStorage.setItem(e,r)}static delete(e){return localStorage.removeItem(e)}static clear(){return localStorage.clear()}static key(e){return localStorage.key(e)}static getLength(){return localStorage.length}};var C=class t{static get(e){return p(sessionStorage.getItem(e)||void 0)}static set(e,n){let r=g(n);return n==null||!r?t.delete(e):sessionStorage.setItem(e,r)}static delete(e){return sessionStorage.removeItem(e)}static clear(){return sessionStorage.clear()}static key(e){return sessionStorage.key(e)}static getLength(){return sessionStorage.length}};export{V as Cookie,h as LocalStorage,M as Priority,L as SameSite,C as SessionStorage};