storages-proxy
Version:
A simple, lightweight JavaScript API for handling browser storages
4 lines (3 loc) • 4.23 kB
JavaScript
/*! storages-proxy v2.1.0 | MIT */
var Storages=function(t){"use strict";var e=function(){return e=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},e.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var n="object"==typeof global&&global&&global.Object===Object&&global,r="object"==typeof self&&self&&self.Object===Object&&self,o=(n||r||Function("return this")()).Symbol,u=Object.prototype,i=u.hasOwnProperty,c=u.toString,l=o?o.toStringTag:void 0;var a=Object.prototype.toString;var f="[object Null]",_="[object Undefined]",d=o?o.toStringTag:void 0;function s(t){return null==t?void 0===t?_:f:d&&d in Object(t)?function(t){var e=i.call(t,l),n=t[l];try{t[l]=void 0;var r=!0}catch(t){}var o=c.call(t);return r&&(e?t[l]=n:delete t[l]),o}(t):function(t){return a.call(t)}(t)}var v="[object Symbol]";var p=Array.isArray,w=1/0,S=o?o.prototype:void 0,g=S?S.toString:void 0;function E(t){if("string"==typeof t)return t;if(p(t))return function(t,e){for(var n=-1,r=null==t?0:t.length,o=Array(r);++n<r;)o[n]=e(t[n],n,t);return o}(t,E)+"";if(function(t){return"symbol"==typeof t||function(t){return null!=t&&"object"==typeof t}(t)&&s(t)==v}(t))return g?g.call(t):"";var e=t+"";return"0"==e&&1/t==-w?"-0":e}var y=/[\\^$.*+?()[\]{}|]/g,b=RegExp(y.source);function O(t){var e;return(t=null==(e=t)?"":E(e))&&b.test(t)?t.replace(y,"\\$&"):t}function T(t,e){return null!=e?"function"==typeof e?e():e:"function"==typeof window.__STORAGES_DEFAULT__[t].path?window.__STORAGES_DEFAULT__[t].path():window.__STORAGES_DEFAULT__[t].path}function h(t,e){return"path=".concat(T(t,e),";")}function A(t,e){return window.__STORAGES_DEFAULT__[e].include?window.__STORAGES_DEFAULT__[e].include.includes(t):!window.__STORAGES_DEFAULT__[e].exclude||!window.__STORAGES_DEFAULT__[e].exclude.includes(t)}function m(t,e,n){return"symbol"==typeof t?t:A(t,e)?"".concat(h(e,n)).concat(t,"="):t}function j(t){return new Proxy(window[t],{set:function(e,n,r){return Reflect.set(e,m(n,t),r)},get:function(e,n){if("setItem"===n)return function(n,r,o){return null===o?e.setItem(n,r):e.setItem(m(n,t,o),r)};if("getItem"===n)return function(n,r){return null===r?e.getItem(n):e.getItem(m(n,t,r))};if("removeItem"===n)return function(n,r){return null===r?e.removeItem(n):e.removeItem(m(n,t,r))};if("key"===n)return function(n,r){if(null===r)return e.key(n);for(var o=new RegExp("^".concat(O(h(t,r)),"(.+)=$")),u=0,i=0,c=Object.keys(e);i<c.length;i++){var l=c[i];if(A(l,t)){var a=l.match(o);if(null==a?void 0:a[1]){if(n===u)return a[1];++u}}else{if(n===u)return l;++u}}return null};if("clear"===n)return function(n){if(null===n)return e.clear();var r=new RegExp("^".concat(O(h(t,n)),".+=$"));Object.keys(e).forEach((function(n){A(n,t)&&!r.test(n)||e.removeItem(n)}))};if("length"===n){var r=new RegExp("^".concat(O(h(t)),".+=$"));return Object.keys(e).filter((function(e){return!A(e,t)||r.test(e)})).length}return Reflect.get(e,m(n,t))},deleteProperty:function(e,n){return Reflect.deleteProperty(e,m(n,t))}})}var R=function(){var t;return"/".concat(null!==(t=window.location.pathname.split("/")[1])&&void 0!==t?t:"")};function I(){return{path:R()}}function G(t,n,r){var o;if(void 0===n&&(n=I()),void 0===r&&(r=!1),(null==n?void 0:n.include)&&(null==n?void 0:n.exclude))throw new Error("Cannot configure INCLUDE and EXCLUDE at the same time.");r?window.__STORAGES_DEFAULT__[t]=e(e({},n),{path:T(t,null!==(o=null==n?void 0:n.path)&&void 0!==o?o:R())}):Object.entries(n).forEach((function(e){var n=e[0],r=e[1];window.__STORAGES_DEFAULT__[t][n]="path"===n?T(t,r):r}))}return window.__STORAGES_DEFAULT__={localStorage:I(),sessionStorage:I()},window.__STORAGES_INIT__={localStorage:!1,sessionStorage:!1},t.createGlobalStorage=function(t,e){G(t,e,!0),window.__STORAGES_INIT__[t]||(Object.defineProperty(window,t,{configurable:!0,enumerable:!0,value:j(t)}),window.__STORAGES_INIT__[t]=!0)},t.length=function(t,e){var n=Object.keys(window[t]);if(null===e)return n.length;var r=new RegExp("^".concat(O(h(t,e)),".+=$"));return n.filter((function(e){return!A(e,t)||r.test(e)})).length},t.updateGlobalStorage=G,t}({});
//# sourceMappingURL=index.min.js.map