Simple window.localStorage
abstraction wrapper
$ bower info local-storage
$ bower install local-storage
$ git clone https://github.com/mwager/LocalStorage
$ npm install && bower install
# -> src/LocalStorage.js
# run the tests
$ grunt test
# or:
$ open test/index.html
The API is very simple, window.localStorage
's exceptions will be
propagated.
There are 4 public methods:
get(key)
save(key, data)
drop(key)
nuke()
// Initializing throws an exception if one of the following is true:
// - localStorage is not supported in the running environment
// - `window.JSON` is not defined
var ls = new LocalStorage();
var obj;
// persist:
try {
ls.save('some-key', {foo:'bar'});
}
catch(e) {
// localStorage exception, eg. e.name === 'QuotaExceededError'
console.log(e);
}
// read:
obj = ls.get('some-key');
console.log(typeof obj); // 'object' -> {foo:'bar'}
// delete:
ls.drop('some-key');
obj = ls.get('some-key'); // null
// clear localStorage:
ls.nuke();
# We use jshint for static analysis, execute:
$ jshint .