vue-localstorage
Advanced tools
Comparing version 0.5.0 to 0.6.0
{ | ||
"name": "vue-localstorage", | ||
"description": "Vue.js localStorage plugin with types support", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"author": { | ||
@@ -31,3 +31,3 @@ "name": "Alexander Avakov", | ||
"build:minify": "rollup -c build/rollup.minify.js", | ||
"build": "npm run build:es; npm run build:main; npm run build:minify", | ||
"build": "npm run build:es && npm run build:main && npm run build:minify", | ||
"coverage": "jest --coverage", | ||
@@ -34,0 +34,0 @@ "test": "jest" |
@@ -21,3 +21,3 @@ # VueLocalStorage | ||
import VueLocalStorage from 'vue-localstorage' | ||
Vue.use(VueLocalStorage) | ||
@@ -27,3 +27,3 @@ // Or you can specify any other name and use it via this.$ls, this.$whatEverYouWant | ||
name: 'ls', | ||
createComputed: true //created computed members from your variable declarations | ||
bind: true //created computed members from your variable declarations | ||
}) | ||
@@ -38,2 +38,5 @@ | ||
// Default type if value isn't registered in localStorage section | ||
Vue.localStorage.get('property', null, Number) | ||
//register localStorage variables and adds computed variables to local components | ||
@@ -40,0 +43,0 @@ //to be used like regular computeds that are stored in the localstorage |
@@ -1,2 +0,2 @@ | ||
import VueLocalStorage from './VueLocalStorage' | ||
import vueLocalStorage from './VueLocalStorage' | ||
@@ -21,2 +21,4 @@ export default { | ||
let isSupported = true | ||
try { | ||
@@ -28,2 +30,5 @@ const test = '__vue-localstorage-test__' | ||
} catch (e) { | ||
isSupported = false | ||
vueLocalStorage._isSupported = false | ||
console.error('Local storage is not supported') | ||
@@ -35,4 +40,12 @@ } | ||
if (options.namespace) { | ||
vueLocalStorage.namespace = options.namespace | ||
} | ||
Vue.mixin({ | ||
beforeCreate () { | ||
if (!isSupported) { | ||
return | ||
} | ||
if (this.$options[name]) { | ||
@@ -43,5 +56,5 @@ Object.keys(this.$options[name]).forEach((key) => { | ||
VueLocalStorage.addProperty(key, type, defaultValue) | ||
vueLocalStorage.addProperty(key, type, defaultValue) | ||
const existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage, key) | ||
const existingProp = Object.getOwnPropertyDescriptor(vueLocalStorage, key) | ||
@@ -55,4 +68,4 @@ if (!existingProp) { | ||
Object.defineProperty(VueLocalStorage, key, prop) | ||
Vue.util.defineReactive(VueLocalStorage, key, defaultValue) | ||
Object.defineProperty(vueLocalStorage, key, prop) | ||
Vue.util.defineReactive(vueLocalStorage, key, defaultValue) | ||
} else if (!Vue.config.silent) { | ||
@@ -77,5 +90,5 @@ console.log(`${key}: is already defined and will be reused`) | ||
Vue[name] = VueLocalStorage | ||
Vue.prototype[`$${name}`] = VueLocalStorage | ||
Vue[name] = vueLocalStorage | ||
Vue.prototype[`$${name}`] = vueLocalStorage | ||
} | ||
} |
@@ -7,5 +7,66 @@ class VueLocalStorage { | ||
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 | ||
@@ -15,8 +76,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) { | ||
@@ -29,3 +95,3 @@ if (key === lsKey) { | ||
return this._process(type, window.localStorage[lsKey]) | ||
return this._process(type, this._lsGet(lsKey)) | ||
} | ||
@@ -44,7 +110,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) | ||
@@ -55,3 +125,3 @@ return value | ||
window.localStorage.setItem(lsKey, value) | ||
this._lsSet(lsKey, value) | ||
@@ -67,2 +137,6 @@ return value | ||
remove (lsKey) { | ||
if (!this._isSupported) { | ||
return null | ||
} | ||
return window.localStorage.removeItem(lsKey) | ||
@@ -83,7 +157,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) | ||
} | ||
@@ -105,3 +176,3 @@ } | ||
case Number: | ||
return parseInt(value, 10) | ||
return parseFloat(value) | ||
case Array: | ||
@@ -108,0 +179,0 @@ try { |
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
23102
596
75