#LocalStorage
A wrapper library for HTML5's native localStorage, with some utility functions, validation and namespacing.
##Installation
You can install the library by using bower install localstoragejs
or you can checkout the repo directly. Depending on your configuration, either incude the file in the src
directory or the one in the dist
directory.
The module exposes both a global LocalStorage
variable and, if you're using it, a requirejs
module.
##Usage
You can use the module directly from the LocalStorage
global variable through its static methods, that allow for a straightforward wrapping of the localStorage functions. The only difference is that whatever you're saving in the dictionary is automatically stringified/parsed as JSON.
LocalStorage.set('myValue', 15);
LocalStorage.get('myValue');
LocalStorage.remove('myValue');
LocalStorage.clear();
LocalStorage.toggleConstructorCheck(value)
LocalStorage.allowConstructor(constructor);
If you want more flexibility, you can create an instance of the LocalStorage object. This will namespace all the keys to the name of the instance, and will also allow you to use validators.
###Namespacing
Namespacing allows you to 'group' keys together by using a common prefix, which is based on the string you pass to the LocalStorage constructor.
var db = new LocalStorage('DB');
db.set('myValue', [1,2,3]);
db.get('myValue');
db.remove('myValue');
db.all();
db.clear();
###Validation
Sometimes you might want to validate a value before saving it. Validators serve exactly that purpose and come in two flavors: one-shot and permanent.
When you create an instance of the LocalStorage
object, you have the option to pass a second parameter with some options. Right now, the only option available is the blockingValidation
option which, if set to true, makes the set
call throw a LocalStorageException
when failing validation instead of simply returning false
.
####One-shot validators
A one-shot validator is evaluated within the scope of a single set
call and is not saved within your LocalStorage
object instance.
var db = new LocalStorage('DB');
db.set('username', 'fred', function(value) {
return value.length >= 8;
});
####Permanents validators
A permanent validator is associated with a key and will be called anytime you attempt to save a value for that key. A permanent validator has lower priority than a one-shot validator, so you can always override it by passing a one-shot validator (or true
) to a set
call.
var db = new LocalStorage('DB', {blockingValidation: true});
db.setValidator('username', function(value) {
return value.length >= 8;
});
db.set('username', 'fred');
db.set('username', 'fred1234');
db.set('username', 'fred', function(value) { return value === 'fred'; });
db.set('username', 'brad', true);
If you want to clear a permanent validator, or you want to clear all of them, you can do so by calling either the clearValidator
or the clearValidators
functions:
db.clearValidator('username');
db.clearValidators();
###Chaining
The following instance methods are chainable:
set
(when validation passes)remove
all
setValidator
clearValidator
clearValidators
That's pretty much it (for now)!
##A note on compatibility
I'm assuming this library will be used in projects made for modern browsers. I haven't put any effort in testing compatibility other than to check if the localStorage
variable is present on the global scope. If you feel that this is not sufficient, please send a PR and I will consider it!
##Promises?
Even though all this stuff is synchronous, I'm toying with the idea of using promises instead of/alongside return values. For example, there could be an option like usePromises
that makes each method return a promise instead of the result of the operation or the object itself (for chainable methods). This would remove the ambiguity of the return value for the set
method, and would allow - with some tweaking - the use of asynchronous validators. For this purpose I would need to make this library dependent on a library such as Q or BlueBird. Thoughts?
##Contributing
If you find bugs or things you don't like, please send a PR. Remember to build the minified version of library using grunt
and update the README if required.
##License
MIT