diff --git a/dist/store.common.js b/dist/store.common.js index f49c149..6575360 100644 --- a/dist/store.common.js +++ b/dist/store.common.js @@ -10,7 +10,59 @@ 'use strict'; -var storage = window.localStorage; +var storage = window.localStorage, + _inMemoryStorage = {}, + _length = 0; // deal QuotaExceededError if user use incognito mode in browser + +(function () { + function isSupported() { + var _KEY = '_Is_Incognit', + _VALUE = 'yes'; + + try { + storage.setItem(_KEY, _VALUE); + storage.removeItem(_KEY); + return true; + } catch (error) { + return false; + } + } + + if (!isSupported()) { + storage.__proto__ = { + setItem: function setItem(key, value) { + _inMemoryStorage[key] = '' + value; + + if (!_inMemoryStorage.hasOwnProperty(key)) { + _length++; + } + }, + getItem: function getItem(key) { + if (key in _inMemoryStorage) { + return _inMemoryStorage[key]; + } + + return null; + }, + removeItem: function removeItem(key) { + if (_inMemoryStorage.hasOwnProperty(key)) { + delete _inMemoryStorage[key]; + _length--; + } + }, + clear: function clear() { + _inMemoryStorage = {}; + _length = 0; + }, + length: function length() { + return _length; + }, + key: function key(index) { + return Object.keys(_inMemoryStorage)[index] || null; + } + }; + } +})(); function isJSON(obj) { obj = JSON.stringify(obj); @@ -44,36 +96,7 @@ function isFunction(value) { function isArray(value) { return Object.prototype.toString.call(value) === "[object Array]"; -} // https://github.com/jaywcjlove/store.js/pull/8 -// Error: QuotaExceededError - - -function dealIncognito(storage) { - var _KEY = '_Is_Incognit', - _VALUE = 'yes'; - - try { - storage.setItem(_KEY, _VALUE); - } catch (e) { - if (e.name === 'QuotaExceededError') { - var _nothing = function _nothing() {}; - - storage.__proto__ = { - setItem: _nothing, - getItem: _nothing, - removeItem: _nothing, - clear: _nothing - }; - } - } finally { - if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY); - } - - return storage; -} // deal QuotaExceededError if user use incognito mode in browser - - -storage = dealIncognito(storage); +} function Store() { if (!(this instanceof Store)) { diff --git a/dist/store.common.min.js b/dist/store.common.min.js index a51c443..38b07df 100644 --- a/dist/store.common.min.js +++ b/dist/store.common.min.js @@ -1,2 +1,2 @@ /*! storejs v1.0.24 | MIT (c) 2018 kenny wang | https://github.com/jaywcjlove/store.js */ -"use strict";var storage=window.localStorage;function isJSON(t){return t=JSON.stringify(t),/^\{[\s\S]*\}$/.test(t)}function stringify(t){return void 0===t||"function"==typeof t?t+"":JSON.stringify(t)}function deserialize(e){if("string"==typeof e)try{return JSON.parse(e)}catch(t){return e}}function isFunction(t){return"[object Function]"==={}.toString.call(t)}function isArray(t){return"[object Array]"===Object.prototype.toString.call(t)}function dealIncognito(e){var t="_Is_Incognit";try{e.setItem(t,"yes")}catch(t){if("QuotaExceededError"===t.name){var r=function(){};e.__proto__={setItem:r,getItem:r,removeItem:r,clear:r}}}finally{"yes"===e.getItem(t)&&e.removeItem(t)}return e}function Store(){if(!(this instanceof Store))return new Store}storage=dealIncognito(storage),Store.prototype={set:function(t,e){if(t&&!isJSON(t))storage.setItem(t,stringify(e));else if(isJSON(t))for(var r in t)this.set(r,t[r]);return this},get:function(t){if(t)return"?"===t.charAt(0)?this.has(t.substr(1)):deserialize(storage.getItem(t));var r={};return this.forEach(function(t,e){return r[t]=e}),r},clear:function(){return storage.clear(),this},remove:function(t){var e=this.get(t);return storage.removeItem(t),e},has:function(t){return{}.hasOwnProperty.call(this.get(),t)},keys:function(){var e=[];return this.forEach(function(t){e.push(t)}),e},forEach:function(t){for(var e=0,r=storage.length;e | https://github.com/jaywcjlove/store.js */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.store=e()}(this,function(){"use strict";var i=window.localStorage;function f(t){return t=JSON.stringify(t),/^\{[\s\S]*\}$/.test(t)}function u(t){return"[object Function]"==={}.toString.call(t)}function s(){if(!(this instanceof s))return new s}i=function(e){var t="_Is_Incognit";try{e.setItem(t,"yes")}catch(t){if("QuotaExceededError"===t.name){var r=function(){};e.__proto__={setItem:r,getItem:r,removeItem:r,clear:r}}}finally{"yes"===e.getItem(t)&&e.removeItem(t)}return e}(i),s.prototype={set:function(t,e){if(t&&!f(t))i.setItem(t,void 0===(n=e)||"function"==typeof n?n+"":JSON.stringify(n));else if(f(t))for(var r in t)this.set(r,t[r]);var n;return this},get:function(t){if(t)return"?"===t.charAt(0)?this.has(t.substr(1)):function(e){if("string"==typeof e)try{return JSON.parse(e)}catch(t){return e}}(i.getItem(t));var r={};return this.forEach(function(t,e){return r[t]=e}),r},clear:function(){return i.clear(),this},remove:function(t){var e=this.get(t);return i.removeItem(t),e},has:function(t){return{}.hasOwnProperty.call(this.get(),t)},keys:function(){var e=[];return this.forEach(function(t){e.push(t)}),e},forEach:function(t){for(var e=0,r=i.length;e