safe-localstorage

1.0.1 • Public • Published

Safe localStorage

A window.localStorage wrapper that will fail gracefully

Why?

Safari Private Browsing

In Safari's Private Browsing mode, attempting to store data in localStorage will throw an error. safeLocalStorage checks that window.localStorage is safe to use. Added bonus - you can pass its methods an arbitrary callback to be fired if localStorage is unavailable.

Really old browsers

The localStorage API is available in all modern browsers, going back to IE 8. Using this library will make you safe in IE 6 and 7.

Installation

npm install --save safe-localstorage

Usage

import safeLocalStorage from 'safe-localstorage';
 
safeLocalStorage.set('myKey', 'myValue', () => { console.warn('unable to use localStorage'); })
safeLocalStorage.get('myKey');

API

get(key[, onLocalStorageNotAvailable])

Wrapper for localStorage.getItem

  • key (required): key string at which to get value
  • onLocalStorageNotAvailable (optional): callback to be fired if localStorage is undefined or not safe to use

set(key, value[, onLocalStorageNotAvailable])

Wrapper for localStorage.setItem

  • key (required): key string to update (will be created if it doesn't exist)
  • value (required): string value to be stored at key
  • onLocalStorageNotAvailable (optional): callback to be fired if localStorage is undefined or not safe to use

remove(key[, onLocalStorageNotAvailable])

Wrapper for localStorage.removeItem

  • key (required): key string at which to delete the current value
  • onLocalStorageNotAvailable (optional): callback to be fired if localStorage is undefined or not safe to use

removeAll([onLocalStorageNotAvailable])

Wrapper for localStorage.clear

  • onLocalStorageNotAvailable (optional): callback to be fired if localStorage is undefined or not safe to use

Package Sidebar

Install

npm i safe-localstorage

Weekly Downloads

45

Version

1.0.1

License

MIT

Last publish

Collaborators

  • andrewdushane