vue2-storage
Advanced tools
Comparing version 1.0.0 to 2.0.0
/*! | ||
* vue2-storage v1.0.0 | ||
* vue2-storage v2.0.0 | ||
* (c) 2018 Yarkov Aleksey | ||
@@ -10,23 +10,19 @@ * Released under the MIT License. | ||
/* */ | ||
var Storage = function Storage (config) { | ||
if ( config === void 0 ) config = {}; | ||
this.version = '1.0.0'; | ||
this.name = "vue2-storage[v" + (this.version) + "]"; | ||
var options = Object.assign({ | ||
storage: 'local', | ||
ttl: 60 * 60 * 24 * 1000 // 24 hours | ||
}, config); | ||
this.options = Object.freeze(options); | ||
switch (this.options.storage) { | ||
this.version = '2.0.0'; | ||
this.name = 'vue2-storage[v2.0.0]'; | ||
this.setOptions(config); | ||
switch (this.options.driver) { | ||
case 'local': | ||
this.storage = window.localStorage; | ||
break | ||
this.driver = window.localStorage; | ||
break | ||
case 'session': | ||
this.storage = window.sessionStorage; | ||
this.driver = window.sessionStorage; | ||
break | ||
default: | ||
this.storage = window.localStorage; | ||
this.driver = window.localStorage; | ||
break | ||
@@ -36,2 +32,17 @@ } | ||
Storage.prototype.setOptions = function setOptions (config) { | ||
if ( config === void 0 ) config = {}; | ||
var options = Object.assign({ | ||
prefix: '', | ||
driver: 'local', | ||
ttl: 60 * 60 * 24 * 1000 // 24 hours | ||
}, config); | ||
this.options = Object.freeze(options); | ||
}; | ||
Storage.prototype.keyPrefix = function keyPrefix (key) { | ||
return ("" + (this.options.prefix || '') + key) | ||
}; | ||
Storage.prototype.toJSON = function toJSON (data, options) { | ||
@@ -48,11 +59,17 @@ if ( options === void 0 ) options = {}; | ||
Storage.prototype.fromJSON = function fromJSON (key) { | ||
try { | ||
var data = JSON.parse(this.storage.getItem(key)); | ||
if (data && data.ttl >= Date.now()) { | ||
return data.value | ||
} | ||
this.remove(key); | ||
return null | ||
} catch (e) { | ||
return null | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
try { | ||
var data = JSON.parse(this.driver.getItem(key)); | ||
if (data && data.ttl >= Date.now()) { | ||
return data.value | ||
} | ||
this.remove(key); | ||
return null | ||
} catch (e) { | ||
return null | ||
} | ||
default: | ||
return null | ||
} | ||
@@ -67,3 +84,3 @@ }; | ||
try { | ||
return this.fromJSON(key) | ||
return this.fromJSON(this.keyPrefix(key)) | ||
} catch (e) { | ||
@@ -78,3 +95,3 @@ this.printError(e); | ||
try { | ||
this.storage.setItem(key, this.toJSON(val, options)); | ||
this.driver.setItem(this.keyPrefix(key), this.toJSON(val, options)); | ||
} catch (e) { | ||
@@ -86,6 +103,12 @@ this.printError(e); | ||
Storage.prototype.remove = function remove (key) { | ||
try { | ||
this.storage.removeItem(key); | ||
} catch (e) { | ||
this.printError(e); | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
default: | ||
try { | ||
this.driver.removeItem(this.keyPrefix(key)); | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
break | ||
} | ||
@@ -95,9 +118,39 @@ }; | ||
Storage.prototype.clear = function clear () { | ||
try { | ||
this.storage.clear(); | ||
} catch (e) { | ||
this.printError(e); | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
default: | ||
try { | ||
this.driver.clear(); | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
break | ||
} | ||
}; | ||
Storage.prototype.has = function has (key) { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
return (this.keyPrefix(key) in this.driver) | ||
default: | ||
return false | ||
} | ||
}; | ||
Storage.prototype.keys = function keys () { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
return Object.keys(this.driver) | ||
default: | ||
return [] | ||
} | ||
}; | ||
Storage.prototype.length = function length () { | ||
return this.keys().length | ||
}; | ||
/* */ | ||
@@ -104,0 +157,0 @@ |
/*! | ||
* vue2-storage v1.0.0 | ||
* vue2-storage v2.0.0 | ||
* (c) 2018 Yarkov Aleksey | ||
@@ -14,23 +14,19 @@ * Released under the MIT License. | ||
/* */ | ||
var Storage = function Storage (config) { | ||
if ( config === void 0 ) config = {}; | ||
this.version = '1.0.0'; | ||
this.name = "vue2-storage[v" + (this.version) + "]"; | ||
var options = Object.assign({ | ||
storage: 'local', | ||
ttl: 60 * 60 * 24 * 1000 // 24 hours | ||
}, config); | ||
this.options = Object.freeze(options); | ||
switch (this.options.storage) { | ||
this.version = '2.0.0'; | ||
this.name = 'vue2-storage[v2.0.0]'; | ||
this.setOptions(config); | ||
switch (this.options.driver) { | ||
case 'local': | ||
this.storage = window.localStorage; | ||
break | ||
this.driver = window.localStorage; | ||
break | ||
case 'session': | ||
this.storage = window.sessionStorage; | ||
this.driver = window.sessionStorage; | ||
break | ||
default: | ||
this.storage = window.localStorage; | ||
this.driver = window.localStorage; | ||
break | ||
@@ -40,2 +36,17 @@ } | ||
Storage.prototype.setOptions = function setOptions (config) { | ||
if ( config === void 0 ) config = {}; | ||
var options = Object.assign({ | ||
prefix: '', | ||
driver: 'local', | ||
ttl: 60 * 60 * 24 * 1000 // 24 hours | ||
}, config); | ||
this.options = Object.freeze(options); | ||
}; | ||
Storage.prototype.keyPrefix = function keyPrefix (key) { | ||
return ("" + (this.options.prefix || '') + key) | ||
}; | ||
Storage.prototype.toJSON = function toJSON (data, options) { | ||
@@ -52,11 +63,17 @@ if ( options === void 0 ) options = {}; | ||
Storage.prototype.fromJSON = function fromJSON (key) { | ||
try { | ||
var data = JSON.parse(this.storage.getItem(key)); | ||
if (data && data.ttl >= Date.now()) { | ||
return data.value | ||
} | ||
this.remove(key); | ||
return null | ||
} catch (e) { | ||
return null | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
try { | ||
var data = JSON.parse(this.driver.getItem(key)); | ||
if (data && data.ttl >= Date.now()) { | ||
return data.value | ||
} | ||
this.remove(key); | ||
return null | ||
} catch (e) { | ||
return null | ||
} | ||
default: | ||
return null | ||
} | ||
@@ -71,3 +88,3 @@ }; | ||
try { | ||
return this.fromJSON(key) | ||
return this.fromJSON(this.keyPrefix(key)) | ||
} catch (e) { | ||
@@ -82,3 +99,3 @@ this.printError(e); | ||
try { | ||
this.storage.setItem(key, this.toJSON(val, options)); | ||
this.driver.setItem(this.keyPrefix(key), this.toJSON(val, options)); | ||
} catch (e) { | ||
@@ -90,6 +107,12 @@ this.printError(e); | ||
Storage.prototype.remove = function remove (key) { | ||
try { | ||
this.storage.removeItem(key); | ||
} catch (e) { | ||
this.printError(e); | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
default: | ||
try { | ||
this.driver.removeItem(this.keyPrefix(key)); | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
break | ||
} | ||
@@ -99,9 +122,39 @@ }; | ||
Storage.prototype.clear = function clear () { | ||
try { | ||
this.storage.clear(); | ||
} catch (e) { | ||
this.printError(e); | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
default: | ||
try { | ||
this.driver.clear(); | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
break | ||
} | ||
}; | ||
Storage.prototype.has = function has (key) { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
return (this.keyPrefix(key) in this.driver) | ||
default: | ||
return false | ||
} | ||
}; | ||
Storage.prototype.keys = function keys () { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
return Object.keys(this.driver) | ||
default: | ||
return [] | ||
} | ||
}; | ||
Storage.prototype.length = function length () { | ||
return this.keys().length | ||
}; | ||
/* */ | ||
@@ -108,0 +161,0 @@ |
/*! | ||
* vue2-storage v1.0.0 | ||
* vue2-storage v2.0.0 | ||
* (c) 2018 Yarkov Aleksey | ||
* Released under the MIT License. | ||
*/ | ||
!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):t.Vue2Storage=o()}(this,function(){"use strict";function t(t,e){t.prototype.$storage=new o(e||{})}var o=function(t){void 0===t&&(t={}),this.version="1.0.0",this.name="vue2-storage[v"+this.version+"]";var o=Object.assign({storage:"local",ttl:864e5},t);switch(this.options=Object.freeze(o),this.options.storage){case"local":this.storage=window.localStorage;break;case"session":this.storage=window.sessionStorage;break;default:this.storage=window.localStorage}};return o.prototype.toJSON=function(t,o){void 0===o&&(o={});var e="ttl"in o?o.ttl:this.options.ttl;return JSON.stringify({value:t,ttl:Date.now()+e})},o.prototype.fromJSON=function(t){try{var o=JSON.parse(this.storage.getItem(t));return o&&o.ttl>=Date.now()?o.value:(this.remove(t),null)}catch(t){return null}},o.prototype.printError=function(t){console.error(this.name+": "+t.stack)},o.prototype.get=function(t){try{return this.fromJSON(t)}catch(t){this.printError(t)}},o.prototype.set=function(t,o,e){void 0===e&&(e={});try{this.storage.setItem(t,this.toJSON(o,e))}catch(t){this.printError(t)}},o.prototype.remove=function(t){try{this.storage.removeItem(t)}catch(t){this.printError(t)}},o.prototype.clear=function(){try{this.storage.clear()}catch(t){this.printError(t)}},window.Vue2Storage=t,t}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.Vue2Storage=e()}(this,function(){"use strict";function t(t,r){t.prototype.$storage=new e(r||{})}var e=function(t){switch(void 0===t&&(t={}),this.version="2.0.0",this.name="vue2-storage[v2.0.0]",this.setOptions(t),this.options.driver){case"local":this.driver=window.localStorage;break;case"session":this.driver=window.sessionStorage;break;default:this.driver=window.localStorage}};return e.prototype.setOptions=function(t){void 0===t&&(t={});var e=Object.assign({prefix:"",driver:"local",ttl:864e5},t);this.options=Object.freeze(e)},e.prototype.keyPrefix=function(t){return""+(this.options.prefix||"")+t},e.prototype.toJSON=function(t,e){void 0===e&&(e={});var r="ttl"in e?e.ttl:this.options.ttl;return JSON.stringify({value:t,ttl:Date.now()+r})},e.prototype.fromJSON=function(t){switch(this.options.driver){case"local":case"session":try{var e=JSON.parse(this.driver.getItem(t));return e&&e.ttl>=Date.now()?e.value:(this.remove(t),null)}catch(t){return null}default:return null}},e.prototype.printError=function(t){console.error(this.name+": "+t.stack)},e.prototype.get=function(t){try{return this.fromJSON(this.keyPrefix(t))}catch(t){this.printError(t)}},e.prototype.set=function(t,e,r){void 0===r&&(r={});try{this.driver.setItem(this.keyPrefix(t),this.toJSON(e,r))}catch(t){this.printError(t)}},e.prototype.remove=function(t){switch(this.options.driver){case"local":case"session":default:try{this.driver.removeItem(this.keyPrefix(t))}catch(t){this.printError(t)}}},e.prototype.clear=function(){switch(this.options.driver){case"local":case"session":default:try{this.driver.clear()}catch(t){this.printError(t)}}},e.prototype.has=function(t){switch(this.options.driver){case"local":case"session":return this.keyPrefix(t)in this.driver;default:return!1}},e.prototype.keys=function(){switch(this.options.driver){case"local":case"session":return Object.keys(this.driver);default:return[]}},e.prototype.length=function(){return this.keys().length},window.Vue2Storage=t,t}); |
{ | ||
"name": "vue2-storage", | ||
"description": "Browser storage for Vue.js app", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"author": { | ||
@@ -18,3 +18,2 @@ "name": "Yarkov Aleksey", | ||
"build": "node config/build.js", | ||
"changelog": "conventional-changelog -i CHANGELOG.md -s -p eslint", | ||
"clean": "rm -rf coverage && rm -rf dist/*.js* && rm -f ./*.log", | ||
@@ -32,2 +31,22 @@ "dev": "cross-env BABEL_ENV=test webpack-dev-server --inline --hot --open --content-base ./test/unit/ --config config/webpack.dev.conf.js", | ||
}, | ||
"files": [ | ||
"dist/vue2-storage.js", | ||
"dist/vue2-storage.min.js", | ||
"dist/vue2-storage.common.js", | ||
"src" | ||
], | ||
"homepage": "https://github.com/yarkovaleksei/vue2-storage#readme", | ||
"main": "dist/vue2-storage.common.js", | ||
"module": "dist/vue2-storage.esm.js", | ||
"unpkg": "dist/vue2-storage.js", | ||
"keywords": [ | ||
"plugin", | ||
"vue", | ||
"vuejs", | ||
"storage", | ||
"localStorage", | ||
"sessionStorage", | ||
"cookie" | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
@@ -37,4 +56,6 @@ "babel-core": "^6.22.1", | ||
"babel-loader": "^6.2.10", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-plugin-istanbul": "^3.1.2", | ||
"babel-polyfill": "6.22.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-es2015": "^6.22.0", | ||
@@ -45,3 +66,2 @@ "babel-preset-flow-vue": "^1.0.0", | ||
"chromedriver": "^2.27.2", | ||
"conventional-changelog-cli": "^1.2.0", | ||
"cross-env": "^5.0.5", | ||
@@ -72,2 +92,3 @@ "cross-spawn": "^5.0.1", | ||
"rollup": "^0.36.4", | ||
"rollup-plugin-babel": "^3.0.3", | ||
"rollup-plugin-buble": "^0.14.0", | ||
@@ -83,22 +104,2 @@ "rollup-plugin-flow-no-whitespace": "^1.0.0", | ||
}, | ||
"files": [ | ||
"dist/vue2-storage.js", | ||
"dist/vue2-storage.min.js", | ||
"dist/vue2-storage.common.js", | ||
"src" | ||
], | ||
"homepage": "https://github.com/yarkovaleksei/vue2-storage#readme", | ||
"main": "dist/vue2-storage.common.js", | ||
"module": "dist/vue2-storage.esm.js", | ||
"unpkg": "dist/vue2-storage.js", | ||
"keywords": [ | ||
"plugin", | ||
"vue", | ||
"vuejs", | ||
"storage", | ||
"localStorage", | ||
"sessionStorage", | ||
"cookie" | ||
], | ||
"license": "MIT", | ||
"engines": { | ||
@@ -105,0 +106,0 @@ "node": ">= 6.0" |
@@ -6,3 +6,8 @@ # vue2-storage | ||
[![npm](https://img.shields.io/npm/v/vue2-storage.svg)](https://www.npmjs.com/package/vue2-storage) | ||
[![Travis](https://img.shields.io/travis/yarkovaleksei/vue2-storage.svg)](https://travis-ci.org/yarkovaleksei/vue2-storage) | ||
![GitHub release](https://img.shields.io/github/release/yarkovaleksei/vue2-storage.svg) | ||
[![npm](https://img.shields.io/npm/dw/vue2-storage.svg)](https://www.npmjs.com/package/vue2-storage) | ||
[![npm](https://img.shields.io/npm/dm/vue2-storage.svg)](https://www.npmjs.com/package/vue2-storage) | ||
[![npm](https://img.shields.io/npm/dy/vue2-storage.svg)](https://www.npmjs.com/package/vue2-storage) | ||
![node](https://img.shields.io/node/v/vue2-storage.svg) | ||
![npm type definitions](https://img.shields.io/npm/types/vue2-storage.svg) | ||
[![vue2](https://img.shields.io/badge/vue-2.x-brightgreen.svg)](https://vuejs.org/) | ||
@@ -16,6 +21,3 @@ | ||
## :scroll: Changelog | ||
Details changes for each release are documented in the [CHANGELOG.md](https://github.com/yarkovaleksei/vue2-storage/blob/dev/CHANGELOG.md). | ||
## :exclamation: Issues | ||
@@ -22,0 +24,0 @@ Please make sure to read the [Issue Reporting Checklist](https://github.com/yarkovaleksei/vue2-storage/blob/dev/CONTRIBUTING.md#issue-reporting-guidelines) before opening an issue. Issues not conforming to the guidelines may be closed immediately. |
/* @flow */ | ||
import { StorageInterface } from './interface' | ||
import type { StorageOptions, SetterOptions, StorageInterface } from './types' | ||
export type StorageOptions = { | ||
storage?: string, | ||
ttl?: number | ||
} | ||
class Storage implements StorageInterface { | ||
name: string | ||
version: string | ||
options: StorageOptions | ||
storage: any | ||
name: string; | ||
version: string; | ||
options: StorageOptions; | ||
driver: any; | ||
constructor (config: StorageOptions = {}) { | ||
this.version = '__VERSION__' | ||
this.name = `__NAME__[v${this.version}]` | ||
const options = Object.assign({ | ||
storage: 'local', | ||
ttl: 60 * 60 * 24 * 1000 // 24 hours | ||
}, config) | ||
this.options = Object.freeze(options) | ||
switch (this.options.storage) { | ||
this.name = '__NAME__[v__VERSION__]' | ||
this.setOptions(config) | ||
switch (this.options.driver) { | ||
case 'local': | ||
this.storage = window.localStorage | ||
this.driver = window.localStorage | ||
break | ||
case 'session': | ||
this.storage = window.sessionStorage | ||
this.driver = window.sessionStorage | ||
break | ||
default: | ||
this.storage = window.localStorage | ||
this.driver = window.localStorage | ||
break | ||
@@ -37,3 +28,16 @@ } | ||
toJSON (data: any, options: StorageOptions = {}) { | ||
setOptions (config: StorageOptions = {}): void { | ||
const options = Object.assign({ | ||
prefix: '', | ||
driver: 'local', | ||
ttl: 60 * 60 * 24 * 1000 // 24 hours | ||
}, config) | ||
this.options = Object.freeze(options) | ||
} | ||
keyPrefix (key: string): string { | ||
return `${this.options.prefix || ''}${key}` | ||
} | ||
toJSON (data: any, options: SetterOptions = {}): string { | ||
const ttl = ('ttl' in options) ? options.ttl : this.options.ttl | ||
@@ -46,16 +50,22 @@ return JSON.stringify({ | ||
fromJSON (key: string) { | ||
try { | ||
const data: Object = JSON.parse(this.storage.getItem(key)) | ||
if (data && data.ttl >= Date.now()) { | ||
return data.value | ||
} | ||
this.remove(key) | ||
return null | ||
} catch (e) { | ||
return null | ||
fromJSON (key: string): any { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
try { | ||
const data: Object = JSON.parse(this.driver.getItem(key)) | ||
if (data && data.ttl >= Date.now()) { | ||
return data.value | ||
} | ||
this.remove(key) | ||
return null | ||
} catch (e) { | ||
return null | ||
} | ||
default: | ||
return null | ||
} | ||
} | ||
printError (e: Error) { | ||
printError (e: Error): void { | ||
console.error(`${this.name}: ${e.stack}`) | ||
@@ -66,3 +76,3 @@ } | ||
try { | ||
return this.fromJSON(key) | ||
return this.fromJSON(this.keyPrefix(key)) | ||
} catch (e) { | ||
@@ -73,5 +83,5 @@ this.printError(e) | ||
set (key: string, val: any, options: StorageOptions = {}) { | ||
set (key: string, val: any, options: SetterOptions = {}): void { | ||
try { | ||
this.storage.setItem(key, this.toJSON(val, options)) | ||
this.driver.setItem(this.keyPrefix(key), this.toJSON(val, options)) | ||
} catch (e) { | ||
@@ -82,19 +92,55 @@ this.printError(e) | ||
remove (key: string) { | ||
try { | ||
this.storage.removeItem(key) | ||
} catch (e) { | ||
this.printError(e) | ||
remove (key: string): void { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
default: | ||
try { | ||
this.driver.removeItem(this.keyPrefix(key)) | ||
} catch (e) { | ||
this.printError(e) | ||
} | ||
break | ||
} | ||
} | ||
clear () { | ||
try { | ||
this.storage.clear() | ||
} catch (e) { | ||
this.printError(e) | ||
clear (): void { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
default: | ||
try { | ||
this.driver.clear() | ||
} catch (e) { | ||
this.printError(e) | ||
} | ||
break | ||
} | ||
} | ||
has (key: string): boolean { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
return (this.keyPrefix(key) in this.driver) | ||
default: | ||
return false | ||
} | ||
} | ||
keys (): Array<string> { | ||
switch (this.options.driver) { | ||
case 'local': | ||
case 'session': | ||
return Object.keys(this.driver) | ||
default: | ||
return [] | ||
} | ||
} | ||
length (): number { | ||
return this.keys().length | ||
} | ||
} | ||
export default Storage |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
19617
436
31
47
10
1