Comparing version 1.0.10 to 1.1.0
{ | ||
"name": "vue-ls", | ||
"version": "1.0.10", | ||
"version": "1.1.0", | ||
"description": "vue.js localStorage plugin", | ||
@@ -5,0 +5,0 @@ "main": "dist/vue-ls.js", |
@@ -49,6 +49,9 @@ (function (global, factory) { | ||
var VueLocalStorage = function () { | ||
function VueLocalStorage() { | ||
function VueLocalStorage(options) { | ||
_classCallCheck(this, VueLocalStorage); | ||
this.storage = window.localStorage; | ||
this.options = _extends({ | ||
namespace: '' | ||
}, options || {}); | ||
@@ -73,8 +76,6 @@ if (window.addEventListener) { | ||
value: function install(Vue, options) { | ||
this.options = _extends({ | ||
namespace: '' | ||
}, options || {}); | ||
this.options = _extends(this.options, options || {}); | ||
var _this = this; | ||
Vue.localStorage = _this; | ||
Vue.ls = _this; | ||
Object.defineProperty(Vue.prototype, '$localStorage', { | ||
@@ -85,2 +86,7 @@ get: function get() { | ||
}); | ||
Object.defineProperty(Vue.prototype, '$ls', { | ||
get: function get() { | ||
return _this; | ||
} | ||
}); | ||
} | ||
@@ -87,0 +93,0 @@ }, { |
@@ -1,2 +0,2 @@ | ||
!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,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e){function t(t){var n=JSON.parse(e.newValue),o=JSON.parse(e.oldValue),i="object"===("undefined"==typeof n?"undefined":r(n))?n.value:o,a=o&&"object"===("undefined"==typeof o?"undefined":r(o))?o.value:o;t(i,a,e.url||e.uri)}e||(e=window.event);var n=i[e.key];n&&n.forEach(t)}var n=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},o=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="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i={};try{var a=window.localStorage,u="__storage_test__";a.setItem(u,u),a.removeItem(u)}catch(e){throw new Error("Local storage not supported by this browser")}var s=function(){function r(){e(this,r),this.storage=window.localStorage,window.addEventListener?window.addEventListener("storage",t,!1):window.attachEvent?window.attachEvent("onstorage",t):window.onstorage=t,Object.defineProperty(this,"length",{get:function(){return this.storage.length}})}return o(r,[{key:"install",value:function(e,t){this.options=n({namespace:""},t||{});var o=this;e.localStorage=o,Object.defineProperty(e.prototype,"$localStorage",{get:function(){return o}})}},{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){var o=JSON.parse(n);if(null===o.expire)return o.value;if(o.expire>=(new Date).getTime())return o.value;this.remove(e)}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=0;e<this.length;e++){var t=this.storage.key(e),n=new RegExp("^"+this.options.namespace+".+","i");n.test(t)!==!1&&this.storage.removeItem(t)}}},{key:"on",value:function(e,t){i[e]?i[e].push(t):i[e]=[t]}},{key:"off",value:function(e,t){var n=i[e];n.length>1?n.splice(n.indexOf(t),1):i[e]=[]}}]),r}();return"undefined"!=typeof window&&"undefined"!=typeof window.Vue&&window.Vue.use(new s),s}); | ||
!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,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e){function t(t){var n=JSON.parse(e.newValue),o=JSON.parse(e.oldValue),i="object"===("undefined"==typeof n?"undefined":r(n))?n.value:o,a=o&&"object"===("undefined"==typeof o?"undefined":r(o))?o.value:o;t(i,a,e.url||e.uri)}e||(e=window.event);var n=i[e.key];n&&n.forEach(t)}var n=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},o=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="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i={};try{var a=window.localStorage,u="__storage_test__";a.setItem(u,u),a.removeItem(u)}catch(e){throw new Error("Local storage not supported by this browser")}var s=function(){function r(o){e(this,r),this.storage=window.localStorage,this.options=n({namespace:""},o||{}),window.addEventListener?window.addEventListener("storage",t,!1):window.attachEvent?window.attachEvent("onstorage",t):window.onstorage=t,Object.defineProperty(this,"length",{get:function(){return this.storage.length}})}return o(r,[{key:"install",value:function(e,t){this.options=n(this.options,t||{});var o=this;e.localStorage=o,e.ls=o,Object.defineProperty(e.prototype,"$localStorage",{get:function(){return o}}),Object.defineProperty(e.prototype,"$ls",{get:function(){return o}})}},{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){var o=JSON.parse(n);if(null===o.expire)return o.value;if(o.expire>=(new Date).getTime())return o.value;this.remove(e)}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=0;e<this.length;e++){var t=this.storage.key(e),n=new RegExp("^"+this.options.namespace+".+","i");n.test(t)!==!1&&this.storage.removeItem(t)}}},{key:"on",value:function(e,t){i[e]?i[e].push(t):i[e]=[t]}},{key:"off",value:function(e,t){var n=i[e];n.length>1?n.splice(n.indexOf(t),1):i[e]=[]}}]),r}();return"undefined"!=typeof window&&"undefined"!=typeof window.Vue&&window.Vue.use(new s),s}); | ||
//# sourceMappingURL=vue-ls.min.js.map |
{ | ||
"name": "vue-ls", | ||
"version": "1.0.10", | ||
"version": "1.1.0", | ||
"description": "vue.js localStorage plugin", | ||
"main": "dist/vue-ls.js", | ||
"scripts": { | ||
"build": "node_modules/.bin/gulp" | ||
"build": "node_modules/.bin/gulp", | ||
"test": "node_modules/.bin/ava" | ||
}, | ||
@@ -33,2 +34,3 @@ "dependencies": {}, | ||
"devDependencies": { | ||
"ava": "^0.18.2", | ||
"babel-plugin-transform-object-assign": "^6.22.0", | ||
@@ -41,3 +43,5 @@ "babel-preset-es2015": "^6.22.0", | ||
"gulp-uglify": "^2.0.1", | ||
"rollup-plugin-babel": "^2.7.1" | ||
"mock-browser": "^0.92.12", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"vue": "^2.1.10" | ||
}, | ||
@@ -50,3 +54,11 @@ "semistandard": { | ||
] | ||
}, | ||
"ava": { | ||
"files": [ | ||
"./test/*.js" | ||
], | ||
"require": [ | ||
"./test/mock/browser.js" | ||
] | ||
} | ||
} |
@@ -19,10 +19,11 @@ # vue-ls | ||
## Install | ||
Using `npm` | ||
``` bash | ||
npm install vue-ls --save | ||
``` | ||
or | ||
Using `yarn` | ||
``` bash | ||
yarn install vue-ls | ||
``` | ||
or | ||
Using `bower` | ||
``` bash | ||
@@ -61,7 +62,7 @@ bower install vue-ls --save | ||
mounted: function() { | ||
this.$localStorage.set('foo', 'boo'); | ||
Vue.ls.set('foo', 'boo'); | ||
//Set expire for item | ||
this.$localStorage.set('foo', 'boo', 60 * 60 * 1000); //expiry 1 hour | ||
this.$localStorage.get('foo'); | ||
this.$localStorage.get('boo', 10); //if not set boo returned default 10 | ||
Vue.ls.set('foo', 'boo', 60 * 60 * 1000); //expiry 1 hour | ||
Vue.ls.get('foo'); | ||
Vue.ls.get('boo', 10); //if not set boo returned default 10 | ||
@@ -72,6 +73,6 @@ let callback = (val, oldVal, uri) => { | ||
this.$localStorage.on('foo', callback) //watch change foo key and triggered callback | ||
this.$localStorage.off('foo', callback) //unwatch | ||
Vue.ls.on('foo', callback) //watch change foo key and triggered callback | ||
Vue.ls.off('foo', callback) //unwatch | ||
this.$localStorage.remove('foo'); | ||
Vue.ls.remove('foo'); | ||
} | ||
@@ -81,5 +82,14 @@ }); | ||
#### Global | ||
- `Vue.ls` | ||
- `Vue.localStorage` #alias | ||
#### Context | ||
- `this.$ls` | ||
- `this.$localStorage` #alias | ||
## API | ||
#### `this.$localStorage.get(name, def)` | ||
#### `Vue.ls.get(name, def)` | ||
@@ -90,3 +100,3 @@ Returns value under `name` in local storage. Internally parses the value from JSON before returning it. | ||
#### `this.$localStorage.set(name, value, expire)` | ||
#### `Vue.ls.set(name, value, expire)` | ||
@@ -97,11 +107,11 @@ Persists `value` under `name` in local storage. Internally converts the `value` to JSON. | ||
#### `this.$localStorage.remove(name)` | ||
#### `Vue.ls.remove(name)` | ||
Removes `name` from local storage. Returns `true` if the property was successfully deleted, and `false` otherwise. | ||
#### `this.$localStorage.clear()` | ||
#### `Vue.ls.clear()` | ||
Clears local storage. | ||
#### `this.$localStorage.on(name, callback)` | ||
#### `Vue.ls.on(name, callback)` | ||
@@ -114,5 +124,5 @@ Listen for changes persisted against `name` on other tabs. Triggers `callback` when a change occurs, passing the following arguments. | ||
#### `this.$localStorage.off(name, callback)` | ||
#### `Vue.ls.off(name, callback)` | ||
Removes a listener previously attached with `this.$localStorage.on(name, callback)`. | ||
Removes a listener previously attached with `Vue.ls.on(name, callback)`. | ||
@@ -119,0 +129,0 @@ ## Note |
@@ -35,4 +35,7 @@ let listeners = {}; | ||
export default class VueLocalStorage { | ||
constructor () { | ||
constructor (options) { | ||
this.storage = window.localStorage; | ||
this.options = Object.assign({ | ||
namespace: '' | ||
}, options || {}); | ||
@@ -55,8 +58,6 @@ if (window.addEventListener) { | ||
install (Vue, options) { | ||
this.options = Object.assign({ | ||
namespace: '' | ||
}, options || {}); | ||
this.options = Object.assign(this.options, options || {}); | ||
let _this = this; | ||
Vue.localStorage = _this; | ||
Vue.ls = _this; | ||
Object.defineProperty(Vue.prototype, '$localStorage', { | ||
@@ -67,2 +68,7 @@ get () { | ||
}); | ||
Object.defineProperty(Vue.prototype, '$ls', { | ||
get () { | ||
return _this; | ||
} | ||
}); | ||
} | ||
@@ -69,0 +75,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
167642
18
400
130
11