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 • 4.07 kB
JavaScript
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="2.0.1";var config={version:"2.0.1",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 n=Math.floor(Math.log(t)/Math.log(1024));return(t/Math.pow(1024,n)).toFixed(2)+" "+(e?["B","KB","MB"][n]:"")}function units(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.`),auto(t,n)}return n?`${r} ${e}`:r}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 n=config.generateKey(t),r=this.storage.getItem(n);if(utils.isNull(r))return e;try{let n=utils.parse(r);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}}function set(t,e,n={}){if(isInvalidArg(t))return null;const r=config.generateKey(t),{expire:i}=n;let o={value:e,...i?{expire:Date.now()+i}:{}};this.storage.setItem(r,JSON.stringify(o))}function has(t){const e=this.get(t);return null!==e&&(""!==e&&("undefined"!==e&&void 0!==e))}function remove(t){const e=this.get(t),n=config.generateKey(t);return this.storage.removeItem(n),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 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 utils.units(r,t,n)}return r}function setFormattedData(t,e){const n=getSeparator();for(let r in e)r in e&&this.set(`${t}${n}${r}`,e[r])}function getFormattedData(t){const e={},n=getSeparator();for(let r=0,i=this.storage.length;r<i;r++){const i=this.key(r),[,o,s]=i.split(`${n}`,3);o===t&&s&&(e[s]=this.get(`${o}${n}${s}`))}return e}const DEFAULT_CAPACITY=5242880;function free(t={}){const e=(t.capacity||5242880)-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 utils.units(n,e,r)}return n}function used(){let t=0;const e=getSeparator();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}function capacity(t={}){const e=t.capacity||5242880;if(t.format||"unit"in t){const n=t.format||"MB",r=!("unit"in t)||t.unit;return utils.units(e,n,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};