Skip to content

Commit

Permalink
Fix #3 Issue when native storage is not available neither js-cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Maurel committed Nov 17, 2017
1 parent 9fb50d5 commit 99eba67
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
node_modules/
.idea/
+*.iml
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
40 changes: 30 additions & 10 deletions js.storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, '{}');
}
Expand Down Expand Up @@ -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';*

This comment has been minimized.

Copy link
@Kobe

Kobe Nov 19, 2017

@julien-maurel please remove the * ;-)

This comment has been minimized.

Copy link
@increddibelly

increddibelly Nov 20, 2017

Contributor

fixed in #8

This comment has been minimized.

Copy link
@julien-maurel

julien-maurel Nov 21, 2017

Owner

fixed, sorry for that


// Namespace object
var storage = {
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -484,23 +496,32 @@
},
// Items empty
isEmpty: function () {
if (!storage_available && !cookies_available){
return null;
}
return this._callMethod(_isEmpty, arguments);
},
// Items exists
isSet: function () {
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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 99eba67

Please sign in to comment.