JSPM

@toolz/local-storage-is-available

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 11
  • Score
    100M100P100Q29009F
  • License MIT

A simple utility function to safely determine whether localStorage is available in the current environment

Package Exports

  • @toolz/local-storage-is-available

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@toolz/local-storage-is-available) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

local-storage-is-available

local-storage-is-available is a tiny utility function to safely determine whether localStorage is available in the current environment. It's dangerous to assume that localStorage() is available. It can be absent in non-supported browsers, or when scripts are invoked from the command line, or when the user's browser is in Incognito Mode. Trying to access localStorage() when it's unavailable can spawn an Error. So it's best to test for its existence before attempting to access it.

Usage

After installation, import the package:

import { localStorageIsAvailable } from '@toolz/local-storage-is-available';

localStorageIsAvailable()

localStorageIsAvailable() attempts to set-and-remove a value in localStorage, returning a Boolean value based on its failure or success.

const API = {
   arguments: {
      // none
   },
   returns: Boolean,
}

Examples:

localStorageIsAvailable(); // returns Boolean TRUE in "normal" browser situations
localStorageIsAvailable(); // returns Boolean FALSE when invoked outside a "normal" browser
                           // or when the browser can localStorage disabled (e.g., Incognito Mode)