UNPKG

opendb-store

Version:

A lightweight utility to manage browser storage (localStorage, sessionStorage, and cookies) with advanced features. Easily configure namespaces, key trimming, and data expiry.

1 lines 3.34 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define("db",e):(t="undefined"!=typeof globalThis?globalThis:t||self,function(){var n=t.db,r=t.db=e();r.noConflict=function(){return t.db=n,r}}())}(this,(function(){const t={namespace:"app",separator:".",trimKeys:!0,expiry:!0};function e(e){t.namespace=e}function n(){return t.separator}var r={version:"1.2.0",createNamespace:function(e){t.namespace=e},getCurrentNamespace:function(){return t.namespace},switchNamespace:e,get:function(){return t},setSeparator:function(e){t.separator=e},getSeparator:n,generateKey:function(e){return`${t.namespace}${t.separator}${e}`}};function i(t){return void 0===t}function o(t){return null===t||"null"===t}var s={parse:function(t){try{return JSON.parse(t)}catch{return t}},isUndefined:i,isNull:o,units:function(t,e,n){let r="";switch(e=e.toUpperCase()){case"B":r=t.toFixed(2);break;case"KB":r=(t/1024).toFixed(2);break;case"MB":r=(t/1048576).toFixed(2);break;default:return console.warn(`Unsupported format "${e}" provided. Falling back to auto-formatting.`),function(t,e){if(0===t)return"0 B";const n=Math.floor(Math.log(t)/Math.log(1024));return(t/Math.pow(1024,n)).toFixed(2)+" "+(e?["B","KB","MB"][n]:"")}(t,n)}return n?`${r} ${e}`:r}};function u(t){return i(t)||o(t)}var a={size:function(t,e={}){const n=this.get(t);if(null===n)return 0;const r=new Blob([JSON.stringify(n)]).size;if(e.format){let t=!("unit"in e)||e.unit;return s.units(r,e.format,t)}return r},free:function(t={}){const e=(t.capacity||5242880)-this.used(),n=e>0?e:0;if(t.format){let e=!("unit"in t)||t.unit;return s.units(n,t.format,e)}return n},used:function(){let t=0;const e=n();for(let n=0;n<this.count;n++){const r=this.key(n),[,i]=r.split(`${e}`,2),o=this.get(i);t+=new Blob([JSON.stringify(r)]).size,t+=new Blob([JSON.stringify(o)]).size}return t},capacity:function(t={}){const e=t.capacity||5242880;if(t.format){let n=!("unit"in t)||t.unit;return s.units(e,t.format,n)}return e},from:function(t){t&&e(t)},get:function(t,e=null){if(u(t))return null;const n=r.generateKey(t),i=this.storage.getItem(n);if(s.isNull(i))return e;try{let n=s.parse(i);return n.expire&&Date.now()>n.expire?(this.remove(t),e):n.value}catch{return e}},set:function(t,e,n={}){if(u(t))return null;const i=r.generateKey(t),{expire:o}=n;let s={value:e,...o?{expire:Date.now()+o}:{}};this.storage.setItem(i,JSON.stringify(s))},has:function(t){return!!this.get(t)},remove:function(t){const e=this.get(t),n=r.generateKey(t);return this.storage.removeItem(n),e},clear:function(){return this.storage.clear()},key:function(t){return this.storage.key(t)},keys:function(){const t=[];for(let e=0;e<this.storage.length;e++)t.push(this.storage.key(e));return t},trim:function(t){return this.storage.get(t).trim()},getFormattedData:function(t){const e={},r=n();for(let n=0,i=this.storage.length;n<i;n++){const i=this.key(n),[,o,s]=i.split(`${r}`,3);o===t&&s&&(e[s]=this.get(`${o}${r}${s}`))}return e},setFormattedData:function(t,e){const r=n();for(let n in e)n in e&&this.set(`${t}${r}${n}`,e[n])}};function c(t){return{storage:t,get count(){return t.length},...a}}function f(t){if(!t)throw new Error("Storage is not supported in this environment.");return t}return{config:r,local:c(f(window.localStorage)),session:c(f(window.sessionStorage))}}));