-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.min.js
1 lines (1 loc) · 3.61 KB
/
storage.min.js
1
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).storage={})}(this,(function(e){"use strict";var t=function(){function e(e){this.storage=e}return e.prototype.get=function(e){var t,n,o=this;if(!(n=this.storage.getItem)||"object"!=typeof n&&"function"!=typeof n||"function"!=typeof n.then){var r=this.storage.getItem(String(e));try{r=JSON.parse(null!==(t=r)&&void 0!==t?t:"")}catch(e){}return r}return new Promise((function(t){o.storage.getItem(String(e)).then((function(e){try{e=JSON.parse(null!=e?e:"")}catch(e){}t(t)}))}))},e.prototype.set=function(e,t){var n;n="object"==typeof t?JSON.stringify(t):String(t),this.storage.setItem(String(e),n)},e.prototype.remove=function(e){this.storage.removeItem(String(e))},e.prototype.clear=function(){this.storage.clear()},e}(),n=function(){function e(){this.length=0}return e.prototype.clear=function(){this.length=0,document.cookie=""},e.prototype.getItem=function(e){if(this.length>0){var t=document.cookie.indexOf(e+"="),n=void 0;if(-1!==t)return t=t+String(e).length+1,-1===(n=document.cookie.indexOf(";",t))&&(n=document.cookie.length),decodeURIComponent(document.cookie.substring(t,n))}return null},e.prototype.key=function(e){throw new Error("Method not implemented.")},e.prototype.removeItem=function(e){if(this.length>0){var t=document.cookie.indexOf(e+"="),n=void 0;if(-1!==t){var o=t+String(e).length+1;if(-1===(n=document.cookie.indexOf(";",o))){var r=document.cookie.substring(t,n);document.cookie.replace(r,""),this.length--}}}},e.prototype.setItem=function(e,t){this.length++,document.cookie=e+"="+encodeURIComponent(t)+";max-age=31525459200;path=/"},e}(),o=function(){function e(){this.data={},this.length=0}return e.create=function(){return this.instance||(this.instance=new e),this.instance},e.prototype.clear=function(){this.data={}},e.prototype.getItem=function(e){return this.data[e]},e.prototype.key=function(e){throw new Error("Method not implemented.")},e.prototype.removeItem=function(e){delete this.data[e],this.length--},e.prototype.setItem=function(e,t){this.length++,this.data[e]=t},e}(),r="xmy6364_storage_table",i=function(){function e(){var e=this;this.length=0,indexedDB||console.error("您的浏览器不支持indexedDB"),this.request=indexedDB.open("xmy6364_storage_db"),this.request.onupgradeneeded=function(){console.log("[onupgradeneeded]");var t=e.request.result;t.objectStoreNames.contains(r)||t.createObjectStore(r,{keyPath:"key"}),e.db=t},this.request.onsuccess=function(){e.db=e.request.result,console.log("indexedDB 启动成功")}}return e.prototype.clear=function(){var e=this;return new Promise((function(t){e.db.transaction([r],"readwrite").objectStore(r).clear().onsuccess=function(){e.length=0,t()}}))},e.prototype.getItem=function(e){var t=this;return new Promise((function(n){var o=t.db.transaction([r],"readonly").objectStore(r).get(e);o.onsuccess=function(){n(o.result.value)}}))},e.prototype.removeItem=function(e){var t=this;return new Promise((function(n){t.db.transaction([r],"readwrite").objectStore(r).delete(e).onsuccess=function(){t.length--,n()}}))},e.prototype.setItem=function(e,t){var n=this;return new Promise((function(o){var i=n.db.transaction([r],"readwrite").objectStore(r),c=i.get(e);c.onsuccess=function(){c.result?i.put({key:e,value:t}).onsuccess=function(){o()}:i.add({key:e,value:t}).onsuccess=function(){n.length++,o()}}}))},e}(),c=new t(localStorage),s=new t(sessionStorage),u=new t(new n),a=new t(o.create()),f=new t(new i);e.cookie=u,e.indexed=f,e.local=c,e.memory=a,e.session=s,Object.defineProperty(e,"__esModule",{value:!0})}));