vue-localstorage
Advanced tools
Comparing version 0.6.0 to 0.6.1
/** | ||
* vue-local-storage v0.5.0 | ||
* vue-local-storage v0.6.0 | ||
* (c) 2017 Alexander Avakov | ||
@@ -12,5 +12,66 @@ * @license MIT | ||
this._properties = {}; | ||
this._namespace = ''; | ||
this._isSupported = true; | ||
} | ||
/** | ||
* Namespace getter. | ||
* | ||
* @returns {string} | ||
*/ | ||
get namespace () { | ||
return this._namespace | ||
} | ||
/** | ||
* Namespace setter. | ||
* | ||
* @param {string} value | ||
*/ | ||
set namespace (value) { | ||
this._namespace = value ? `${value}.` : ''; | ||
} | ||
/** | ||
* Concatenates localStorage key with namespace prefix. | ||
* | ||
* @param {string} lsKey | ||
* @returns {string} | ||
* @private | ||
*/ | ||
_getLsKey (lsKey) { | ||
return `${this._namespace}${lsKey}` | ||
} | ||
/** | ||
* Set a value to localStorage giving respect to the namespace. | ||
* | ||
* @param {string} lsKey | ||
* @param {*} rawValue | ||
* @param {*} type | ||
* @private | ||
*/ | ||
_lsSet (lsKey, rawValue, type) { | ||
const key = this._getLsKey(lsKey); | ||
const value = type && [Array, Object].includes(type) | ||
? JSON.stringify(rawValue) | ||
: rawValue; | ||
window.localStorage.setItem(key, value); | ||
} | ||
/** | ||
* Get value from localStorage giving respect to the namespace. | ||
* | ||
* @param {string} lsKey | ||
* @returns {any} | ||
* @private | ||
*/ | ||
_lsGet (lsKey) { | ||
const key = this._getLsKey(lsKey); | ||
return window.localStorage[key] | ||
} | ||
/** | ||
* Get value from localStorage | ||
@@ -20,8 +81,13 @@ * | ||
* @param {*} defaultValue | ||
* @param {*} defaultType | ||
* @returns {*} | ||
*/ | ||
get (lsKey, defaultValue = null) { | ||
if (window.localStorage[lsKey]) { | ||
let type = String; | ||
get (lsKey, defaultValue = null, defaultType = String) { | ||
if (!this._isSupported) { | ||
return null | ||
} | ||
if (this._lsGet(lsKey)) { | ||
let type = defaultType; | ||
for (const key in this._properties) { | ||
@@ -34,3 +100,3 @@ if (key === lsKey) { | ||
return this._process(type, window.localStorage[lsKey]) | ||
return this._process(type, this._lsGet(lsKey)) | ||
} | ||
@@ -49,7 +115,11 @@ | ||
set (lsKey, value) { | ||
if (!this._isSupported) { | ||
return null | ||
} | ||
for (const key in this._properties) { | ||
const type = this._properties[key].type; | ||
if ((key === lsKey) && [Array, Object].includes(type)) { | ||
window.localStorage.setItem(lsKey, JSON.stringify(value)); | ||
if ((key === lsKey)) { | ||
this._lsSet(lsKey, value, type); | ||
@@ -60,3 +130,3 @@ return value | ||
window.localStorage.setItem(lsKey, value); | ||
this._lsSet(lsKey, value); | ||
@@ -72,2 +142,6 @@ return value | ||
remove (lsKey) { | ||
if (!this._isSupported) { | ||
return null | ||
} | ||
return window.localStorage.removeItem(lsKey) | ||
@@ -88,7 +162,4 @@ } | ||
if (!window.localStorage[key] && defaultValue !== null) { | ||
window.localStorage.setItem( | ||
key, | ||
[Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue | ||
); | ||
if (!this._lsGet(key) && defaultValue !== null) { | ||
this._lsSet(key, defaultValue, type); | ||
} | ||
@@ -110,3 +181,3 @@ } | ||
case Number: | ||
return parseInt(value, 10) | ||
return parseFloat(value) | ||
case Array: | ||
@@ -132,3 +203,3 @@ try { | ||
var VueLocalStorage$1 = new VueLocalStorage(); | ||
var vueLocalStorage = new VueLocalStorage(); | ||
@@ -153,2 +224,4 @@ var index = { | ||
let isSupported = true; | ||
try { | ||
@@ -160,2 +233,5 @@ const test = '__vue-localstorage-test__'; | ||
} catch (e) { | ||
isSupported = false; | ||
vueLocalStorage._isSupported = false; | ||
console.error('Local storage is not supported'); | ||
@@ -167,4 +243,12 @@ } | ||
if (options.namespace) { | ||
vueLocalStorage.namespace = options.namespace; | ||
} | ||
Vue.mixin({ | ||
beforeCreate () { | ||
if (!isSupported) { | ||
return | ||
} | ||
if (this.$options[name]) { | ||
@@ -175,5 +259,5 @@ Object.keys(this.$options[name]).forEach((key) => { | ||
VueLocalStorage$1.addProperty(key, type, defaultValue); | ||
vueLocalStorage.addProperty(key, type, defaultValue); | ||
const existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage$1, key); | ||
const existingProp = Object.getOwnPropertyDescriptor(vueLocalStorage, key); | ||
@@ -187,4 +271,4 @@ if (!existingProp) { | ||
Object.defineProperty(VueLocalStorage$1, key, prop); | ||
Vue.util.defineReactive(VueLocalStorage$1, key, defaultValue); | ||
Object.defineProperty(vueLocalStorage, key, prop); | ||
Vue.util.defineReactive(vueLocalStorage, key, defaultValue); | ||
} else if (!Vue.config.silent) { | ||
@@ -209,4 +293,4 @@ console.log(`${key}: is already defined and will be reused`); | ||
Vue[name] = VueLocalStorage$1; | ||
Vue.prototype[`$${name}`] = VueLocalStorage$1; | ||
Vue[name] = vueLocalStorage; | ||
Vue.prototype[`$${name}`] = vueLocalStorage; | ||
} | ||
@@ -213,0 +297,0 @@ }; |
/** | ||
* vue-local-storage v0.5.0 | ||
* vue-local-storage v0.6.0 | ||
* (c) 2017 Alexander Avakov | ||
@@ -14,5 +14,68 @@ * @license MIT | ||
this._properties = {}; | ||
this._namespace = ''; | ||
this._isSupported = true; | ||
}; | ||
var prototypeAccessors = { namespace: {} }; | ||
/** | ||
* Namespace getter. | ||
* | ||
* @returns {string} | ||
*/ | ||
prototypeAccessors.namespace.get = function () { | ||
return this._namespace | ||
}; | ||
/** | ||
* Namespace setter. | ||
* | ||
* @param {string} value | ||
*/ | ||
prototypeAccessors.namespace.set = function (value) { | ||
this._namespace = value ? (value + ".") : ''; | ||
}; | ||
/** | ||
* Concatenates localStorage key with namespace prefix. | ||
* | ||
* @param {string} lsKey | ||
* @returns {string} | ||
* @private | ||
*/ | ||
VueLocalStorage.prototype._getLsKey = function _getLsKey (lsKey) { | ||
return ("" + (this._namespace) + lsKey) | ||
}; | ||
/** | ||
* Set a value to localStorage giving respect to the namespace. | ||
* | ||
* @param {string} lsKey | ||
* @param {*} rawValue | ||
* @param {*} type | ||
* @private | ||
*/ | ||
VueLocalStorage.prototype._lsSet = function _lsSet (lsKey, rawValue, type) { | ||
var key = this._getLsKey(lsKey); | ||
var value = type && [Array, Object].includes(type) | ||
? JSON.stringify(rawValue) | ||
: rawValue; | ||
window.localStorage.setItem(key, value); | ||
}; | ||
/** | ||
* Get value from localStorage giving respect to the namespace. | ||
* | ||
* @param {string} lsKey | ||
* @returns {any} | ||
* @private | ||
*/ | ||
VueLocalStorage.prototype._lsGet = function _lsGet (lsKey) { | ||
var key = this._getLsKey(lsKey); | ||
return window.localStorage[key] | ||
}; | ||
/** | ||
* Get value from localStorage | ||
@@ -22,11 +85,17 @@ * | ||
* @param {*} defaultValue | ||
* @param {*} defaultType | ||
* @returns {*} | ||
*/ | ||
VueLocalStorage.prototype.get = function get (lsKey, defaultValue) { | ||
VueLocalStorage.prototype.get = function get (lsKey, defaultValue, defaultType) { | ||
var this$1 = this; | ||
if ( defaultValue === void 0 ) defaultValue = null; | ||
if ( defaultType === void 0 ) defaultType = String; | ||
if (window.localStorage[lsKey]) { | ||
var type = String; | ||
if (!this._isSupported) { | ||
return null | ||
} | ||
if (this._lsGet(lsKey)) { | ||
var type = defaultType; | ||
for (var key in this$1._properties) { | ||
@@ -39,3 +108,3 @@ if (key === lsKey) { | ||
return this._process(type, window.localStorage[lsKey]) | ||
return this._process(type, this._lsGet(lsKey)) | ||
} | ||
@@ -56,7 +125,11 @@ | ||
if (!this._isSupported) { | ||
return null | ||
} | ||
for (var key in this$1._properties) { | ||
var type = this$1._properties[key].type; | ||
if ((key === lsKey) && [Array, Object].includes(type)) { | ||
window.localStorage.setItem(lsKey, JSON.stringify(value)); | ||
if ((key === lsKey)) { | ||
this$1._lsSet(lsKey, value, type); | ||
@@ -67,3 +140,3 @@ return value | ||
window.localStorage.setItem(lsKey, value); | ||
this._lsSet(lsKey, value); | ||
@@ -79,2 +152,6 @@ return value | ||
VueLocalStorage.prototype.remove = function remove (lsKey) { | ||
if (!this._isSupported) { | ||
return null | ||
} | ||
return window.localStorage.removeItem(lsKey) | ||
@@ -97,7 +174,4 @@ }; | ||
if (!window.localStorage[key] && defaultValue !== null) { | ||
window.localStorage.setItem( | ||
key, | ||
[Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue | ||
); | ||
if (!this._lsGet(key) && defaultValue !== null) { | ||
this._lsSet(key, defaultValue, type); | ||
} | ||
@@ -119,3 +193,3 @@ }; | ||
case Number: | ||
return parseInt(value, 10) | ||
return parseFloat(value) | ||
case Array: | ||
@@ -140,4 +214,6 @@ try { | ||
var VueLocalStorage$1 = new VueLocalStorage(); | ||
Object.defineProperties( VueLocalStorage.prototype, prototypeAccessors ); | ||
var vueLocalStorage = new VueLocalStorage(); | ||
var index = { | ||
@@ -163,2 +239,4 @@ /** | ||
var isSupported = true; | ||
try { | ||
@@ -170,2 +248,5 @@ var test = '__vue-localstorage-test__'; | ||
} catch (e) { | ||
isSupported = false; | ||
vueLocalStorage._isSupported = false; | ||
console.error('Local storage is not supported'); | ||
@@ -177,2 +258,6 @@ } | ||
if (options.namespace) { | ||
vueLocalStorage.namespace = options.namespace; | ||
} | ||
Vue.mixin({ | ||
@@ -182,2 +267,6 @@ beforeCreate: function beforeCreate () { | ||
if (!isSupported) { | ||
return | ||
} | ||
if (this.$options[name]) { | ||
@@ -190,5 +279,5 @@ Object.keys(this.$options[name]).forEach(function (key) { | ||
VueLocalStorage$1.addProperty(key, type, defaultValue); | ||
vueLocalStorage.addProperty(key, type, defaultValue); | ||
var existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage$1, key); | ||
var existingProp = Object.getOwnPropertyDescriptor(vueLocalStorage, key); | ||
@@ -202,4 +291,4 @@ if (!existingProp) { | ||
Object.defineProperty(VueLocalStorage$1, key, prop); | ||
Vue.util.defineReactive(VueLocalStorage$1, key, defaultValue); | ||
Object.defineProperty(vueLocalStorage, key, prop); | ||
Vue.util.defineReactive(vueLocalStorage, key, defaultValue); | ||
} else if (!Vue.config.silent) { | ||
@@ -224,4 +313,4 @@ console.log((key + ": is already defined and will be reused")); | ||
Vue[name] = VueLocalStorage$1; | ||
Vue.prototype[("$" + name)] = VueLocalStorage$1; | ||
Vue[name] = vueLocalStorage; | ||
Vue.prototype[("$" + name)] = vueLocalStorage; | ||
} | ||
@@ -228,0 +317,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.VueLocalStorage=t()}(this,function(){"use strict";var e=function(){this._properties={}};e.prototype.get=function(e,t){var r=this;if(void 0===t&&(t=null),window.localStorage[e]){var o=String;for(var n in r._properties)if(n===e){o=r._properties[n].type;break}return this._process(o,window.localStorage[e])}return null!==t?t:null},e.prototype.set=function(e,t){var r=this;for(var o in r._properties){var n=r._properties[o].type;if(o===e&&[Array,Object].includes(n))return window.localStorage.setItem(e,JSON.stringify(t)),t}return window.localStorage.setItem(e,t),t},e.prototype.remove=function(e){return window.localStorage.removeItem(e)},e.prototype.addProperty=function(e,t,r){void 0===r&&(r=void 0),t=t||String,this._properties[e]={type:t},window.localStorage[e]||null===r||window.localStorage.setItem(e,[Array,Object].includes(t)?JSON.stringify(r):r)},e.prototype._process=function(e,t){switch(e){case Boolean:return"true"===t;case Number:return parseInt(t,10);case Array:try{var r=JSON.parse(t);return Array.isArray(r)?r:[]}catch(e){return[]}case Object:try{return JSON.parse(t)}catch(e){return{}}default:return t}};var t=new e;return{install:function(e,r){if(void 0===r&&(r={}),"undefined"==typeof process||!(process.server||process.SERVER_BUILD||process.env&&"server"===process.env.VUE_ENV)){try{var o="__vue-localstorage-test__";window.localStorage.setItem(o,o),window.localStorage.removeItem(o)}catch(e){console.error("Local storage is not supported")}var n=r.name||"localStorage",i=r.bind;e.mixin({beforeCreate:function(){var r=this;this.$options[n]&&Object.keys(this.$options[n]).forEach(function(o){var a=r.$options[n][o],s=[a.type,a.default],c=s[0],p=s[1];if(t.addProperty(o,c,p),Object.getOwnPropertyDescriptor(t,o))e.config.silent||console.log(o+": is already defined and will be reused");else{var l={get:function(){return e.localStorage.get(o,p)},set:function(t){return e.localStorage.set(o,t)},configurable:!0};Object.defineProperty(t,o,l),e.util.defineReactive(t,o,p)}(i||a.bind)&&!1!==a.bind&&(r.$options.computed=r.$options.computed||{},r.$options.computed[o]||(r.$options.computed[o]={get:function(){return e.localStorage[o]},set:function(t){e.localStorage[o]=t}}))})}}),e[n]=t,e.prototype["$"+n]=t}}}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.VueLocalStorage=t()}(this,function(){"use strict";var e=function(){this._properties={},this._namespace="",this._isSupported=!0},t={namespace:{}};t.namespace.get=function(){return this._namespace},t.namespace.set=function(e){this._namespace=e?e+".":""},e.prototype._getLsKey=function(e){return""+this._namespace+e},e.prototype._lsSet=function(e,t,r){var o=this._getLsKey(e),n=r&&[Array,Object].includes(r)?JSON.stringify(t):t;window.localStorage.setItem(o,n)},e.prototype._lsGet=function(e){var t=this._getLsKey(e);return window.localStorage[t]},e.prototype.get=function(e,t,r){var o=this;if(void 0===t&&(t=null),void 0===r&&(r=String),!this._isSupported)return null;if(this._lsGet(e)){var n=r;for(var s in o._properties)if(s===e){n=o._properties[s].type;break}return this._process(n,this._lsGet(e))}return null!==t?t:null},e.prototype.set=function(e,t){var r=this;if(!this._isSupported)return null;for(var o in r._properties){var n=r._properties[o].type;if(o===e)return r._lsSet(e,t,n),t}return this._lsSet(e,t),t},e.prototype.remove=function(e){return this._isSupported?window.localStorage.removeItem(e):null},e.prototype.addProperty=function(e,t,r){void 0===r&&(r=void 0),t=t||String,this._properties[e]={type:t},this._lsGet(e)||null===r||this._lsSet(e,r,t)},e.prototype._process=function(e,t){switch(e){case Boolean:return"true"===t;case Number:return parseFloat(t);case Array:try{var r=JSON.parse(t);return Array.isArray(r)?r:[]}catch(e){return[]}case Object:try{return JSON.parse(t)}catch(e){return{}}default:return t}},Object.defineProperties(e.prototype,t);var r=new e;return{install:function(e,t){if(void 0===t&&(t={}),"undefined"==typeof process||!(process.server||process.SERVER_BUILD||process.env&&"server"===process.env.VUE_ENV)){var o=!0;try{var n="__vue-localstorage-test__";window.localStorage.setItem(n,n),window.localStorage.removeItem(n)}catch(e){o=!1,r._isSupported=!1,console.error("Local storage is not supported")}var s=t.name||"localStorage",i=t.bind;t.namespace&&(r.namespace=t.namespace),e.mixin({beforeCreate:function(){var t=this;o&&this.$options[s]&&Object.keys(this.$options[s]).forEach(function(o){var n=t.$options[s][o],a=[n.type,n.default],p=a[0],c=a[1];if(r.addProperty(o,p,c),Object.getOwnPropertyDescriptor(r,o))e.config.silent||console.log(o+": is already defined and will be reused");else{var u={get:function(){return e.localStorage.get(o,c)},set:function(t){return e.localStorage.set(o,t)},configurable:!0};Object.defineProperty(r,o,u),e.util.defineReactive(r,o,c)}(i||n.bind)&&!1!==n.bind&&(t.$options.computed=t.$options.computed||{},t.$options.computed[o]||(t.$options.computed[o]={get:function(){return e.localStorage[o]},set:function(t){e.localStorage[o]=t}}))})}}),e[s]=r,e.prototype["$"+s]=r}}}}); |
{ | ||
"name": "vue-localstorage", | ||
"description": "Vue.js localStorage plugin with types support", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Alexander Avakov", |
26715
741