Skip to content

Lightweight utilities that can make easier to access application storage in client browser.

License

Notifications You must be signed in to change notification settings

gadingnst/simple-webstorage

Repository files navigation

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

Support Me

Global

ko-fi

Indonesia


Feel free to contribute simple-webstorage 🙂

Copyright © 2019 by Sutan Gading Fadhillah Nasution