Comparing version 2.2.18 to 2.2.21
@@ -137,9 +137,11 @@ (function (global, factory) { | ||
* | ||
* @param {Object} e | ||
* @param {Object} event | ||
*/ | ||
function change(e) { | ||
if (!e) { | ||
e = window.event; | ||
} | ||
function change(event) { | ||
var e = event || window.event; | ||
var emit = function emit(listener) { | ||
listener(e.newValue ? JSON.parse(e.newValue).value : e.newValue, e.oldValue ? JSON.parse(e.oldValue).value : e.oldValue, e.url || e.uri); | ||
}; | ||
if (typeof e === 'undefined' || typeof e.key === 'undefined') { | ||
@@ -154,6 +156,2 @@ return; | ||
} | ||
function emit(listener) { | ||
listener(e.newValue ? JSON.parse(e.newValue).value : e.newValue, e.oldValue ? JSON.parse(e.oldValue).value : e.oldValue, e.url || e.uri); | ||
} | ||
} | ||
@@ -168,12 +166,11 @@ | ||
* @param {Object} storage | ||
* @param {Object} options | ||
*/ | ||
function Storage(storage, options) { | ||
function Storage(storage) { | ||
classCallCheck(this, Storage); | ||
this.storage = storage; | ||
this.options = _extends({ | ||
this.options = { | ||
namespace: '', | ||
events: ['storage'] | ||
}, options || {}); | ||
}; | ||
@@ -205,7 +202,5 @@ Object.defineProperty(this, 'length', { | ||
/** | ||
* Set item | ||
* Set Options | ||
* | ||
* @param {string} name | ||
* @param {*} value | ||
* @param {number} expire - seconds | ||
* @param {Object} options | ||
*/ | ||
@@ -215,2 +210,18 @@ | ||
createClass(Storage, [{ | ||
key: 'setOptions', | ||
value: function setOptions() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
this.options = _extends(this.options, options); | ||
} | ||
/** | ||
* Set item | ||
* | ||
* @param {string} name | ||
* @param {*} value | ||
* @param {number} expire - seconds | ||
*/ | ||
}, { | ||
key: 'set', | ||
@@ -365,7 +376,7 @@ value: function set$$1(name, value) { | ||
install: function install(Vue, options) { | ||
storageObject.options = _extends(storageObject.options, { | ||
storageObject.setOptions(_extends(storageObject.options, { | ||
namespace: '' | ||
}, options || {}); | ||
}, options || {})); | ||
Vue.ls = storageObject; | ||
Vue.ls = storageObject; // eslint-disable-line | ||
Object.defineProperty(Vue.prototype, '$ls', { | ||
@@ -372,0 +383,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e["vue-ls"]=n()}(this,function(){"use strict";function e(e){function n(n){n(e.newValue?JSON.parse(e.newValue).value:e.newValue,e.oldValue?JSON.parse(e.oldValue).value:e.oldValue,e.url||e.uri)}if(e||(e=window.event),void 0!==e&&void 0!==e.key){var t=a[e.key];void 0!==t&&t.forEach(n)}}var n={},t={getItem:function(e){return e in n?n[e]:null},setItem:function(e,t){return n[e]=t,!0},removeItem:function(e){return!!(e in n)&&delete n[e]},clear:function(){return n={},!0},key:function(e){var t=Object.keys(n);return void 0!==t[e]?t[e]:null}};Object.defineProperty(t,"length",{get:function(){return Object.keys(n).length}});var o=function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")},i=function(){function e(e,n){for(var t=0;t<n.length;t++){var o=n[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(n,t,o){return t&&e(n.prototype,t),o&&e(n,o),n}}(),r=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},a={},s=new(function(){function n(t,i){if(o(this,n),this.storage=t,this.options=r({namespace:"",events:["storage"]},i||{}),Object.defineProperty(this,"length",{get:function(){return this.storage.length}}),"undefined"!=typeof window)for(var a in this.options.events)window.addEventListener?window.addEventListener(this.options.events[a],e,!1):window.attachEvent?window.attachEvent("on"+this.options.events[a],e):window["on"+this.options.events[a]]=e}return i(n,[{key:"set",value:function(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;this.storage.setItem(this.options.namespace+e,JSON.stringify({value:n,expire:null!==t?(new Date).getTime()+t:null}))}},{key:"get",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,t=this.storage.getItem(this.options.namespace+e);if(null!==t)try{var o=JSON.parse(t);if(null===o.expire)return o.value;if(o.expire>=(new Date).getTime())return o.value;this.remove(e)}catch(e){return n}return n}},{key:"key",value:function(e){return this.storage.key(e)}},{key:"remove",value:function(e){return this.storage.removeItem(this.options.namespace+e)}},{key:"clear",value:function(){if(0!==this.length){for(var e=[],n=0;n<this.length;n++){var t=this.storage.key(n);!1!==new RegExp("^"+this.options.namespace+".+","i").test(t)&&e.push(t)}for(var o in e)this.storage.removeItem(e[o])}}},{key:"on",value:function(e,n){a[this.options.namespace+e]?a[this.options.namespace+e].push(n):a[this.options.namespace+e]=[n]}},{key:"off",value:function(e,n){var t=a[this.options.namespace+e];t.length>1?t.splice(t.indexOf(n),1):a[this.options.namespace+e]=[]}}]),n}())("undefined"!=typeof window&&"localStorage"in window?window.localStorage:t),u={install:function(e,n){s.options=r(s.options,{namespace:""},n||{}),e.ls=s,Object.defineProperty(e.prototype,"$ls",{get:function(){return s}})}};return"undefined"!=typeof window&&(window.VueLocalStorage=u),u}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e["vue-ls"]=t()}(this,function(){"use strict";function e(e){var t=e||window.event,n=function(e){e(t.newValue?JSON.parse(t.newValue).value:t.newValue,t.oldValue?JSON.parse(t.oldValue).value:t.oldValue,t.url||t.uri)};if(void 0!==t&&void 0!==t.key){var o=a[t.key];void 0!==o&&o.forEach(n)}}var t={},n={getItem:function(e){return e in t?t[e]:null},setItem:function(e,n){return t[e]=n,!0},removeItem:function(e){return!!(e in t)&&delete t[e]},clear:function(){return t={},!0},key:function(e){var n=Object.keys(t);return void 0!==n[e]?n[e]:null}};Object.defineProperty(n,"length",{get:function(){return Object.keys(t).length}});var o=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},i=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,n,o){return n&&e(t.prototype,n),o&&e(t,o),t}}(),r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},a={},s=new(function(){function t(n){if(o(this,t),this.storage=n,this.options={namespace:"",events:["storage"]},Object.defineProperty(this,"length",{get:function(){return this.storage.length}}),"undefined"!=typeof window)for(var i in this.options.events)window.addEventListener?window.addEventListener(this.options.events[i],e,!1):window.attachEvent?window.attachEvent("on"+this.options.events[i],e):window["on"+this.options.events[i]]=e}return i(t,[{key:"setOptions",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=r(this.options,e)}},{key:"set",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;this.storage.setItem(this.options.namespace+e,JSON.stringify({value:t,expire:null!==n?(new Date).getTime()+n:null}))}},{key:"get",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=this.storage.getItem(this.options.namespace+e);if(null!==n)try{var o=JSON.parse(n);if(null===o.expire)return o.value;if(o.expire>=(new Date).getTime())return o.value;this.remove(e)}catch(e){return t}return t}},{key:"key",value:function(e){return this.storage.key(e)}},{key:"remove",value:function(e){return this.storage.removeItem(this.options.namespace+e)}},{key:"clear",value:function(){if(0!==this.length){for(var e=[],t=0;t<this.length;t++){var n=this.storage.key(t);!1!==new RegExp("^"+this.options.namespace+".+","i").test(n)&&e.push(n)}for(var o in e)this.storage.removeItem(e[o])}}},{key:"on",value:function(e,t){a[this.options.namespace+e]?a[this.options.namespace+e].push(t):a[this.options.namespace+e]=[t]}},{key:"off",value:function(e,t){var n=a[this.options.namespace+e];n.length>1?n.splice(n.indexOf(t),1):a[this.options.namespace+e]=[]}}]),t}())("undefined"!=typeof window&&"localStorage"in window?window.localStorage:n),u={install:function(e,t){s.setOptions(r(s.options,{namespace:""},t||{})),e.ls=s,Object.defineProperty(e.prototype,"$ls",{get:function(){return s}})}};return"undefined"!=typeof window&&(window.VueLocalStorage=u),u}); | ||
//# sourceMappingURL=vue-ls.min.js.map |
{ | ||
"name": "vue-ls", | ||
"version": "2.2.18", | ||
"version": "2.2.21", | ||
"description": "Vue plugin for work with LocalStorage from Vue context", | ||
@@ -27,3 +27,2 @@ "main": "dist/vue-ls.js", | ||
"lint": "node_modules/.bin/eslint ./", | ||
"hint": "node_modules/.bin/jshint", | ||
"report": "npm test && node_modules/.bin/nyc report --reporter=html", | ||
@@ -64,2 +63,3 @@ "coveralls": "./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls", | ||
"babel-core": "^6.24.0", | ||
"babel-eslint": "^7.2.3", | ||
"babel-loader": "^7.0.0", | ||
@@ -76,3 +76,8 @@ "babel-plugin-external-helpers": "^6.22.0", | ||
"coveralls": "^2.11.16", | ||
"eslint": "^4.1.1", | ||
"eslint": "^4.2.0", | ||
"eslint-config-airbnb-base": "^11.2.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-node": "^5.1.1", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"get-gulp-args": "^0.0.1", | ||
@@ -88,3 +93,2 @@ "gulp": "github:gulpjs/gulp#4.0", | ||
"jasmine-core": "^2.5.2", | ||
"jshint": "^2.9.4", | ||
"karma": "^1.5.0", | ||
@@ -100,3 +104,3 @@ "karma-chrome-launcher": "^2.0.0", | ||
"remove": "^0.1.5", | ||
"rollup": "^0.43.0", | ||
"rollup": "^0.45.2", | ||
"rollup-plugin-babel": "^2.7.1", | ||
@@ -106,3 +110,3 @@ "selenium-server": "^3.3.1", | ||
"vue": "^2.2.4", | ||
"webpack": "^3.0.0" | ||
"webpack": "^3.3.0" | ||
}, | ||
@@ -109,0 +113,0 @@ "semistandard": { |
@@ -178,4 +178,5 @@ <p align="center"> | ||
## License | ||
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FRobinCK%2Fvue-ls.svg?type=large)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FRobinCK%2Fvue-ls?ref=badge_large) | ||
## License | ||
MIT © [Igor Ognichenko](https://github.com/RobinCK) |
import ls from './localStorage'; | ||
let VueLocalStorage = { | ||
const VueLocalStorage = { | ||
/** | ||
@@ -11,8 +11,8 @@ * Install plugin | ||
*/ | ||
install (Vue, options) { | ||
ls.options = Object.assign(ls.options, { | ||
namespace: '' | ||
}, options || {}); | ||
install(Vue, options) { | ||
ls.setOptions(Object.assign(ls.options, { | ||
namespace: '', | ||
}, options || {})); | ||
Vue.ls = ls; | ||
Vue.ls = ls; // eslint-disable-line | ||
Object.defineProperty(Vue.prototype, '$ls', { | ||
@@ -24,7 +24,7 @@ /** | ||
*/ | ||
get () { | ||
get() { | ||
return ls; | ||
} | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
@@ -31,0 +31,0 @@ |
import memoryStorage from './memoryStorage'; | ||
import {Storage} from './storage'; | ||
import { Storage } from './storage'; | ||
@@ -4,0 +4,0 @@ const store = typeof window !== 'undefined' && 'localStorage' in window |
@@ -10,3 +10,3 @@ let ls = {}; | ||
*/ | ||
getItem (name) { | ||
getItem(name) { | ||
return name in ls ? ls[name] : null; | ||
@@ -22,3 +22,3 @@ }, | ||
*/ | ||
setItem (name, value) { | ||
setItem(name, value) { | ||
ls[name] = value; | ||
@@ -35,4 +35,4 @@ | ||
*/ | ||
removeItem (name) { | ||
var found = name in ls; | ||
removeItem(name) { | ||
const found = name in ls; | ||
@@ -51,3 +51,3 @@ if (found) { | ||
*/ | ||
clear () { | ||
clear() { | ||
ls = {}; | ||
@@ -64,7 +64,7 @@ | ||
*/ | ||
key (index) { | ||
let keys = Object.keys(ls); | ||
key(index) { | ||
const keys = Object.keys(ls); | ||
return typeof keys[index] !== 'undefined' ? keys[index] : null; | ||
} | ||
}, | ||
}; | ||
@@ -78,7 +78,7 @@ | ||
*/ | ||
get () { | ||
get() { | ||
return Object.keys(ls).length; | ||
} | ||
}, | ||
}); | ||
export default memoryStorage; |
@@ -6,9 +6,18 @@ const eventListeners = {}; | ||
* | ||
* @param {Object} e | ||
* @param {Object} event | ||
*/ | ||
function change (e) { | ||
if (!e) { | ||
e = window.event; | ||
} | ||
function change(event) { | ||
const e = event || window.event; | ||
const emit = (listener) => { | ||
listener( | ||
e.newValue | ||
? JSON.parse(e.newValue).value | ||
: e.newValue, | ||
e.oldValue | ||
? JSON.parse(e.oldValue).value | ||
: e.oldValue | ||
, e.url || e.uri); | ||
}; | ||
if (typeof e === 'undefined' || typeof e.key === 'undefined') { | ||
@@ -18,3 +27,3 @@ return; | ||
let all = eventListeners[e.key]; | ||
const all = eventListeners[e.key]; | ||
@@ -24,6 +33,2 @@ if (typeof all !== 'undefined') { | ||
} | ||
function emit (listener) { | ||
listener(e.newValue ? JSON.parse(e.newValue).value : e.newValue, e.oldValue ? JSON.parse(e.oldValue).value : e.oldValue, e.url || e.uri); | ||
} | ||
} | ||
@@ -37,10 +42,9 @@ | ||
* @param {Object} storage | ||
* @param {Object} options | ||
*/ | ||
constructor (storage, options) { | ||
constructor(storage) { | ||
this.storage = storage; | ||
this.options = Object.assign({ | ||
this.options = { | ||
namespace: '', | ||
events: ['storage'] | ||
}, options || {}); | ||
events: ['storage'], | ||
}; | ||
@@ -53,9 +57,9 @@ Object.defineProperty(this, 'length', { | ||
*/ | ||
get () { | ||
get() { | ||
return this.storage.length; | ||
} | ||
}, | ||
}); | ||
if (typeof window !== 'undefined') { | ||
for (let i in this.options.events) { | ||
for (const i in this.options.events) { | ||
if (window.addEventListener) { | ||
@@ -73,2 +77,11 @@ window.addEventListener(this.options.events[i], change, false); | ||
/** | ||
* Set Options | ||
* | ||
* @param {Object} options | ||
*/ | ||
setOptions(options = {}) { | ||
this.options = Object.assign(this.options, options); | ||
} | ||
/** | ||
* Set item | ||
@@ -80,6 +93,6 @@ * | ||
*/ | ||
set (name, value, expire = null) { | ||
set(name, value, expire = null) { | ||
this.storage.setItem( | ||
this.options.namespace + name, | ||
JSON.stringify({value: value, expire: expire !== null ? new Date().getTime() + expire : null}) | ||
JSON.stringify({ value, expire: expire !== null ? new Date().getTime() + expire : null }), | ||
); | ||
@@ -95,8 +108,8 @@ } | ||
*/ | ||
get (name, def = null) { | ||
let item = this.storage.getItem(this.options.namespace + name); | ||
get(name, def = null) { | ||
const item = this.storage.getItem(this.options.namespace + name); | ||
if (item !== null) { | ||
try { | ||
let data = JSON.parse(item); | ||
const data = JSON.parse(item); | ||
@@ -126,3 +139,3 @@ if (data.expire === null) { | ||
*/ | ||
key (index) { | ||
key(index) { | ||
return this.storage.key(index); | ||
@@ -137,3 +150,3 @@ } | ||
*/ | ||
remove (name) { | ||
remove(name) { | ||
return this.storage.removeItem(this.options.namespace + name); | ||
@@ -145,3 +158,3 @@ } | ||
*/ | ||
clear () { | ||
clear() { | ||
if (this.length === 0) { | ||
@@ -151,7 +164,7 @@ return; | ||
let removedKeys = []; | ||
const removedKeys = []; | ||
for (let i = 0; i < this.length; i++) { | ||
let key = this.storage.key(i); | ||
let regexp = new RegExp(`^${this.options.namespace}.+`, 'i'); | ||
const key = this.storage.key(i); | ||
const regexp = new RegExp(`^${this.options.namespace}.+`, 'i'); | ||
@@ -165,3 +178,3 @@ if (regexp.test(key) === false) { | ||
for (let key in removedKeys) { | ||
for (const key in removedKeys) { | ||
this.storage.removeItem(removedKeys[key]); | ||
@@ -177,3 +190,3 @@ } | ||
*/ | ||
on (name, callback) { | ||
on(name, callback) { | ||
if (eventListeners[this.options.namespace + name]) { | ||
@@ -192,4 +205,4 @@ eventListeners[this.options.namespace + name].push(callback); | ||
*/ | ||
off (name, callback) { | ||
let ns = eventListeners[this.options.namespace + name]; | ||
off(name, callback) { | ||
const ns = eventListeners[this.options.namespace + name]; | ||
@@ -207,3 +220,2 @@ if (ns.length > 1) { | ||
change, | ||
eventListeners | ||
}; |
30293
614
182
46