Giter Club home page Giter Club logo

storage.js's Introduction

storage.js

A tiny JavaScript library that help use localStorage and sessionStorage (HTML5 API)

Usage

Download storage.min.js (~1.271 kb) and include it in your HTML document:

<script src="storage.min.js"></script>

If you include storage.js as above, this script will add an object called storage to your global scope.


storage.js also supports AMD. So if you want to include storage.js dynamically, you can just require it with any AMD loader, for example RequireJS. Follow the instructions of your AMD loader to include storage.js.


After that you can call any of methods that are explained in the following.

Choose between local or session

storage.type = localStorage; // or sessionStorage

storage.enabled()

if (storage.enabled()) {
  // Do stuff with local/session storage
} else {
  // Fallback
}

storage.set(name, value)

storage.set(name, value);

You can also set several values at once:

storage.set({
   key1: 'value1',
   key2: 'value2'
});

storage.get(name)

storage.get(name);

Passing just one key like this will return a string. You can also pass an array of keys:

storage.get(['key1', 'key2']);

This will always return an object. The keys of this object will be the keys you passed and the values are the corresponding values.

In case you want to add a default value you can use the second parameter.

storage.get('key', 'default value');

This also works with several keys:

storage.get(['key1', 'key2'], 'default value');

storage.del(name)

This method allows you to remove datas. It accepts an infinite number of keys or an array of keys.

storage.del('key');
storage.del('key1', 'key2');
storage.del(['key1', 'key2']);

storage.clear()

Simply call storage.clear() and every datas will be removed.

storage.all()

var datas = storage.all();

storage.change()

/!\ TODO - Event catch

Chaining

The methods set, del, clear and change return the storage object and therefore enable chaining.

storage.clear().change(function(){ alert('Storage change!') }).set('key', 'value').get('key');

Contribute

If you find any bugs in this script or have ideas how to improve it please let me know by creating an issue.

You are also very welcome to contribute to the code here on github.

Enjoy!

storage.js's People

Contributors

monkeymonk avatar

Stargazers

Fajar Khairil avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.