Lightweight utilities that can make easier to write and read application storage in client browser.
- Local Storage
- Cookie Storage
- Session Storage
# with npm
$ npm i simple-webstorage --save
# or with yarn
$ yarn add simple-webstorage
import SimpleWebStorage from 'simple-webstorage'
const storage = SimpleWebStorage()
storage.local.set('key', 'value') // empty the third parameter to store data permanently (only affected in local)
storage.cookie.set('key', 'value', 5)
storage.session.set('key', 'value', 5)
// # for local storage
import { get as getLocalStorage, set as setLocalStorage } from 'simple-webstorage/lib/local'
// # for session storage
// import { get, set } from 'simple-webstorage/lib/session'
// # for cookie storage
// import { get, set } from 'simple-webstorage/lib/cookie'
setLocalStorage('key', {
name: 'you',
skill: [
'angry',
'crying'
]
})
console.log(getLocalStorage('key')) // { name: 'you', skill: ['angry', 'crying'] }
// # for cookie storage
import CookieStorage from '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)
cookie.set('forgotten', true)
console.log(cookie.get('remembered')) // true # get values from key. returns any
console.log(cookie.keys()) // ['remembered', 'forgotten'] # list all keys. returns array
<script type="text/javascript" src="https://unpkg.com/simple-webstorage/lib/bundle/simple-webstorage.min.js"></script>
<script type="text/javascript">
var storage = SimpleWebStorage();
storage.local.set('key', 'value');
storage.cookie.set('key', 'value', 5);
storage.session.set('key', 'value', 5);
</script>
Storage | Method | Parameters |
---|---|---|
local |
set | key (type: String), value (type: any, default: 0), expiryInMinutes (type: Number, default: null) |
cookie or session |
set | key (type: String), value (type: any, default: 0), expiryInMinutes (type: Number, default: 5) |
local or cookie or session |
get | key (type: String) |
local or cookie or session |
remove | key (type: String) |
local or cookie or session |
keys | none |
local or cookie or session |
clear | none |
Feel free to contribute simple-webstorage 🙂
Copyright © 2019 by Sutan Gading Fadhillah Nasution