UNPKG

@alessiofrittoli/web-utils

Version:
1 lines 3.63 kB
'use strict';var dateUtils=require('@alessiofrittoli/date-utils'),mathUtils=require('@alessiofrittoli/math-utils');var m=t=>t instanceof Promise;var x=t=>typeof t=="string"||t instanceof String;var C=t=>t.trim().length>0,b=t=>!C(t);var T=t=>t.charAt(0).toUpperCase()+t.slice(1),k=t=>t.charAt(0).toLowerCase()+t.slice(1),w=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(),c=t=>m(t)||typeof t=="function"?"":t instanceof Date&&dateUtils.isValidDate(t)?t.toISOString():t instanceof Map||t instanceof Headers?JSON.stringify(Array.from(t.entries())):typeof t=="object"?JSON.stringify(t):t?.toString()||"",g=t=>{if(t&&!(x(t)&&b(t))){if(mathUtils.isNumeric(t))return parseFloat(t);try{let e=new Date(t);return dateUtils.isValidDate(e)?e:JSON.parse(t)}catch{return t}}};var p=t=>new Map(t);var P=(r=>(r.Low="Low",r.Medium="Medium",r.High="High",r))(P||{}),L=(r=>(r.Strict="Strict",r.Lax="Lax",r.None="None",r))(L||{}),E=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=p();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,a])=>(s=s!==o?T(s.toString()):s,s==="Expires"&&dateUtils.isValidDate(a)&&(a=a.toUTCString()),s==="MaxAge"&&(s="Max-Age"),[s,c(a)].join("="))).filter(Boolean).join(";")}static fromString(e){let[n,...r]=e.split(";");if(!n)return null;let[o,u]=n.split("="),s=r.map(a=>{let[f,S]=a.split("=");if(!f||!f.trim().length)return null;let l=k(w(f)),d=t.parseValue(S,l);return d==null?null:[l,d]}).filter(Boolean);return p([["name",o],["value",t.parseValue(u)],...s])}static fromListString(e){let n=p();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 dateUtils.isValidDate(r)?r:void 0}return n==="httpOnly"||n==="secure"||n==="partitioned"?e!=="false":g(e)}};var V=class t{static get(e){return g(localStorage.getItem(e)||void 0)}static set(e,n){let r=c(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 h=class t{static get(e){return g(sessionStorage.getItem(e)||void 0)}static set(e,n){let r=c(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}};exports.Cookie=E;exports.LocalStorage=V;exports.Priority=P;exports.SameSite=L;exports.SessionStorage=h;