Lightweight utilities that can make easier to write and read application storage in client browser. Inspired by Sutan gading's simple web storage, one of our team member
it's a asynchronous API using Promise
- Local Storage
- Cookie Storage
- Session Storage
$ npm i @itpolsri/simple-webstorage
import SimpleWebStorage from '@itpolsri/simple-webstorage'
const storage = SimpleWebStorage()
// empty the third parameter to store data permanently (only affected in local)
storage
.asyncLocal
.set('key', 'value')
.then(r=> /*do what you want to do..*/)
storage
.asyncCookie
.set('key', 'value', 5)
.then(r=> /*do what you want to do..*/)
storage
.asyncSession
.set('key', 'value', 5)
.then(r=> /*do what you want to do..*/)
// we support setBulk too :), if you want to set data all at once
storage
.asyncLocal
.setBulk([
{
key:'a',
value:'hello'
},
{
key:'b',
value:'world'
}
])
.then(r=> /*do what you want to do*/)
// resolved value = [{key:'a',value:'hello'} , {key:'b',value:'world'}]
// # for local storage
import {
get as getLocalStorage,
set as setLocalStorage ,
setBulk as setBulkLocalStorage
} from '@itpolsri/simple-webstorage/src/local'
setLocalStorage('key', {
name: 'you',
skill: [
'angry',
'crying'
]
})
.then(r=> /*do what you want to do..*/)
setBulkLocalStorage([
{
key:'a',
value:'override this value'
},
{
key:'b',
value:'this one too'
}
])
.then(r=> /*do what you want to do..*/)
// { name: 'you', skill: ['angry', 'crying'] }
getLocalStorage('key')
.then(r=> /*do what you want to do..*/)
// # for cookie storage
import CookieStorage from '@itpolsri/simple-webstorage/lib/cookie'
// # for local storage
// import LocalStorage from 'simple-webstorage/lib/local'
// # for session storage
// import SessionStorage from 'simple-webstorage/lib/session'
const cookie = CookieStorage()
cookie
.set('remembered', true)
.then(r=> /*do what you want to do..*/)
cookie
.set('forgotten', true)
.then(r=> /*do what you want to do..*/)
cookie
.get('remembered')
.then(r=> /*do what you want to do..*/)
// ['remembered', 'forgotten'] # list all keys. returns array
cookie
.keys()
.then(r=> /*do what you want to do..*/)
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/polyfill.js"></script>
<script type="text/javascript" src="https://unpkg.com/@itpolsri/[email protected]/lib/bundle/simple-webstorage.min.js"></script>
<script type="text/javascript">
var storage = SimpleWebStorage();
storage
.asyncLocal
.set('key', 'value');
.then(r=> /*do what you want to do..*/)
storage
.asyncCookie
.set('key', 'value', 5);
.then(r=> /*do what you want to do..*/)
storage
.asyncSession
.set('key', 'value', 5);
.then(r=> /*do what you want to do..*/)
</script>
Storage | Method | Parameters |
---|---|---|
asyncLocal |
set | key (type: String), value (type: any, default: 0), expiryInMinutes (type: Number, default: null) |
asyncCookie or asyncSession or asyncLocal |
get | key (type: String) |
asyncCookie or asyncSession |
set | key (type: String), value (type: any, default: 0), expiryInMinutes (type: Number, default: 5) |
asyncCookie or asyncSession or asyncLocal |
remove | key (type: String) |
asyncCookie or asyncSession or asyncLocal |
keys | none |
asyncCookie or asyncSession or asyncLocal |
clear | none |
asyncCoookie or asyncSession or asyncLocal |
setBulk | arrOfData (type: array) |
Copyright © 2019 by IT-Polsri, soon will be IT-Us 🙂