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.54 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:"2.0.1",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 o(t){return void 0===t}function i(t){return null===t||"null"===t}var s={parse:function(t){try{return JSON.parse(t)}catch{return t}},isUndefined:o,isNull:i,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 o(t)||i(t)}const a=5242880;var c={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||"unit"in e){const t=e.format||"MB",n=!("unit"in e)||e.unit;return s.units(r,t,n)}return r},free:function(t={}){const e=(t.capacity||a)-this.used(),n=e>0?e:0;if(t.format||"unit"in t){const e=t.format||"MB",r=!("unit"in t)||t.unit;return s.units(n,e,r)}return n},used:function(){let t=0;const e=n();for(let n=0;n<this.count;n++){const r=this.key(n),[,o]=r.split(`${e}`,2),i=this.get(o);t+=new Blob([JSON.stringify(r)]).size,t+=new Blob([JSON.stringify(i)]).size}return t},capacity:function(t={}){const e=t.capacity||a;if(t.format||"unit"in t){const n=t.format||"MB",r=!("unit"in t)||t.unit;return s.units(e,n,r)}return e},from:function(t){t&&e(t)},get:function(t,e=null){if(u(t))return null;const n=r.generateKey(t),o=this.storage.getItem(n);if(s.isNull(o))return e;try{let n=s.parse(o);return n.expire&&Date.now()>n.expire?(this.remove(t),e):n.value}catch(n){return console.warn(`Failed to parse stored value for key "${t}":`,n),e}},set:function(t,e,n={}){if(u(t))return null;const o=r.generateKey(t),{expire:i}=n;let s={value:e,...i?{expire:Date.now()+i}:{}};this.storage.setItem(o,JSON.stringify(s))},has:function(t){const e=this.get(t);return null!==e&&(""!==e&&("undefined"!==e&&void 0!==e))},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,o=this.storage.length;n<o;n++){const o=this.key(n),[,i,s]=o.split(`${r}`,3);i===t&&s&&(e[s]=this.get(`${i}${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 f(t){return{storage:t,get count(){return t.length},...c}}function l(t){if(!t)throw new Error("Storage is not supported in this environment.");return t}return{config:r,local:f(l(window.localStorage)),session:f(l(window.sessionStorage))}});