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