0.2.3 • Published 7 years ago

localstorage-plus v0.2.3

Weekly downloads
7
License
MIT
Repository
github
Last release
7 years ago

Build Status Code Climate npm

localstorage-plus

Simple helper class that extends the localStorage implementation

Contents

Getting started

Download via npm.

npm install localstorage-plus --save-dev

You can also download the project and include the store.min.js file into your HTML-Document

<html>
    <head>
        ...
    </head>
    <body>
        ...

        <script src="/path/to/store.min.js" type="text/javascript"></script>
        <script src="/path/to/your/app.js" type="text/javascript"></script>
    </body>
</html>

Documentation

Initialize a Store

paramtypeflagdescription
namestringThe name of the store
flushExpiredbooleanoptinalrun auto flush for expired values
var Store = require('localstorage-plus');

var UserStore = new Store('user')

If you are using the <script> way, the Store will be applied to the window object.

Suppress auto expiredFlush on creation

When a new Store instace gets created, the constructor will run the flushExpired method automaticly, accept your provide a boolean as the second parameter like so:

var UserStore = new Store('UserStore', false);

Static methods

Store.flush

Removes all Store entries

return: void

var Store = require('localstorage-plus');

Store.flush();

Store.flushExpired

Removes all expired Store entries

return: void

Store.flushExpired();

Instance methods

Store#set

Set a key with the value to store

paramtypeflagdescription
keystringThe name of the value
datamixedThe data to store
expiresAtintegeroptionalWhen should this value expire

return: boolean

var Store = require('localstorage-plus');

var UserStore = new Store('user');

UserStore.set('name', 'John Doe');                   // -> true
UserStore.set('wife', 'Jane Doe');                   // -> true

// set a value with an expire date (2s)
UserStore.set('token', 'secret', Date.now() + 2000); // -> true

Store#isset

Check if the given key exists

paramtypedescription
keystringKey defined in Store.set

return: boolean

var Store = require('localstorage-plus');

var UserStore = new Store('user');

UserStore.isset('user');  // -> true
UserStore.isset('age');   // -> false

Store#get

Get the value stored under the given key, or the whole store

paramtypeflagdescription
keystringoptionalKey defined in Store.set

return: object, mixed

var Store = require('localstorage-plus');

var UserStore = new Store('user');

UserStore.get('name');   // -> John Doe
UserStore.get();         // -> { name: 'John Doe', wife: 'Jane Doe' }

Store#remove

Remove an item

paramtypedescription
keystringKey defined in Store.set

return: boolean

var Store = require('localstorage-plus');

var UserStore = new Store('user');

UserStore.remove('name') // -> true

Store#flush

Flush all values, that are stored in the Store instance

return: void

var Store = require('localstorage-plus');

var UserStore = new Store('user');

UserStore.flush();

Store#flushExpired

Flush expired values defined in this store

return: void

var Store = require('localstorage-plus');

var UserStore = new Store('user');

UserStore.flushExpired();

Test

To run a test just enter the following command

npm run test