项目作者: MatteoGabriele

项目描述 :
Local storage helper
高级语言: JavaScript
项目地址: git://github.com/MatteoGabriele/storage-helper.git
创建时间: 2016-10-21T19:46:08Z
项目社区:https://github.com/MatteoGabriele/storage-helper

开源协议:MIT License

下载


:warning: Sorry but this package is not longer maintained. With love, the guy who made the package.

Build Status npm version npm

storage-helper

A very simple way to manage browser storage.

If the browser doesn’t support localStorage, data will be saved in a cookie.
If the browser then doesn’t support cookies either (not a lucky day!), it will be saved in a plain object!
It also handles the QuotaExceeded error, silently logging it in the console and saving the data in the object.

Installation

  1. npm install storage-helper

Usage

  1. import storageHelper from 'storage-helper'
  2. const storageKey = 'foo'
  3. const value = 'bar'
  4. // set item
  5. storageHelper.setItem(storageKey, value)
  6. // set an item not permanently
  7. storageHelper.setItem(storageKey, value, false)
  8. // get item
  9. const foo = storageHelper.getItem(storageKey)
  10. // get item parsed
  11. const foo = storageHelper.getItem(storageKey, true)
  12. // get item with fallback value in case the item is undefined or null
  13. const foo = storageHelper.getItem(storageKey, false, 'foo')
  14. // remove item
  15. storageHelper.removeItem(storageKey)
  16. // remove all items
  17. storageHelper.clear()

It’s also possible to export single functions from the module

  1. import { clear } from 'storage-helper'
  2. clear()

The logger is by default set to false, so if you want to see logs during development,
you can just do like so

  1. import { showStorageLogger } from 'storage-helper'
  2. showStorageLogger(true)