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
param | type | flag | description |
---|---|---|---|
name | string | The name of the store | |
flushExpired | boolean | optinal | run auto flush for expired values |
var Store = ; var UserStore = '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 = 'UserStore' false;
Static methods
Store.flush
Removes all Store entries
return: void
var Store = ; Store;
Store.flushExpired
Removes all expired Store entries
return: void
Store;
Instance methods
Store#set
Set a key with the value to store
param | type | flag | description |
---|---|---|---|
key | string | The name of the value | |
data | mixed | The data to store | |
expiresAt | integer | optional | When should this value expire |
return: boolean
var Store = ; var UserStore = 'user'; UserStore; // -> trueUserStore; // -> true // set a value with an expire date (2s)UserStore; // -> true
Store#isset
Check if the given key exists
param | type | description |
---|---|---|
key | string | Key defined in Store.set |
return: boolean
var Store = ; var UserStore = 'user'; UserStore; // -> trueUserStore; // -> false
Store#get
Get the value stored under the given key, or the whole store
param | type | flag | description |
---|---|---|---|
key | string | optional | Key defined in Store.set |
return: object, mixed
var Store = ; var UserStore = 'user'; UserStore; // -> John DoeUserStore; // -> { name: 'John Doe', wife: 'Jane Doe' }
Store#remove
Remove an item
param | type | description |
---|---|---|
key | string | Key defined in Store.set |
return: boolean
var Store = ; var UserStore = 'user'; UserStore // -> true
Store#flush
Flush all values, that are stored in the Store
instance
return: void
var Store = ; var UserStore = 'user'; UserStore;
Store#flushExpired
Flush expired values defined in this store
return: void
var Store = ; var UserStore = 'user'; UserStore;
Test
To run a test just enter the following command
npm run test