vue-localstorage
Advanced tools
Comparing version 0.4.2 to 0.5.0
/** | ||
* vue-local-storage v0.4.2 | ||
* vue-local-storage v0.5.0 | ||
* (c) 2017 Alexander Avakov | ||
@@ -77,3 +77,3 @@ * @license MIT | ||
*/ | ||
addProperty (key, type, defaultValue) { | ||
addProperty (key, type, defaultValue = undefined) { | ||
type = type || String; | ||
@@ -84,3 +84,6 @@ | ||
if (!window.localStorage[key] && defaultValue !== null) { | ||
window.localStorage.setItem(key, [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue); | ||
window.localStorage.setItem( | ||
key, | ||
[Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue | ||
); | ||
} | ||
@@ -153,10 +156,38 @@ } | ||
const name = options.name || 'localStorage'; | ||
const bind = options.bind; | ||
Vue.mixin({ | ||
created () { | ||
beforeCreate () { | ||
if (this.$options[name]) { | ||
Object.keys(this.$options[name]).forEach((key) => { | ||
const [type, defaultValue] = [this.$options[name][key].type, this.$options[name][key].default]; | ||
const config = this.$options[name][key]; | ||
const [type, defaultValue] = [config.type, config.default]; | ||
VueLocalStorage$1.addProperty(key, type, defaultValue); | ||
const existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage$1, key); | ||
if (!existingProp) { | ||
const prop = { | ||
get: () => Vue.localStorage.get(key, defaultValue), | ||
set: val => Vue.localStorage.set(key, val), | ||
configurable: true | ||
}; | ||
Object.defineProperty(VueLocalStorage$1, key, prop); | ||
Vue.util.defineReactive(VueLocalStorage$1, key, defaultValue); | ||
} else if (!Vue.config.silent) { | ||
console.log(`${key}: is already defined and will be reused`); | ||
} | ||
if ((bind || config.bind) && config.bind !== false) { | ||
this.$options.computed = this.$options.computed || {}; | ||
if (!this.$options.computed[key]) { | ||
this.$options.computed[key] = { | ||
get: () => Vue.localStorage[key], | ||
set: (val) => { Vue.localStorage[key] = val; } | ||
}; | ||
} | ||
} | ||
}); | ||
@@ -163,0 +194,0 @@ } |
/** | ||
* vue-local-storage v0.4.2 | ||
* vue-local-storage v0.5.0 | ||
* (c) 2017 Alexander Avakov | ||
@@ -85,2 +85,4 @@ * @license MIT | ||
VueLocalStorage.prototype.addProperty = function addProperty (key, type, defaultValue) { | ||
if ( defaultValue === void 0 ) defaultValue = undefined; | ||
type = type || String; | ||
@@ -91,3 +93,6 @@ | ||
if (!window.localStorage[key] && defaultValue !== null) { | ||
window.localStorage.setItem(key, [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue); | ||
window.localStorage.setItem( | ||
key, | ||
[Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue | ||
); | ||
} | ||
@@ -161,5 +166,6 @@ }; | ||
var name = options.name || 'localStorage'; | ||
var bind = options.bind; | ||
Vue.mixin({ | ||
created: function created () { | ||
beforeCreate: function beforeCreate () { | ||
var this$1 = this; | ||
@@ -169,3 +175,4 @@ | ||
Object.keys(this.$options[name]).forEach(function (key) { | ||
var ref = [this$1.$options[name][key].type, this$1.$options[name][key].default]; | ||
var config = this$1.$options[name][key]; | ||
var ref = [config.type, config.default]; | ||
var type = ref[0]; | ||
@@ -175,2 +182,28 @@ var defaultValue = ref[1]; | ||
VueLocalStorage$1.addProperty(key, type, defaultValue); | ||
var existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage$1, key); | ||
if (!existingProp) { | ||
var prop = { | ||
get: function () { return Vue.localStorage.get(key, defaultValue); }, | ||
set: function (val) { return Vue.localStorage.set(key, val); }, | ||
configurable: true | ||
}; | ||
Object.defineProperty(VueLocalStorage$1, key, prop); | ||
Vue.util.defineReactive(VueLocalStorage$1, key, defaultValue); | ||
} else if (!Vue.config.silent) { | ||
console.log((key + ": is already defined and will be reused")); | ||
} | ||
if ((bind || config.bind) && config.bind !== false) { | ||
this$1.$options.computed = this$1.$options.computed || {}; | ||
if (!this$1.$options.computed[key]) { | ||
this$1.$options.computed[key] = { | ||
get: function () { return Vue.localStorage[key]; }, | ||
set: function (val) { Vue.localStorage[key] = val; } | ||
}; | ||
} | ||
} | ||
}); | ||
@@ -177,0 +210,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.VueLocalStorage=r()}(this,function(){"use strict";var e=function(){this._properties={}};e.prototype.get=function(e,r){var t=this;if(void 0===r&&(r=null),window.localStorage[e]){var o=String;for(var n in t._properties)if(n===e){o=t._properties[n].type;break}return this._process(o,window.localStorage[e])}return null!==r?r:null},e.prototype.set=function(e,r){var t=this;for(var o in t._properties){var n=t._properties[o].type;if(o===e&&[Array,Object].includes(n))return window.localStorage.setItem(e,JSON.stringify(r)),r}return window.localStorage.setItem(e,r),r},e.prototype.remove=function(e){return window.localStorage.removeItem(e)},e.prototype.addProperty=function(e,r,t){r=r||String,this._properties[e]={type:r},window.localStorage[e]||null===t||window.localStorage.setItem(e,[Array,Object].includes(r)?JSON.stringify(t):t)},e.prototype._process=function(e,r){switch(e){case Boolean:return"true"===r;case Number:return parseInt(r,10);case Array:try{var t=JSON.parse(r);return Array.isArray(t)?t:[]}catch(e){return[]}case Object:try{return JSON.parse(r)}catch(e){return{}}default:return r}};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)){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=t.name||"localStorage";e.mixin({created:function(){var e=this;this.$options[n]&&Object.keys(this.$options[n]).forEach(function(t){var o=[e.$options[n][t].type,e.$options[n][t].default],i=o[0],a=o[1];r.addProperty(t,i,a)})}}),e[n]=r,e.prototype["$"+n]=r}}}}); | ||
!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}}}}); |
{ | ||
"name": "vue-localstorage", | ||
"description": "Vue.js localStorage plugin with types support", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Alexander Avakov", |
@@ -25,3 +25,4 @@ # VueLocalStorage | ||
Vue.use(VueLocalStorage, { | ||
name: 'ls' | ||
name: 'ls', | ||
createComputed: true //created computed members from your variable declarations | ||
}) | ||
@@ -36,2 +37,4 @@ | ||
//register localStorage variables and adds computed variables to local components | ||
//to be used like regular computeds that are stored in the localstorage | ||
var vm = new Vue({ | ||
@@ -38,0 +41,0 @@ localStorage: { |
@@ -31,10 +31,38 @@ import VueLocalStorage from './VueLocalStorage' | ||
const name = options.name || 'localStorage' | ||
const bind = options.bind | ||
Vue.mixin({ | ||
created () { | ||
beforeCreate () { | ||
if (this.$options[name]) { | ||
Object.keys(this.$options[name]).forEach((key) => { | ||
const [type, defaultValue] = [this.$options[name][key].type, this.$options[name][key].default] | ||
const config = this.$options[name][key] | ||
const [type, defaultValue] = [config.type, config.default] | ||
VueLocalStorage.addProperty(key, type, defaultValue) | ||
const existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage, key) | ||
if (!existingProp) { | ||
const prop = { | ||
get: () => Vue.localStorage.get(key, defaultValue), | ||
set: val => Vue.localStorage.set(key, val), | ||
configurable: true | ||
} | ||
Object.defineProperty(VueLocalStorage, key, prop) | ||
Vue.util.defineReactive(VueLocalStorage, key, defaultValue) | ||
} else if (!Vue.config.silent) { | ||
console.log(`${key}: is already defined and will be reused`) | ||
} | ||
if ((bind || config.bind) && config.bind !== false) { | ||
this.$options.computed = this.$options.computed || {} | ||
if (!this.$options.computed[key]) { | ||
this.$options.computed[key] = { | ||
get: () => Vue.localStorage[key], | ||
set: (val) => { Vue.localStorage[key] = val } | ||
} | ||
} | ||
} | ||
}) | ||
@@ -41,0 +69,0 @@ } |
@@ -72,3 +72,3 @@ class VueLocalStorage { | ||
*/ | ||
addProperty (key, type, defaultValue) { | ||
addProperty (key, type, defaultValue = undefined) { | ||
type = type || String | ||
@@ -79,3 +79,6 @@ | ||
if (!window.localStorage[key] && defaultValue !== null) { | ||
window.localStorage.setItem(key, [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue) | ||
window.localStorage.setItem( | ||
key, | ||
[Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue | ||
) | ||
} | ||
@@ -82,0 +85,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21566
526
72