1.0.1 • Published 5 years ago

@itpolsri/simple-webstorage v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Async Simple Web Storage

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

Support :

  • Local Storage
  • Cookie Storage
  • Session Storage

How using this package

1. Use Package with NPM

$ npm i @itpolsri/simple-webstorage

All API import

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'}]

Partial API import

// # 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..*/)
or you can import partial API like this :
// # 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..*/)

2. All in minified js

<script type="text/javascript" src="https://unpkg.com/babel-polyfill@6.26.0/dist/polyfill.js"></script>
<script type="text/javascript" src="https://unpkg.com/@itpolsri/simple-webstorage@1.0.1/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>

API Details

StorageMethodParameters
asyncLocalsetkey (type: String), value (type: any, default: 0), expiryInMinutes (type: Number, default: null)
asyncCookie or asyncSession or asyncLocalgetkey (type: String)
asyncCookie or asyncSessionsetkey (type: String), value (type: any, default: 0), expiryInMinutes (type: Number, default: 5)
asyncCookie or asyncSession or asyncLocalremovekey (type: String)
asyncCookie or asyncSession or asyncLocalkeysnone
asyncCookie or asyncSession or asyncLocalclearnone

Copyright © 2019 by IT-Polsri, soon will be IT-Us 🙂