Socket
Book a DemoInstallSign in
Socket

safe-localstorage

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safe-localstorage

localStorage wrapper that fails gracefully when it's not available, like in Safari private mode

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
216
-1.82%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

localstorage

FAQs

Package last updated on 31 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.