localstoragejs
Advanced tools
Comparing version 0.1.2 to 0.1.6
{ | ||
"name": "localstoragejs", | ||
"version": "0.1.2", | ||
"version": "0.1.6", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Gian Marco Toso <gianmarco.toso@gmail.com>" |
@@ -1,1 +0,1 @@ | ||
!function(a,b){"function"==typeof define&&define.amd?define([],function(){var c=b();return a.LocalStorage=c,c}):a.LocalStorage=b()}(this,function(){var a=[Object,Number,String,Array,Date,Boolean],b=function(a,b){this.name="LocalStorageException",this.message=a,this.value=b};if(!this.localStorage)throw new b("Local storage is not supported by your configuration. Sorry :(");var c=function(a,c){if(!a)throw new b("A LocalStorage instance must have a name");c=c&&c.constructor===Object?c:{};var d=a,e={},f={blockingValidation:!1},g=f;Object.keys(f).forEach(function(a){c.hasOwnProperty(a)&&(g[a]=c[a])}),Object.defineProperty(this,"options",{enumerable:!0,get:function(){return g},set:function(a){if(a.constructor!==Object)throw new b("Cannot set the options property to anything other than an object.");g=a}}),Object.defineProperty(this,"name",{enumerable:!0,get:function(){return d}}),Object.defineProperty(this,"validators",{enumerable:!0,get:function(){return e},set:function(a){if(a.constructor!==Object)throw new b("Cannot set the validators property to anything other than an object. You shouldn't write this property directly anyways!");e=a}})};return c.get=function(a){var b=localStorage.getItem(a);return b&&(b=JSON.parse(b)),b},c.set=function(c,d){if(!d)throw new b("No value has been passed");if(-1===a.indexOf(d.constructor))throw new b("Cannot serialize value",d);localStorage.setItem(c,JSON.stringify(d))},c.remove=function(a){localStorage.removeItem(a)},c.clear=function(){Object.keys(localStorage).forEach(function(a){c.remove(a)})},c.prototype.get=function(a){return c.get(this.name+":"+a)},c.prototype.set=function(a,d,e){if(e&&e.constructor===Function&&!e(d)||!e&&this.validators[a]&&!this.validators[a](d)){if(this.options.blockingValidation===!0)throw new b('Validation failed for key "'+a+'"',d);return!1}return c.set(this.name+":"+a,d),this},c.prototype.remove=function(a){return c.remove(this.name+":"+a),this},c.prototype.all=function(){var a={},b=new RegExp("^"+this.name+":.+$");return Object.keys(localStorage).forEach(function(d){b.test(d)&&(a[d.substr(this.name.length+1)]=c.get(d))}.bind(this)),a},c.prototype.clear=function(){var a=new RegExp("^"+this.name+":.+$");return Object.keys(localStorage).forEach(function(b){a.test(b)&&c.remove(b)}.bind(this)),this},c.prototype.setValidator=function(a,c){if(c.constructor!==Function)throw new b("A validator must be a function!");return this.validators[a]=c,this},c.prototype.clearValidator=function(a){return this.validators[a]&&delete this.validators[a],this},c.prototype.clearValidators=function(){return this.validators={},this},c}); | ||
!function(a,b){"function"==typeof define&&define.amd?define([],function(){var c=b();return a.LocalStorage=c,c}):"undefined"!=typeof exports?module.exports=b():a.LocalStorage=b()}(this,function(){var a=!1,b=[Object,Number,String,Array,Date,Boolean],c=function(a,b){this.name="LocalStorageException",this.message=a,this.value=b};this.localStorage||(this.localStorage={_data:{},setItem:function(a,b){return this._data[a]=String(b)},getItem:function(a){return this._data.hasOwnProperty(a)?this._data[a]:void 0},removeItem:function(a){return delete this._data[a]},clear:function(){return this._data={}}},console.warning("LocalStorage is not supported by your browser. Avoiding hard crash by shimming (quick and dirty), thanks to https://gist.github.com/juliocesar/926500"));var d=function(a,b){if(!a)throw new c("A LocalStorage instance must have a name");b=b&&b.constructor===Object?b:{};var d,e=a,f={},g={blockingValidation:!1,usePromises:!1},h=g;Object.keys(g).forEach(function(a){b.hasOwnProperty(a)&&(h[a]=b[a])}),Object.defineProperty(this,"options",{enumerable:!0,get:function(){return h},set:function(a){if(a.constructor!==Object)throw new c("Cannot set the options property to anything other than an object.");h=a}}),Object.defineProperty(this,"name",{enumerable:!0,get:function(){return e}}),Object.defineProperty(this,"validators",{enumerable:!0,get:function(){return f},set:function(a){if(a.constructor!==Object)throw new c("Cannot set the validators property to anything other than an object. You shouldn't write this property directly anyways!");f=a}}),Object.defineProperty(this,"Promise",{enumerable:!0,get:function(){if(!d&&window.Promise&&(d=window.Promise),!d)throw new c("Promises are not supported by your configuration, but you might try with a library or a polyfill!");return d},set:function(a){if(a.constructor!==Function)throw new c("Promise implementation must be a constructor.");d=a}})};return d.get=function(a){var b=localStorage.getItem(a);return b&&(b=JSON.parse(b)),b},d.set=function(d,e){if(!e)throw new c("No value has been passed");if(a===!0&&-1===b.indexOf(e.constructor))throw new c("Cannot serialize value",e);localStorage.setItem(d,JSON.stringify(e))},d.remove=function(a){localStorage.removeItem(a)},d.clear=function(){Object.keys(localStorage).forEach(function(a){d.remove(a)})},d.allowConstructor=function(a){b.push(a)},d.toggleConstructorCheck=function(b){a=b},d.prototype.get=function(a){return d.get(this.name+":"+a)},d.prototype.set=function(a,b,e){var f=e&&e.constructor===Function&&!e(b)||!e&&this.validators[a]&&!this.validators[a](b);if(this.options.usePromises===!0){var g=new this.Promise(function(e,g){return f?g(this.options.blockingValidation===!0?new c('Validation failed for key "'+a+'"',b):!1):(d.set(this.name+":"+a,b),e(this))}.bind(this));return g}if(f){if(this.options.blockingValidation===!0)throw new c('Validation failed for key "'+a+'"',b);return!1}return d.set(this.name+":"+a,b),this},d.prototype.remove=function(a){return d.remove(this.name+":"+a),this},d.prototype.all=function(){var a={},b=new RegExp("^"+this.name+":.+$");return Object.keys(localStorage).forEach(function(c){b.test(c)&&(a[c.substr(this.name.length+1)]=d.get(c))}.bind(this)),a},d.prototype.clear=function(){var a=new RegExp("^"+this.name+":.+$");return Object.keys(localStorage).forEach(function(b){a.test(b)&&d.remove(b)}.bind(this)),this},d.prototype.setValidator=function(a,b){if(b.constructor!==Function)throw new c("A validator must be a function!");return this.validators[a]=b,this},d.prototype.clearValidator=function(a){return this.validators[a]&&delete this.validators[a],this},d.prototype.clearValidators=function(){return this.validators={},this},d}); |
{ | ||
"name": "localstoragejs", | ||
"version": "0.1.2", | ||
"version": "0.1.6", | ||
"description": "A LocalStorage wrapper with some extra functions", | ||
@@ -5,0 +5,0 @@ "main": "src/LocalStorage.js", |
@@ -17,2 +17,4 @@ #LocalStorage | ||
LocalStorage.clear(); // Removes everything from local storage. | ||
LocalStorage.toggleConstructorCheck(value) // Enables or disables the check on the value constructor (disallows the serialization of exotic objects) | ||
LocalStorage.allowConstructor(constructor); // Allows the serialization of the specified constructor. | ||
```` | ||
@@ -124,2 +126,2 @@ | ||
##License | ||
MIT | ||
MIT |
@@ -17,2 +17,4 @@ (function(root, factory) { | ||
})(this, function() { | ||
var constructorCheck = false; | ||
var allowedConstructors = [ | ||
@@ -34,3 +36,11 @@ Object, | ||
if (!this.localStorage) { | ||
throw new LocalStorageException("Local storage is not supported by your configuration. Sorry :("); | ||
window.localStorage = { | ||
_data : {}, | ||
setItem : function(id, val) { return this._data[id] = String(val); }, | ||
getItem : function(id) { return this._data.hasOwnProperty(id) ? this._data[id] : undefined; }, | ||
removeItem : function(id) { return delete this._data[id]; }, | ||
clear : function() { return this._data = {}; } | ||
}; | ||
console.error('LocalStorage is not supported by your browser. Avoiding hard crash by shimming (quick and dirty), thanks to https://gist.github.com/juliocesar/926500'); | ||
} | ||
@@ -135,3 +145,3 @@ | ||
if (allowedConstructors.indexOf(value.constructor) === -1) { | ||
if (constructorCheck === true && allowedConstructors.indexOf(value.constructor) === -1) { | ||
throw new LocalStorageException("Cannot serialize value", value); | ||
@@ -153,2 +163,10 @@ } | ||
LocalStorage.allowConstructor = function(constructor) { | ||
allowedConstructors.push(constructor); | ||
} | ||
LocalStorage.toggleConstructorCheck = function(value) { | ||
constructorCheck = value; | ||
} | ||
// Instance methods | ||
@@ -155,0 +173,0 @@ LocalStorage.prototype.get = function(key) { |
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
18574
256
126