Skip to content

Simple `window.localStorage` abstraction with JSON (stringify/parse) support

Notifications You must be signed in to change notification settings

mwager/LocalStorage

Repository files navigation

LocalStorage.js

Simple window.localStorage abstraction wrapper

Build Status

Installation

Via bower

$ bower info local-storage
$ bower install local-storage

From source

$ git clone https://github.com/mwager/LocalStorage
$ npm install && bower install
# -> src/LocalStorage.js

# run the tests
$ grunt test
# or:
$ open test/index.html

Usage

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()

Examples

// 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();

Code Quality

# We use jshint for static analysis, execute:
$ jshint .

About

Simple `window.localStorage` abstraction with JSON (stringify/parse) support

Resources

Stars

Watchers

Forks

Packages

No packages published