Browser storage layer with enforced keyspaces and a switchable backend.
A reusable Store module with the following characteristics:
Here's an example:
// Create a new store, passing the keys we want and some options
var store = new Store(['user', 'auth'], {
// Prefer local storage, but fall back to session
backend: ['local', 'session'],
// Run data through JSON.stringify on the way in (set),
// and JSON.parse on the way out (get).
transformer: JSON
});
// Set and get some data
store.set('user', { id: 10, name: 'Tom' });
var user = store.get('user'); // user.name === 'Tom'
// You cannot use keys you haven't declared
store.set('not-allowed', { evil: true }); // Throws!
The code is very well documented; have a read.
MIT