Update js.storage.js (#1126)

1.0.2 --> 1.0.4
https://github.com/julien-maurel/js-storage/blob/1.0.4/CHANGELOG.txt
This commit is contained in:
DavidLiedke authored and Jimmy Conner committed 2017-12-03 08:56:02 -05:00
1 parent 14be99a33b
commit 7f67710631
1 file changed
+30 -10
+30 -10
View File
@@ -9,7 +9,7 @@
* Project home:
* https://github.com/julien-maurel/js-storage
*
* Version: 1.0.2
* Version: 1.0.4
*/
(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;