1.0.1 • Published 7 years ago
safe-localstorage v1.0.1
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 valueonLocalStorageNotAvailable
(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 atkey
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 valueonLocalStorageNotAvailable
(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