From 99eba675999de7a75cd0c38ff898916b5c185b97 Mon Sep 17 00:00:00 2001 From: Julien Maurel Date: Fri, 17 Nov 2017 09:09:02 +0100 Subject: [PATCH] Fix #3 Issue when native storage is not available neither js-cookies --- .gitignore | 4 +++- CHANGELOG.txt | 2 ++ bower.json | 2 +- js.storage.js | 40 ++++++++++++++++++++++++++++++---------- js.storage.min.js | 4 ++-- package.json | 2 +- 6 files changed, 39 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 40b878d..beafa5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules/ \ No newline at end of file +node_modules/ +.idea/ ++*.iml \ No newline at end of file diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 4676706..154881c 100755 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,5 @@ +1.0.3 + Fix issue when native storage is not available neither js-cookies 1.0.2 Fix isEmpty with false boolean diff --git a/bower.json b/bower.json index 9f0c84d..413bee9 100755 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-storage", - "version": "1.0.2", + "version": "1.0.3", "main": "js.storage.js", "description": "Plugin that simplify access to storages (HTML5) & cookies, add namespace storage and provide compatiblity for old browsers", "keywords": [ diff --git a/js.storage.js b/js.storage.js index c61fabd..45a1f52 100755 --- a/js.storage.js +++ b/js.storage.js @@ -9,7 +9,7 @@ * Project home: * https://github.com/julien-maurel/js-storage * - * Version: 1.0.2 + * Version: 1.0.3 */ (function (factory) { var registeredInModuleLoader = false; @@ -344,7 +344,7 @@ localStorage: _extend({}, apis.localStorage, {_ns: name}), sessionStorage: _extend({}, apis.sessionStorage, {_ns: name}) }; - if (typeof Cookies !== 'undefined') { + if (cookies_available) { if (!window.cookieStorage.getItem(name)) { window.cookieStorage.setItem(name, '{}'); } @@ -416,8 +416,9 @@ return result; } - // Check if storages are natively available on browser + // Check if storages are natively available on browser and check is js-cookie is present var storage_available = _testStorage('localStorage'); + var cookies_available = typeof Cookies !== 'undefined';* // Namespace object var storage = { @@ -426,7 +427,6 @@ _callMethod: function (f, a) { a = Array.prototype.slice.call(a); var p = [], a0 = a[0]; - if (this._ns) { p.push(this._ns); } @@ -441,6 +441,9 @@ alwaysUseJson: false, // Get items. If no parameters and storage have a namespace, return all namespace get: function () { + if (!storage_available && !cookies_available){ + return null; + } return this._callMethod(_get, arguments); }, // Set items @@ -449,6 +452,9 @@ if (l < 1 || !_isPlainObject(a0) && l < 2) { throw new Error('Minimum 2 arguments must be given or first parameter must be an object'); } + if (!storage_available && !cookies_available){ + return null; + } // If first argument is an object and storage is a namespace storage, set values individually if (_isPlainObject(a0) && this._ns) { for (var i in a0) { @@ -471,10 +477,16 @@ if (arguments.length < 1) { throw new Error('Minimum 1 argument must be given'); } + if (!storage_available && !cookies_available){ + return null; + } return this._callMethod(_remove, arguments); }, // Delete all items removeAll: function (reinit_ns) { + if (!storage_available && !cookies_available){ + return null; + } if (this._ns) { this._callMethod(_set, [{}]); return true; @@ -484,6 +496,9 @@ }, // Items empty isEmpty: function () { + if (!storage_available && !cookies_available){ + return null; + } return this._callMethod(_isEmpty, arguments); }, // Items exists @@ -491,16 +506,22 @@ if (arguments.length < 1) { throw new Error('Minimum 1 argument must be given'); } + if (!storage_available && !cookies_available){ + return null; + } return this._callMethod(_isSet, arguments); }, // Get keys of items keys: function () { + if (!storage_available && !cookies_available){ + return null; + } return this._callMethod(_keys, arguments); } }; // Use js-cookie for compatibility with old browsers and give access to cookieStorage - if (typeof Cookies !== 'undefined') { + if (cookies_available) { // sessionStorage is valid for one window/tab. To simulate that with cookie, we set a name for the window and use it for the name of the cookie if (!window.name) { window.name = Math.floor(Math.random() * 100000000); @@ -597,11 +618,6 @@ return _createNamespace(ns); }; if (storage_available) { - // About alwaysUseJson - // By default, all values are string on html storages and the plugin don't use json to store simple values (strings, int, float...) - // So by default, if you do storage.setItem('test',2), value in storage will be "2", not 2 - // If you set this property to true, all values set with the plugin will be stored as json to have typed values in any cases - // localStorage API apis.localStorage = _extend({}, storage, {_type: 'localStorage'}); // sessionStorage API @@ -625,6 +641,10 @@ apis.namespaceStorages = {}; } }; + // About alwaysUseJson + // By default, all values are string on html storages and the plugin don't use json to store simple values (strings, int, float...) + // So by default, if you do storage.setItem('test',2), value in storage will be "2", not 2 + // If you set this property to true, all values set with the plugin will be stored as json to have typed values in any cases apis.alwaysUseJsonInStorage = function (value) { storage.alwaysUseJson = value; apis.localStorage.alwaysUseJson = value; diff --git a/js.storage.min.js b/js.storage.min.js index 603e2f2..7ad7f0a 100755 --- a/js.storage.min.js +++ b/js.storage.min.js @@ -9,6 +9,6 @@ * Project home: * https://github.com/julien-maurel/js-storage * - * Version: 1.0.2 + * Version: 1.0.3 */ -!function(e){var t=!1;if("function"==typeof define&&define.amd&&(define(e),t=!0),"object"==typeof exports&&(module.exports=e(),t=!0),!t){var r=window.Storages,o=window.Storages=e();o.noConflict=function(){return window.Storages=r,o}}}(function(){function e(){var e,t,r,o,n,i=this._type,s=arguments.length,a=window[i],f=arguments,c=f[0];if(1>s)throw new Error("Minimum 1 argument must be given");if(Array.isArray(c)){t={};for(o in c)if(c.hasOwnProperty(o)){e=c[o];try{t[e]=JSON.parse(a.getItem(e))}catch(h){t[e]=a.getItem(e)}}return t}if(1!=s){try{t=JSON.parse(a.getItem(c))}catch(h){throw new ReferenceError(c+" is not defined in this storage")}for(o=1;s-1>o;o++)if(t=t[f[o]],void 0===t)throw new ReferenceError([].slice.call(f,1,o+1).join(".")+" is not defined in this storage");if(Array.isArray(f[o])){r=t,t={};for(n in f[o])f[o].hasOwnProperty(n)&&(t[f[o][n]]=r[f[o][n]]);return t}return t[f[o]]}try{return JSON.parse(a.getItem(c))}catch(h){return a.getItem(c)}}function t(){var e,t,r,o,n=this._type,i=arguments.length,s=window[n],a=arguments,f=a[0],h=a[1],l=isNaN(h)?{}:[];if(1>i||!c(f)&&2>i)throw new Error("Minimum 2 arguments must be given or first parameter must be an object");if(c(f)){for(o in f)f.hasOwnProperty(o)&&(e=f[o],c(e)||this.alwaysUseJson?s.setItem(o,JSON.stringify(e)):s.setItem(o,e));return f}if(2==i)return"object"==typeof h||this.alwaysUseJson?s.setItem(f,JSON.stringify(h)):s.setItem(f,h),h;try{r=s.getItem(f),null!=r&&(l=JSON.parse(r))}catch(u){}for(r=l,o=1;i-2>o;o++)e=a[o],t=isNaN(a[o+1])?"object":"array",(!r[e]||"object"==t&&!c(r[e])||"array"==t&&!Array.isArray(r[e]))&&("array"==t?r[e]=[]:r[e]={}),r=r[e];return r[a[o]]=a[o+1],s.setItem(f,JSON.stringify(l)),l}function r(){var e,t,r,o,n=this._type,i=arguments.length,s=window[n],a=arguments,f=a[0];if(1>i)throw new Error("Minimum 1 argument must be given");if(Array.isArray(f)){for(r in f)f.hasOwnProperty(r)&&s.removeItem(f[r]);return!0}if(1==i)return s.removeItem(f),!0;try{e=t=JSON.parse(s.getItem(f))}catch(c){throw new ReferenceError(f+" is not defined in this storage")}for(r=1;i-1>r;r++)if(t=t[a[r]],void 0===t)throw new ReferenceError([].slice.call(a,1,r).join(".")+" is not defined in this storage");if(Array.isArray(a[r]))for(o in a[r])a[r].hasOwnProperty(o)&&delete t[a[r][o]];else delete t[a[r]];return s.setItem(f,JSON.stringify(e)),!0}function o(e){var t,o=s.call(this);for(t in o)o.hasOwnProperty(t)&&r.call(this,o[t]);if(e)for(t in d.namespaceStorages)d.namespaceStorages.hasOwnProperty(t)&&a(t)}function n(){var t,r=arguments.length,o=arguments,i=o[0];if(0==r)return 0==s.call(this).length;if(Array.isArray(i)){for(t=0;tr)throw new Error("Minimum 1 argument must be given");if(Array.isArray(n)){for(t=0;t0?e.apply(this,arguments):o,i&&i._cookie){var s=Cookies.get();for(var a in s)s.hasOwnProperty(a)&&""!=a&&n.push(a.replace(i._prefix,""))}else for(var f in i)i.hasOwnProperty(f)&&n.push(f);return n}function a(e){if(!e||"string"!=typeof e)throw new Error("First parameter must be a string");v?(window.localStorage.getItem(e)||window.localStorage.setItem(e,"{}"),window.sessionStorage.getItem(e)||window.sessionStorage.setItem(e,"{}")):(window.localCookieStorage.getItem(e)||window.localCookieStorage.setItem(e,"{}"),window.sessionCookieStorage.getItem(e)||window.sessionCookieStorage.setItem(e,"{}"));var t={localStorage:l({},d.localStorage,{_ns:e}),sessionStorage:l({},d.sessionStorage,{_ns:e})};return"undefined"!=typeof Cookies&&(window.cookieStorage.getItem(e)||window.cookieStorage.setItem(e,"{}"),t.cookieStorage=l({},d.cookieStorage,{_ns:e})),d.namespaceStorages[e]=t,t}function f(e){var t="jsapi";try{return window[e]?(window[e].setItem(t,t),window[e].removeItem(t),!0):!1}catch(r){return!1}}function c(e){var t,r;return e&&"[object Object]"===g.call(e)?(t=y(e))?(r=w.call(t,"constructor")&&t.constructor,"function"==typeof r&&p.call(r)===m):!0:!1}function h(e){var t;for(t in e)return!1;return!0}function l(){for(var e=1,t=arguments[0];ee||!c(o)&&2>e)throw new Error("Minimum 2 arguments must be given or first parameter must be an object");if(c(o)&&this._ns){for(var n in o)o.hasOwnProperty(n)&&this._callMethod(t,[n,o[n]]);return o}var i=this._callMethod(t,r);return this._ns?i[o.split(".")[0]]:i},remove:function(){if(arguments.length<1)throw new Error("Minimum 1 argument must be given");return this._callMethod(r,arguments)},removeAll:function(e){return this._ns?(this._callMethod(t,[{}]),!0):this._callMethod(o,[e])},isEmpty:function(){return this._callMethod(n,arguments)},isSet:function(){if(arguments.length<1)throw new Error("Minimum 1 argument must be given");return this._callMethod(i,arguments)},keys:function(){return this._callMethod(s,arguments)}};if("undefined"!=typeof Cookies){window.name||(window.name=Math.floor(1e8*Math.random()));var O={_cookie:!0,_prefix:"",_expires:null,_path:null,_domain:null,setItem:function(e,t){Cookies.set(this._prefix+e,t,{expires:this._expires,path:this._path,domain:this._domain})},getItem:function(e){return Cookies.get(this._prefix+e)},removeItem:function(e){return Cookies.remove(this._prefix+e,{path:this._path})},clear:function(){var e=Cookies.get();for(var t in e)e.hasOwnProperty(t)&&""!=t&&(!this._prefix&&-1===t.indexOf(S)&&-1===t.indexOf(_)||this._prefix&&0===t.indexOf(this._prefix))&&Cookies.remove(t)},setExpires:function(e){return this._expires=e,this},setPath:function(e){return this._path=e,this},setDomain:function(e){return this._domain=e,this},setConf:function(e){return e.path&&(this._path=e.path),e.domain&&(this._domain=e.domain),e.expires&&(this._expires=e.expires),this},setDefaultConf:function(){this._path=this._domain=this._expires=null}};v||(window.localCookieStorage=l({},O,{_prefix:S,_expires:3650}),window.sessionCookieStorage=l({},O,{_prefix:_+window.name+"_"})),window.cookieStorage=l({},O),d.cookieStorage=l({},k,{_type:"cookieStorage",setExpires:function(e){return window.cookieStorage.setExpires(e),this},setPath:function(e){return window.cookieStorage.setPath(e),this},setDomain:function(e){return window.cookieStorage.setDomain(e),this},setConf:function(e){return window.cookieStorage.setConf(e),this},setDefaultConf:function(){return window.cookieStorage.setDefaultConf(),this}})}return d.initNamespaceStorage=function(e){return a(e)},v?(d.localStorage=l({},k,{_type:"localStorage"}),d.sessionStorage=l({},k,{_type:"sessionStorage"})):(d.localStorage=l({},k,{_type:"localCookieStorage"}),d.sessionStorage=l({},k,{_type:"sessionCookieStorage"})),d.namespaceStorages={},d.removeAllStorages=function(e){d.localStorage.removeAll(e),d.sessionStorage.removeAll(e),d.cookieStorage&&d.cookieStorage.removeAll(e),e||(d.namespaceStorages={})},d.alwaysUseJsonInStorage=function(e){k.alwaysUseJson=e,d.localStorage.alwaysUseJson=e,d.sessionStorage.alwaysUseJson=e,d.cookieStorage&&(d.cookieStorage.alwaysUseJson=e)},d}); \ No newline at end of file +!function(e){var t=!1;if("function"==typeof define&&define.amd&&(define(e),t=!0),"object"==typeof exports&&(module.exports=e(),t=!0),!t){var r=window.Storages,o=window.Storages=e();o.noConflict=function(){return window.Storages=r,o}}}(function(){function e(){var e,t,r,o,n,i=this._type,s=arguments.length,a=window[i],l=arguments,f=l[0];if(1>s)throw new Error("Minimum 1 argument must be given");if(Array.isArray(f)){t={};for(o in f)if(f.hasOwnProperty(o)){e=f[o];try{t[e]=JSON.parse(a.getItem(e))}catch(u){t[e]=a.getItem(e)}}return t}if(1!=s){try{t=JSON.parse(a.getItem(f))}catch(u){throw new ReferenceError(f+" is not defined in this storage")}for(o=1;s-1>o;o++)if(t=t[l[o]],void 0===t)throw new ReferenceError([].slice.call(l,1,o+1).join(".")+" is not defined in this storage");if(Array.isArray(l[o])){r=t,t={};for(n in l[o])l[o].hasOwnProperty(n)&&(t[l[o][n]]=r[l[o][n]]);return t}return t[l[o]]}try{return JSON.parse(a.getItem(f))}catch(u){return a.getItem(f)}}function t(){var e,t,r,o,n=this._type,i=arguments.length,s=window[n],a=arguments,l=a[0],u=a[1],c=isNaN(u)?{}:[];if(1>i||!f(l)&&2>i)throw new Error("Minimum 2 arguments must be given or first parameter must be an object");if(f(l)){for(o in l)l.hasOwnProperty(o)&&(e=l[o],f(e)||this.alwaysUseJson?s.setItem(o,JSON.stringify(e)):s.setItem(o,e));return l}if(2==i)return"object"==typeof u||this.alwaysUseJson?s.setItem(l,JSON.stringify(u)):s.setItem(l,u),u;try{r=s.getItem(l),null!=r&&(c=JSON.parse(r))}catch(h){}for(r=c,o=1;i-2>o;o++)e=a[o],t=isNaN(a[o+1])?"object":"array",(!r[e]||"object"==t&&!f(r[e])||"array"==t&&!Array.isArray(r[e]))&&("array"==t?r[e]=[]:r[e]={}),r=r[e];return r[a[o]]=a[o+1],s.setItem(l,JSON.stringify(c)),c}function r(){var e,t,r,o,n=this._type,i=arguments.length,s=window[n],a=arguments,l=a[0];if(1>i)throw new Error("Minimum 1 argument must be given");if(Array.isArray(l)){for(r in l)l.hasOwnProperty(r)&&s.removeItem(l[r]);return!0}if(1==i)return s.removeItem(l),!0;try{e=t=JSON.parse(s.getItem(l))}catch(f){throw new ReferenceError(l+" is not defined in this storage")}for(r=1;i-1>r;r++)if(t=t[a[r]],void 0===t)throw new ReferenceError([].slice.call(a,1,r).join(".")+" is not defined in this storage");if(Array.isArray(a[r]))for(o in a[r])a[r].hasOwnProperty(o)&&delete t[a[r][o]];else delete t[a[r]];return s.setItem(l,JSON.stringify(e)),!0}function o(e){var t,o=s.call(this);for(t in o)o.hasOwnProperty(t)&&r.call(this,o[t]);if(e)for(t in d.namespaceStorages)d.namespaceStorages.hasOwnProperty(t)&&a(t)}function n(){var t,r=arguments.length,o=arguments,i=o[0];if(0==r)return 0==s.call(this).length;if(Array.isArray(i)){for(t=0;tr)throw new Error("Minimum 1 argument must be given");if(Array.isArray(n)){for(t=0;t0?e.apply(this,arguments):o,i&&i._cookie){var s=Cookies.get();for(var a in s)s.hasOwnProperty(a)&&""!=a&&n.push(a.replace(i._prefix,""))}else for(var l in i)i.hasOwnProperty(l)&&n.push(l);return n}function a(e){if(!e||"string"!=typeof e)throw new Error("First parameter must be a string");v?(window.localStorage.getItem(e)||window.localStorage.setItem(e,"{}"),window.sessionStorage.getItem(e)||window.sessionStorage.setItem(e,"{}")):(window.localCookieStorage.getItem(e)||window.localCookieStorage.setItem(e,"{}"),window.sessionCookieStorage.getItem(e)||window.sessionCookieStorage.setItem(e,"{}"));var t={localStorage:c({},d.localStorage,{_ns:e}),sessionStorage:c({},d.sessionStorage,{_ns:e})};return k&&(window.cookieStorage.getItem(e)||window.cookieStorage.setItem(e,"{}"),t.cookieStorage=c({},d.cookieStorage,{_ns:e})),d.namespaceStorages[e]=t,t}function l(e){var t="jsapi";try{return window[e]?(window[e].setItem(t,t),window[e].removeItem(t),!0):!1}catch(r){return!1}}function f(e){var t,r;return e&&"[object Object]"===g.call(e)?(t=y(e))?(r=w.call(t,"constructor")&&t.constructor,"function"==typeof r&&p.call(r)===m):!0:!1}function u(e){var t;for(t in e)return!1;return!0}function c(){for(var e=1,t=arguments[0];ee||!f(o)&&2>e)throw new Error("Minimum 2 arguments must be given or first parameter must be an object");if(!v&&!k)return null;if(f(o)&&this._ns){for(var n in o)o.hasOwnProperty(n)&&this._callMethod(t,[n,o[n]]);return o}var i=this._callMethod(t,r);return this._ns?i[o.split(".")[0]]:i},remove:function(){if(arguments.length<1)throw new Error("Minimum 1 argument must be given");return v||k?this._callMethod(r,arguments):null},removeAll:function(e){return v||k?this._ns?(this._callMethod(t,[{}]),!0):this._callMethod(o,[e]):null},isEmpty:function(){return v||k?this._callMethod(n,arguments):null},isSet:function(){if(arguments.length<1)throw new Error("Minimum 1 argument must be given");return v||k?this._callMethod(i,arguments):null},keys:function(){return v||k?this._callMethod(s,arguments):null}};if(k){window.name||(window.name=Math.floor(1e8*Math.random()));var I={_cookie:!0,_prefix:"",_expires:null,_path:null,_domain:null,setItem:function(e,t){Cookies.set(this._prefix+e,t,{expires:this._expires,path:this._path,domain:this._domain})},getItem:function(e){return Cookies.get(this._prefix+e)},removeItem:function(e){return Cookies.remove(this._prefix+e,{path:this._path})},clear:function(){var e=Cookies.get();for(var t in e)e.hasOwnProperty(t)&&""!=t&&(!this._prefix&&-1===t.indexOf(S)&&-1===t.indexOf(_)||this._prefix&&0===t.indexOf(this._prefix))&&Cookies.remove(t)},setExpires:function(e){return this._expires=e,this},setPath:function(e){return this._path=e,this},setDomain:function(e){return this._domain=e,this},setConf:function(e){return e.path&&(this._path=e.path),e.domain&&(this._domain=e.domain),e.expires&&(this._expires=e.expires),this},setDefaultConf:function(){this._path=this._domain=this._expires=null}};v||(window.localCookieStorage=c({},I,{_prefix:S,_expires:3650}),window.sessionCookieStorage=c({},I,{_prefix:_+window.name+"_"})),window.cookieStorage=c({},I),d.cookieStorage=c({},O,{_type:"cookieStorage",setExpires:function(e){return window.cookieStorage.setExpires(e),this},setPath:function(e){return window.cookieStorage.setPath(e),this},setDomain:function(e){return window.cookieStorage.setDomain(e),this},setConf:function(e){return window.cookieStorage.setConf(e),this},setDefaultConf:function(){return window.cookieStorage.setDefaultConf(),this}})}return d.initNamespaceStorage=function(e){return a(e)},v?(d.localStorage=c({},O,{_type:"localStorage"}),d.sessionStorage=c({},O,{_type:"sessionStorage"})):(d.localStorage=c({},O,{_type:"localCookieStorage"}),d.sessionStorage=c({},O,{_type:"sessionCookieStorage"})),d.namespaceStorages={},d.removeAllStorages=function(e){d.localStorage.removeAll(e),d.sessionStorage.removeAll(e),d.cookieStorage&&d.cookieStorage.removeAll(e),e||(d.namespaceStorages={})},d.alwaysUseJsonInStorage=function(e){O.alwaysUseJson=e,d.localStorage.alwaysUseJson=e,d.sessionStorage.alwaysUseJson=e,d.cookieStorage&&(d.cookieStorage.alwaysUseJson=e)},d}); \ No newline at end of file diff --git a/package.json b/package.json index 2dc3059..7a13305 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-storage", - "version": "1.0.2", + "version": "1.0.3", "author": { "name": "Julien Maurel", "url": "https://github.com/julien-maurel"