Skip to content
Lightweight utilities that can make easier to write and read application storage in client browser.
Branch: master
Clone or download
Latest commit acd1e49 Mar 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src modified base storage.js Mar 22, 2019
tests version 1.4.2 Mar 22, 2019
.gitignore
.travis.yml
LICENSE add license Mar 14, 2019
README.md
_config.yml Set theme jekyll-theme-hacker Mar 22, 2019
jest.config.js add badges and resetup travis Mar 22, 2019
package.json modified badges Mar 22, 2019
webpack.config.js

README.md

Simple Web Storage

GitHub Codacy Badge Build Status npm npm npm David GitHub issues Website

Lightweight utilities that can make easier to write and read application storage in client browser.

Support :

  • Local Storage
  • Cookie Storage
  • Session Storage

HOW TO USE

1. Use Package with NPM or YARN

# with npm
$ npm i simple-webstorage --save

# or with yarn
$ yarn add simple-webstorage

All API import

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)

Partial API import

// # 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'] }
or you can import partial API like this :
// # 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

2. All in minified js

<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>

API Details

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

You can’t perform that action at this time.