vue2-storage
Advanced tools
Comparing version 3.4.0 to 4.0.0
/*! | ||
* vue2-storage v3.3.0 | ||
* (c) 2018 Yarkov Aleksey | ||
* vue2-storage v4.0.0 | ||
* (c) 2019 Yarkov Aleksey | ||
* Released under the MIT License. | ||
@@ -12,216 +12,189 @@ */ | ||
/* */ | ||
var storage = {}; | ||
var MemoryStorage = function MemoryStorage () {}; | ||
var prototypeAccessors$1 = { length: {},storage: {} }; | ||
prototypeAccessors$1.length.get = function () { | ||
return Object.keys(this.storage).length | ||
}; | ||
prototypeAccessors$1.storage.get = function () { | ||
return storage | ||
}; | ||
MemoryStorage.prototype.getItem = function getItem (key) { | ||
return (key in this.storage) ? this.storage[key] : null | ||
}; | ||
MemoryStorage.prototype.setItem = function setItem (key, value) { | ||
this.storage[key] = value; | ||
}; | ||
MemoryStorage.prototype.removeItem = function removeItem (key) { | ||
var found = (key in this.storage); | ||
if (found) { | ||
delete this.storage[key]; | ||
} | ||
}; | ||
MemoryStorage.prototype.clear = function clear () { | ||
var this$1 = this; | ||
for (var k in this.storage) { | ||
delete this$1.storage[k]; | ||
} | ||
}; | ||
Object.defineProperties( MemoryStorage.prototype, prototypeAccessors$1 ); | ||
const storage = {}; | ||
class MemoryStorage { | ||
get length() { | ||
return Object.keys(this.storage).length; | ||
} | ||
get storage() { | ||
return storage; | ||
} | ||
getItem(key) { | ||
return (key in this.storage) ? this.storage[key] : null; | ||
} | ||
setItem(key, value) { | ||
this.storage[key] = value; | ||
} | ||
removeItem(key) { | ||
if (key in this.storage) { | ||
delete this.storage[key]; | ||
} | ||
} | ||
clear() { | ||
const keys = Object.keys(this.storage); | ||
for (let i = 0; i <= keys.length; i++) { | ||
try { | ||
delete this.storage[keys[i]]; | ||
} | ||
catch (_a) { | ||
// pass | ||
} | ||
} | ||
} | ||
} | ||
var MemoryStorage$1 = new MemoryStorage(); | ||
/* */ | ||
var StorageDriver; | ||
(function (StorageDriver) { | ||
StorageDriver["LOCAL"] = "local"; | ||
StorageDriver["SESSION"] = "session"; | ||
StorageDriver["MEMORY"] = "memory"; | ||
})(StorageDriver || (StorageDriver = {})); | ||
var Storage = function Storage (config) { | ||
if ( config === void 0 ) config = {}; | ||
this.setOptions(config); | ||
}; | ||
var prototypeAccessors = { name: {},version: {},length: {},driver: {} }; | ||
prototypeAccessors.name.get = function () { | ||
return 'vue2-storage[v3.3.0]' | ||
}; | ||
prototypeAccessors.version.get = function () { | ||
return '3.3.0' | ||
}; | ||
prototypeAccessors.length.get = function () { | ||
return this.keys().length | ||
}; | ||
prototypeAccessors.driver.get = function () { | ||
switch (this.options.driver) { | ||
case 'local': | ||
default: | ||
return (typeof window !== 'undefined') ? window.localStorage : MemoryStorage$1 | ||
case 'session': | ||
return (typeof window !== 'undefined') ? window.sessionStorage : MemoryStorage$1 | ||
case 'memory': | ||
return MemoryStorage$1 | ||
} | ||
}; | ||
Storage.prototype.setOptions = function setOptions (config) { | ||
if ( config === void 0 ) config = {}; | ||
var options = objectAssign({ | ||
prefix: 'app_', | ||
driver: 'local', | ||
ttl: 60 * 60 * 24 * 1000 // 24 hours | ||
}, config); | ||
this.options = Object.freeze(options); | ||
}; | ||
Storage.prototype.printError = function printError (e) { | ||
console.error(((this.name) + ": " + (e.stack))); | ||
}; | ||
Storage.prototype.addPrefix = function addPrefix (key) { | ||
return ("" + (this.options.prefix || '') + key) | ||
}; | ||
Storage.prototype.removePrefix = function removePrefix (key) { | ||
var re = new RegExp(("^" + (this.options.prefix || ''))); | ||
return key.replace(re, '') | ||
}; | ||
Storage.prototype.toJSON = function toJSON (data, options) { | ||
if ( options === void 0 ) options = {}; | ||
var ttl = ('ttl' in options) ? options.ttl : this.options.ttl; | ||
return JSON.stringify({ | ||
value: data, | ||
ttl: Date.now() + ttl | ||
}) | ||
}; | ||
Storage.prototype.fromJSON = function fromJSON (key) { | ||
try { | ||
var data = JSON.parse(this.driver.getItem(key)); | ||
if (data !== null) { | ||
if (('ttl' in data) && | ||
Number(data.ttl) < Date.now()) { | ||
this.remove(this.removePrefix(key)); | ||
return null | ||
} | ||
if ('value' in data) { | ||
return data.value | ||
} | ||
return data | ||
class Vue2Storage { | ||
constructor(config = {}) { | ||
this.setOptions(config); | ||
} | ||
return null | ||
} catch (e) { | ||
return null | ||
} | ||
}; | ||
Storage.prototype.get = function get (key, fallback) { | ||
if ( fallback === void 0 ) fallback = null; | ||
try { | ||
var val = this.fromJSON(this.addPrefix(key)); | ||
if (val === null) { | ||
return fallback | ||
get length() { | ||
return this.keys().length; | ||
} | ||
return val | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
Storage.prototype.set = function set (key, val, options) { | ||
if ( options === void 0 ) options = {}; | ||
try { | ||
this.driver.setItem(this.addPrefix(key), this.toJSON(val, options)); | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
Storage.prototype.remove = function remove (key) { | ||
try { | ||
this.driver.removeItem(this.addPrefix(key)); | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
Storage.prototype.clear = function clear (force) { | ||
var this$1 = this; | ||
if ( force === void 0 ) force = false; | ||
try { | ||
if (force) { | ||
this.driver.clear(); | ||
} else { | ||
var keys = this.keys().filter(function (key) { return key.startsWith(this$1.options.prefix || ''); }); | ||
keys.forEach(function (key) { return this$1.remove(this$1.removePrefix(key)); }); | ||
get prefix() { | ||
return this.options.prefix; | ||
} | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
get name() { | ||
return 'vue2-storage'; | ||
} | ||
get version() { | ||
return '4.0.0'; | ||
} | ||
get driver() { | ||
switch (this.options.driver) { | ||
case StorageDriver.LOCAL: | ||
default: | ||
return (typeof window !== 'undefined') ? window.localStorage : MemoryStorage$1; | ||
case StorageDriver.SESSION: | ||
return (typeof window !== 'undefined') ? window.sessionStorage : MemoryStorage$1; | ||
case StorageDriver.MEMORY: | ||
return MemoryStorage$1; | ||
} | ||
} | ||
static install(Vue, options) { | ||
const storage = new Vue2Storage(options); | ||
Vue.$storage = storage; | ||
Vue.prototype.$storage = storage; | ||
} | ||
setOptions(config = {}) { | ||
const options = objectAssign({ | ||
prefix: 'app_', | ||
driver: StorageDriver.LOCAL, | ||
ttl: 60 * 60 * 24 * 1000, | ||
}, config); | ||
this.options = Object.freeze(options); | ||
} | ||
get(key, fallback = null) { | ||
try { | ||
const val = this.fromJSON(this.addPrefix(key)); | ||
if (val === null) { | ||
return fallback; | ||
} | ||
return val; | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
set(key, val, options = {}) { | ||
try { | ||
this.driver.setItem(this.addPrefix(key), this.toJSON(val, options)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
remove(key) { | ||
try { | ||
this.driver.removeItem(this.addPrefix(key)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
clear(force = false) { | ||
try { | ||
if (force) { | ||
this.driver.clear(); | ||
} | ||
else { | ||
const keys = this.keys().filter((key) => key.startsWith(this.options.prefix || '')); | ||
keys.forEach((key) => this.remove(this.removePrefix(key))); | ||
} | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
has(key) { | ||
if (this.options.driver !== StorageDriver.MEMORY) { | ||
return (this.addPrefix(key) in this.driver); | ||
} | ||
return (this.addPrefix(key) in this.driver.storage); | ||
} | ||
key(index) { | ||
try { | ||
const key = this.keys()[index]; | ||
return this.get(this.removePrefix(key)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
keys() { | ||
if (this.options.driver !== StorageDriver.MEMORY) { | ||
return Object.keys(this.driver); | ||
} | ||
return Object.keys(this.driver.storage); | ||
} | ||
addPrefix(key) { | ||
return `${this.options.prefix || ''}${key}`; | ||
} | ||
removePrefix(key) { | ||
const re = new RegExp(`^${this.options.prefix || ''}`); | ||
return key.replace(re, ''); | ||
} | ||
toJSON(data, options = {}) { | ||
const ttl = ('ttl' in options) ? options.ttl : this.options.ttl; | ||
return JSON.stringify({ | ||
value: data, | ||
ttl: Date.now() + ttl, | ||
}); | ||
} | ||
fromJSON(key) { | ||
try { | ||
const data = JSON.parse(this.driver.getItem(key)); | ||
if (data !== null) { | ||
if (('ttl' in data) && | ||
Number(data.ttl) < Date.now()) { | ||
this.remove(this.removePrefix(key)); | ||
return null; | ||
} | ||
if ('value' in data) { | ||
return data.value; | ||
} | ||
return data; | ||
} | ||
return null; | ||
} | ||
catch (e) { | ||
return null; | ||
} | ||
} | ||
printError(e) { | ||
console.error(`${this.name}[${this.version}]: ${e.stack}`); | ||
} | ||
} | ||
Storage.prototype.has = function has (key) { | ||
if (this.options.driver !== 'memory') { | ||
return (this.addPrefix(key) in this.driver) | ||
} | ||
return (this.addPrefix(key) in this.driver.storage) | ||
}; | ||
Storage.prototype.key = function key (index) { | ||
try { | ||
var key = this.keys()[index]; | ||
return this.get(this.removePrefix(key)) | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
Storage.prototype.keys = function keys () { | ||
if (this.options.driver !== 'memory') { | ||
return Object.keys(this.driver) | ||
} | ||
return Object.keys(this.driver.storage) | ||
}; | ||
Object.defineProperties( Storage.prototype, prototypeAccessors ); | ||
/* */ | ||
function Vue2Storage (Vue, options) { | ||
var storage = new Storage(options); | ||
Vue['$storage'] = storage; | ||
Vue.prototype.$storage = storage; | ||
if (typeof window !== 'undefined') { | ||
// tslint:disable-next-line | ||
window['Vue2Storage'] = Vue2Storage; | ||
} | ||
window.Vue2Storage = Vue2Storage; | ||
module.exports = Vue2Storage; |
/*! | ||
* vue2-storage v3.3.0 | ||
* (c) 2018 Yarkov Aleksey | ||
* vue2-storage v4.0.0 | ||
* (c) 2019 Yarkov Aleksey | ||
* Released under the MIT License. | ||
@@ -9,223 +9,196 @@ */ | ||
typeof define === 'function' && define.amd ? define(['object-assign'], factory) : | ||
(global.Vue2Storage = factory(global.objectAssign)); | ||
}(this, (function (objectAssign) { 'use strict'; | ||
(global = global || self, global.Vue2Storage = factory(global.objectAssign)); | ||
}(this, function (objectAssign) { 'use strict'; | ||
objectAssign = 'default' in objectAssign ? objectAssign['default'] : objectAssign; | ||
objectAssign = objectAssign && objectAssign.hasOwnProperty('default') ? objectAssign['default'] : objectAssign; | ||
/* */ | ||
var storage = {}; | ||
var MemoryStorage = function MemoryStorage () {}; | ||
var prototypeAccessors$1 = { length: {},storage: {} }; | ||
prototypeAccessors$1.length.get = function () { | ||
return Object.keys(this.storage).length | ||
}; | ||
prototypeAccessors$1.storage.get = function () { | ||
return storage | ||
}; | ||
MemoryStorage.prototype.getItem = function getItem (key) { | ||
return (key in this.storage) ? this.storage[key] : null | ||
}; | ||
MemoryStorage.prototype.setItem = function setItem (key, value) { | ||
this.storage[key] = value; | ||
}; | ||
MemoryStorage.prototype.removeItem = function removeItem (key) { | ||
var found = (key in this.storage); | ||
if (found) { | ||
delete this.storage[key]; | ||
const storage = {}; | ||
class MemoryStorage { | ||
get length() { | ||
return Object.keys(this.storage).length; | ||
} | ||
get storage() { | ||
return storage; | ||
} | ||
getItem(key) { | ||
return (key in this.storage) ? this.storage[key] : null; | ||
} | ||
setItem(key, value) { | ||
this.storage[key] = value; | ||
} | ||
removeItem(key) { | ||
if (key in this.storage) { | ||
delete this.storage[key]; | ||
} | ||
} | ||
clear() { | ||
const keys = Object.keys(this.storage); | ||
for (let i = 0; i <= keys.length; i++) { | ||
try { | ||
delete this.storage[keys[i]]; | ||
} | ||
catch (_a) { | ||
// pass | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
var MemoryStorage$1 = new MemoryStorage(); | ||
MemoryStorage.prototype.clear = function clear () { | ||
var this$1 = this; | ||
var StorageDriver; | ||
(function (StorageDriver) { | ||
StorageDriver["LOCAL"] = "local"; | ||
StorageDriver["SESSION"] = "session"; | ||
StorageDriver["MEMORY"] = "memory"; | ||
})(StorageDriver || (StorageDriver = {})); | ||
for (var k in this.storage) { | ||
delete this$1.storage[k]; | ||
} | ||
}; | ||
Object.defineProperties( MemoryStorage.prototype, prototypeAccessors$1 ); | ||
var MemoryStorage$1 = new MemoryStorage(); | ||
/* */ | ||
var Storage = function Storage (config) { | ||
if ( config === void 0 ) config = {}; | ||
this.setOptions(config); | ||
}; | ||
var prototypeAccessors = { name: {},version: {},length: {},driver: {} }; | ||
prototypeAccessors.name.get = function () { | ||
return 'vue2-storage[v3.3.0]' | ||
}; | ||
prototypeAccessors.version.get = function () { | ||
return '3.3.0' | ||
}; | ||
prototypeAccessors.length.get = function () { | ||
return this.keys().length | ||
}; | ||
prototypeAccessors.driver.get = function () { | ||
switch (this.options.driver) { | ||
case 'local': | ||
default: | ||
return (typeof window !== 'undefined') ? window.localStorage : MemoryStorage$1 | ||
case 'session': | ||
return (typeof window !== 'undefined') ? window.sessionStorage : MemoryStorage$1 | ||
case 'memory': | ||
return MemoryStorage$1 | ||
} | ||
}; | ||
Storage.prototype.setOptions = function setOptions (config) { | ||
if ( config === void 0 ) config = {}; | ||
var options = objectAssign({ | ||
prefix: 'app_', | ||
driver: 'local', | ||
ttl: 60 * 60 * 24 * 1000 // 24 hours | ||
}, config); | ||
this.options = Object.freeze(options); | ||
}; | ||
Storage.prototype.printError = function printError (e) { | ||
console.error(((this.name) + ": " + (e.stack))); | ||
}; | ||
Storage.prototype.addPrefix = function addPrefix (key) { | ||
return ("" + (this.options.prefix || '') + key) | ||
}; | ||
Storage.prototype.removePrefix = function removePrefix (key) { | ||
var re = new RegExp(("^" + (this.options.prefix || ''))); | ||
return key.replace(re, '') | ||
}; | ||
Storage.prototype.toJSON = function toJSON (data, options) { | ||
if ( options === void 0 ) options = {}; | ||
var ttl = ('ttl' in options) ? options.ttl : this.options.ttl; | ||
return JSON.stringify({ | ||
value: data, | ||
ttl: Date.now() + ttl | ||
}) | ||
}; | ||
Storage.prototype.fromJSON = function fromJSON (key) { | ||
try { | ||
var data = JSON.parse(this.driver.getItem(key)); | ||
if (data !== null) { | ||
if (('ttl' in data) && | ||
Number(data.ttl) < Date.now()) { | ||
this.remove(this.removePrefix(key)); | ||
return null | ||
class Vue2Storage { | ||
constructor(config = {}) { | ||
this.setOptions(config); | ||
} | ||
if ('value' in data) { | ||
return data.value | ||
get length() { | ||
return this.keys().length; | ||
} | ||
return data | ||
} | ||
return null | ||
} catch (e) { | ||
return null | ||
get prefix() { | ||
return this.options.prefix; | ||
} | ||
get name() { | ||
return 'vue2-storage'; | ||
} | ||
get version() { | ||
return '4.0.0'; | ||
} | ||
get driver() { | ||
switch (this.options.driver) { | ||
case StorageDriver.LOCAL: | ||
default: | ||
return (typeof window !== 'undefined') ? window.localStorage : MemoryStorage$1; | ||
case StorageDriver.SESSION: | ||
return (typeof window !== 'undefined') ? window.sessionStorage : MemoryStorage$1; | ||
case StorageDriver.MEMORY: | ||
return MemoryStorage$1; | ||
} | ||
} | ||
static install(Vue, options) { | ||
const storage = new Vue2Storage(options); | ||
Vue.$storage = storage; | ||
Vue.prototype.$storage = storage; | ||
} | ||
setOptions(config = {}) { | ||
const options = objectAssign({ | ||
prefix: 'app_', | ||
driver: StorageDriver.LOCAL, | ||
ttl: 60 * 60 * 24 * 1000, | ||
}, config); | ||
this.options = Object.freeze(options); | ||
} | ||
get(key, fallback = null) { | ||
try { | ||
const val = this.fromJSON(this.addPrefix(key)); | ||
if (val === null) { | ||
return fallback; | ||
} | ||
return val; | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
set(key, val, options = {}) { | ||
try { | ||
this.driver.setItem(this.addPrefix(key), this.toJSON(val, options)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
remove(key) { | ||
try { | ||
this.driver.removeItem(this.addPrefix(key)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
clear(force = false) { | ||
try { | ||
if (force) { | ||
this.driver.clear(); | ||
} | ||
else { | ||
const keys = this.keys().filter((key) => key.startsWith(this.options.prefix || '')); | ||
keys.forEach((key) => this.remove(this.removePrefix(key))); | ||
} | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
has(key) { | ||
if (this.options.driver !== StorageDriver.MEMORY) { | ||
return (this.addPrefix(key) in this.driver); | ||
} | ||
return (this.addPrefix(key) in this.driver.storage); | ||
} | ||
key(index) { | ||
try { | ||
const key = this.keys()[index]; | ||
return this.get(this.removePrefix(key)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
keys() { | ||
if (this.options.driver !== StorageDriver.MEMORY) { | ||
return Object.keys(this.driver); | ||
} | ||
return Object.keys(this.driver.storage); | ||
} | ||
addPrefix(key) { | ||
return `${this.options.prefix || ''}${key}`; | ||
} | ||
removePrefix(key) { | ||
const re = new RegExp(`^${this.options.prefix || ''}`); | ||
return key.replace(re, ''); | ||
} | ||
toJSON(data, options = {}) { | ||
const ttl = ('ttl' in options) ? options.ttl : this.options.ttl; | ||
return JSON.stringify({ | ||
value: data, | ||
ttl: Date.now() + ttl, | ||
}); | ||
} | ||
fromJSON(key) { | ||
try { | ||
const data = JSON.parse(this.driver.getItem(key)); | ||
if (data !== null) { | ||
if (('ttl' in data) && | ||
Number(data.ttl) < Date.now()) { | ||
this.remove(this.removePrefix(key)); | ||
return null; | ||
} | ||
if ('value' in data) { | ||
return data.value; | ||
} | ||
return data; | ||
} | ||
return null; | ||
} | ||
catch (e) { | ||
return null; | ||
} | ||
} | ||
printError(e) { | ||
console.error(`${this.name}[${this.version}]: ${e.stack}`); | ||
} | ||
} | ||
}; | ||
Storage.prototype.get = function get (key, fallback) { | ||
if ( fallback === void 0 ) fallback = null; | ||
try { | ||
var val = this.fromJSON(this.addPrefix(key)); | ||
if (val === null) { | ||
return fallback | ||
} | ||
return val | ||
} catch (e) { | ||
this.printError(e); | ||
if (typeof window !== 'undefined') { | ||
// tslint:disable-next-line | ||
window['Vue2Storage'] = Vue2Storage; | ||
} | ||
}; | ||
Storage.prototype.set = function set (key, val, options) { | ||
if ( options === void 0 ) options = {}; | ||
return Vue2Storage; | ||
try { | ||
this.driver.setItem(this.addPrefix(key), this.toJSON(val, options)); | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
Storage.prototype.remove = function remove (key) { | ||
try { | ||
this.driver.removeItem(this.addPrefix(key)); | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
Storage.prototype.clear = function clear (force) { | ||
var this$1 = this; | ||
if ( force === void 0 ) force = false; | ||
try { | ||
if (force) { | ||
this.driver.clear(); | ||
} else { | ||
var keys = this.keys().filter(function (key) { return key.startsWith(this$1.options.prefix || ''); }); | ||
keys.forEach(function (key) { return this$1.remove(this$1.removePrefix(key)); }); | ||
} | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
Storage.prototype.has = function has (key) { | ||
if (this.options.driver !== 'memory') { | ||
return (this.addPrefix(key) in this.driver) | ||
} | ||
return (this.addPrefix(key) in this.driver.storage) | ||
}; | ||
Storage.prototype.key = function key (index) { | ||
try { | ||
var key = this.keys()[index]; | ||
return this.get(this.removePrefix(key)) | ||
} catch (e) { | ||
this.printError(e); | ||
} | ||
}; | ||
Storage.prototype.keys = function keys () { | ||
if (this.options.driver !== 'memory') { | ||
return Object.keys(this.driver) | ||
} | ||
return Object.keys(this.driver.storage) | ||
}; | ||
Object.defineProperties( Storage.prototype, prototypeAccessors ); | ||
/* */ | ||
function Vue2Storage (Vue, options) { | ||
var storage = new Storage(options); | ||
Vue['$storage'] = storage; | ||
Vue.prototype.$storage = storage; | ||
} | ||
window.Vue2Storage = Vue2Storage; | ||
return Vue2Storage; | ||
}))); | ||
})); |
/*! | ||
* vue2-storage v3.3.0 | ||
* (c) 2018 Yarkov Aleksey | ||
* vue2-storage v4.0.0 | ||
* (c) 2019 Yarkov Aleksey | ||
* Released under the MIT License. | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("object-assign")):"function"==typeof define&&define.amd?define(["object-assign"],e):t.Vue2Storage=e(t.objectAssign)}(this,function(t){"use strict";function e(t,e){var r=new s(e);t.$storage=r,t.prototype.$storage=r}t="default"in t?t.default:t;var r={},o=function(){},i={length:{},storage:{}};i.length.get=function(){return Object.keys(this.storage).length},i.storage.get=function(){return r},o.prototype.getItem=function(t){return t in this.storage?this.storage[t]:null},o.prototype.setItem=function(t,e){this.storage[t]=e},o.prototype.removeItem=function(t){t in this.storage&&delete this.storage[t]},o.prototype.clear=function(){var t=this;for(var e in this.storage)delete t.storage[e]},Object.defineProperties(o.prototype,i);var n=new o,s=function(t){void 0===t&&(t={}),this.setOptions(t)},u={name:{},version:{},length:{},driver:{}};return u.name.get=function(){return"vue2-storage[v3.3.0]"},u.version.get=function(){return"3.3.0"},u.length.get=function(){return this.keys().length},u.driver.get=function(){switch(this.options.driver){case"local":default:return"undefined"!=typeof window?window.localStorage:n;case"session":return"undefined"!=typeof window?window.sessionStorage:n;case"memory":return n}},s.prototype.setOptions=function(e){void 0===e&&(e={});var r=t({prefix:"app_",driver:"local",ttl:864e5},e);this.options=Object.freeze(r)},s.prototype.printError=function(t){console.error(this.name+": "+t.stack)},s.prototype.addPrefix=function(t){return""+(this.options.prefix||"")+t},s.prototype.removePrefix=function(t){var e=new RegExp("^"+(this.options.prefix||""));return t.replace(e,"")},s.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})},s.prototype.fromJSON=function(t){try{var e=JSON.parse(this.driver.getItem(t));return null!==e?"ttl"in e&&Number(e.ttl)<Date.now()?(this.remove(this.removePrefix(t)),null):"value"in e?e.value:e:null}catch(t){return null}},s.prototype.get=function(t,e){void 0===e&&(e=null);try{var r=this.fromJSON(this.addPrefix(t));return null===r?e:r}catch(t){this.printError(t)}},s.prototype.set=function(t,e,r){void 0===r&&(r={});try{this.driver.setItem(this.addPrefix(t),this.toJSON(e,r))}catch(t){this.printError(t)}},s.prototype.remove=function(t){try{this.driver.removeItem(this.addPrefix(t))}catch(t){this.printError(t)}},s.prototype.clear=function(t){var e=this;void 0===t&&(t=!1);try{if(t)this.driver.clear();else{this.keys().filter(function(t){return t.startsWith(e.options.prefix||"")}).forEach(function(t){return e.remove(e.removePrefix(t))})}}catch(t){this.printError(t)}},s.prototype.has=function(t){return"memory"!==this.options.driver?this.addPrefix(t)in this.driver:this.addPrefix(t)in this.driver.storage},s.prototype.key=function(t){try{var e=this.keys()[t];return this.get(this.removePrefix(e))}catch(t){this.printError(t)}},s.prototype.keys=function(){return"memory"!==this.options.driver?Object.keys(this.driver):Object.keys(this.driver.storage)},Object.defineProperties(s.prototype,u),window.Vue2Storage=e,e}); | ||
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('object-assign')):typeof define==='function'&&define.amd?define(['object-assign'],f):(g=g||self,g.Vue2Storage=f(g.objectAssign));}(this,function(objectAssign){'use strict';objectAssign=objectAssign&&objectAssign.hasOwnProperty('default')?objectAssign['default']:objectAssign;const storage = {}; | ||
class MemoryStorage { | ||
get length() { | ||
return Object.keys(this.storage).length; | ||
} | ||
get storage() { | ||
return storage; | ||
} | ||
getItem(key) { | ||
return (key in this.storage) ? this.storage[key] : null; | ||
} | ||
setItem(key, value) { | ||
this.storage[key] = value; | ||
} | ||
removeItem(key) { | ||
if (key in this.storage) { | ||
delete this.storage[key]; | ||
} | ||
} | ||
clear() { | ||
const keys = Object.keys(this.storage); | ||
for (let i = 0; i <= keys.length; i++) { | ||
try { | ||
delete this.storage[keys[i]]; | ||
} | ||
catch (_a) { | ||
// pass | ||
} | ||
} | ||
} | ||
} | ||
var MemoryStorage$1 = new MemoryStorage();var StorageDriver; | ||
(function (StorageDriver) { | ||
StorageDriver["LOCAL"] = "local"; | ||
StorageDriver["SESSION"] = "session"; | ||
StorageDriver["MEMORY"] = "memory"; | ||
})(StorageDriver || (StorageDriver = {}));class Vue2Storage { | ||
constructor(config = {}) { | ||
this.setOptions(config); | ||
} | ||
get length() { | ||
return this.keys().length; | ||
} | ||
get prefix() { | ||
return this.options.prefix; | ||
} | ||
get name() { | ||
return 'vue2-storage'; | ||
} | ||
get version() { | ||
return '4.0.0'; | ||
} | ||
get driver() { | ||
switch (this.options.driver) { | ||
case StorageDriver.LOCAL: | ||
default: | ||
return (typeof window !== 'undefined') ? window.localStorage : MemoryStorage$1; | ||
case StorageDriver.SESSION: | ||
return (typeof window !== 'undefined') ? window.sessionStorage : MemoryStorage$1; | ||
case StorageDriver.MEMORY: | ||
return MemoryStorage$1; | ||
} | ||
} | ||
static install(Vue, options) { | ||
const storage = new Vue2Storage(options); | ||
Vue.$storage = storage; | ||
Vue.prototype.$storage = storage; | ||
} | ||
setOptions(config = {}) { | ||
const options = objectAssign({ | ||
prefix: 'app_', | ||
driver: StorageDriver.LOCAL, | ||
ttl: 60 * 60 * 24 * 1000, | ||
}, config); | ||
this.options = Object.freeze(options); | ||
} | ||
get(key, fallback = null) { | ||
try { | ||
const val = this.fromJSON(this.addPrefix(key)); | ||
if (val === null) { | ||
return fallback; | ||
} | ||
return val; | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
set(key, val, options = {}) { | ||
try { | ||
this.driver.setItem(this.addPrefix(key), this.toJSON(val, options)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
remove(key) { | ||
try { | ||
this.driver.removeItem(this.addPrefix(key)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
clear(force = false) { | ||
try { | ||
if (force) { | ||
this.driver.clear(); | ||
} | ||
else { | ||
const keys = this.keys().filter((key) => key.startsWith(this.options.prefix || '')); | ||
keys.forEach((key) => this.remove(this.removePrefix(key))); | ||
} | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
has(key) { | ||
if (this.options.driver !== StorageDriver.MEMORY) { | ||
return (this.addPrefix(key) in this.driver); | ||
} | ||
return (this.addPrefix(key) in this.driver.storage); | ||
} | ||
key(index) { | ||
try { | ||
const key = this.keys()[index]; | ||
return this.get(this.removePrefix(key)); | ||
} | ||
catch (e) { | ||
this.printError(e); | ||
} | ||
} | ||
keys() { | ||
if (this.options.driver !== StorageDriver.MEMORY) { | ||
return Object.keys(this.driver); | ||
} | ||
return Object.keys(this.driver.storage); | ||
} | ||
addPrefix(key) { | ||
return `${this.options.prefix || ''}${key}`; | ||
} | ||
removePrefix(key) { | ||
const re = new RegExp(`^${this.options.prefix || ''}`); | ||
return key.replace(re, ''); | ||
} | ||
toJSON(data, options = {}) { | ||
const ttl = ('ttl' in options) ? options.ttl : this.options.ttl; | ||
return JSON.stringify({ | ||
value: data, | ||
ttl: Date.now() + ttl, | ||
}); | ||
} | ||
fromJSON(key) { | ||
try { | ||
const data = JSON.parse(this.driver.getItem(key)); | ||
if (data !== null) { | ||
if (('ttl' in data) && | ||
Number(data.ttl) < Date.now()) { | ||
this.remove(this.removePrefix(key)); | ||
return null; | ||
} | ||
if ('value' in data) { | ||
return data.value; | ||
} | ||
return data; | ||
} | ||
return null; | ||
} | ||
catch (e) { | ||
return null; | ||
} | ||
} | ||
printError(e) { | ||
console.error(`${this.name}[${this.version}]: ${e.stack}`); | ||
} | ||
}if (typeof window !== 'undefined') { | ||
// tslint:disable-next-line | ||
window['Vue2Storage'] = Vue2Storage; | ||
}return Vue2Storage;})); |
117
package.json
{ | ||
"name": "vue2-storage", | ||
"description": "Browser storage for Vue.js app", | ||
"version": "3.4.0", | ||
"version": "4.0.0", | ||
"author": { | ||
@@ -17,20 +17,16 @@ "name": "Yarkov Aleksey", | ||
"scripts": { | ||
"build": "node config/build.js", | ||
"clean": "rm -rf coverage && rm -rf dist/*.js* && rm -f ./*.log", | ||
"dev": "cross-env BABEL_ENV=test webpack-dev-server --inline --hot --open --content-base ./test/unit/ --config config/webpack.dev.conf.js", | ||
"docs": "gitbook serve ./gitbook ./docs", | ||
"docs:build": "node config/version.js && gitbook build ./gitbook ./docs", | ||
"build": "cross-env NODE_ENV=production rollup -c rollup.config.ts", | ||
"start": "cross-env NODE_ENV=development rollup -c rollup.config.ts --watch", | ||
"docs:version": "node config/version.js", | ||
"docs:install": "gitbook install ./gitbook", | ||
"flow": "flow check", | ||
"lint": "eslint src test config", | ||
"test": "npm run lint && npm run flow && npm run test:cover", | ||
"test:cover": "cross-env BABEL_ENV=test karma start config/karma.cover.conf.js", | ||
"test:e2e": "npm run build && node test/e2e/runner.js", | ||
"test:unit": "cross-env BABEL_ENV=test karma start config/karma.unit.conf.js" | ||
"docs:watch": "run-s docs:version docs:install && gitbook serve ./gitbook ./docs", | ||
"docs:build": "run-s docs:version docs:install && gitbook build ./gitbook ./docs", | ||
"lint": "tslint -c tslint.json -p tsconfig.json --force", | ||
"lint:fix": "yarn lint --fix", | ||
"test": "jest", | ||
"test:watch": "jest --watch", | ||
"clean": "rimraf dist/*.js* ./*.log" | ||
}, | ||
"files": [ | ||
"dist/vue2-storage.js", | ||
"dist/vue2-storage.min.js", | ||
"dist/vue2-storage.common.js", | ||
"src" | ||
"dist/*.js" | ||
], | ||
@@ -41,3 +37,3 @@ "homepage": "https://github.com/yarkovaleksei/vue2-storage#readme", | ||
"unpkg": "dist/vue2-storage.js", | ||
"typings": "types/index.d.ts", | ||
"typings": "dist/vue2-storage.d.ts", | ||
"keywords": [ | ||
@@ -53,56 +49,43 @@ "vue", | ||
"license": "MIT", | ||
"dependencies": { | ||
"object-assign": "^4.1.1" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.22.1", | ||
"babel-eslint": "^7.1.0", | ||
"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-flow-vue": "^1.0.0", | ||
"babel-preset-power-assert": "^1.0.0", | ||
"buble": "^0.14.0", | ||
"chromedriver": "^2.27.2", | ||
"cross-env": "^5.0.5", | ||
"cross-spawn": "^5.0.1", | ||
"eslint": "^3.14.1", | ||
"eslint-loader": "^1.6.1", | ||
"eslint-plugin-flowtype": "^2.30.0", | ||
"eslint-plugin-vue-libs": "^1.2.0", | ||
"flow-bin": "^0.38.0", | ||
"gitbook-cli": "^2.3.0", | ||
"html-webpack-plugin": "^2.19.0", | ||
"http-server": "^0.9.0", | ||
"karma": "^1.4.1", | ||
"karma-chrome-launcher": "^2.1.1", | ||
"karma-coverage": "^1.1.1", | ||
"karma-firefox-launcher": "^1.0.0", | ||
"karma-mocha": "^1.3.0", | ||
"karma-mocha-reporter": "^2.2.2", | ||
"karma-safari-launcher": "^1.0.0", | ||
"karma-sourcemap-loader": "^0.3.7", | ||
"karma-webpack": "^2.0.2", | ||
"mocha": "^3.2.0", | ||
"mocha-loader": "^1.1.1", | ||
"nightwatch": "^0.9.12", | ||
"nightwatch-helpers": "^1.2.0", | ||
"power-assert": "^1.4.2", | ||
"rollup": "^0.36.4", | ||
"rollup-plugin-babel": "^3.0.3", | ||
"rollup-plugin-buble": "^0.14.0", | ||
"rollup-plugin-flow-no-whitespace": "^1.0.0", | ||
"rollup-plugin-json": "^2.3.0", | ||
"rollup-plugin-replace": "^1.1.1", | ||
"selenium-server": "2.53.1", | ||
"uglify-js": "^2.7.5", | ||
"vue": "^2.1.10", | ||
"webpack": "^2.2.0", | ||
"webpack-dev-server": "^2.2.1" | ||
"@types/jest": "^24.0.12", | ||
"cross-env": "^5.2.0", | ||
"gitbook-cli": "^2.3.2", | ||
"husky": "^2.2.0", | ||
"jest": "^24.8.0", | ||
"jest-config": "^24.8.0", | ||
"jest-localstorage-mock": "^2.4.0", | ||
"lint-staged": "^8.1.6", | ||
"npm-run-all": "^4.1.5", | ||
"rimraf": "^2.6.3", | ||
"rollup": "^1.11.3", | ||
"rollup-plugin-commonjs": "^9.3.4", | ||
"rollup-plugin-copy": "^2.0.1", | ||
"rollup-plugin-node-resolve": "^4.2.4", | ||
"rollup-plugin-replace": "^2.2.0", | ||
"rollup-plugin-tslint": "^0.2.2", | ||
"rollup-plugin-typescript": "^1.0.1", | ||
"ts-jest": "^24.0.2", | ||
"tslint-eslint-rules": "^5.4.0", | ||
"typescript": "^3.4.5", | ||
"vue": "^2.6.10" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"src/**/*.ts": [ | ||
"yarn lint:fix", | ||
"yarn test --passWithNoTests", | ||
"git add" | ||
] | ||
}, | ||
"engines": { | ||
"node": ">= 6.0" | ||
}, | ||
"dependencies": { | ||
"object-assign": "^4.1.1" | ||
"node": ">= 8.*" | ||
} | ||
} |
@@ -20,12 +20,4 @@ # vue2-storage | ||
## :exclamation: Issues | ||
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. | ||
## :muscle: Contribution | ||
Please make sure to read the [Contributing Guide](https://github.com/yarkovaleksei/vue2-storage/blob/dev/CONTRIBUTING.md) before making a pull request. | ||
## :copyright: License | ||
[MIT](http://opensource.org/licenses/MIT) |
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
27481
21
763
0
8
23
1