Socket
Socket
Sign inDemoInstall

@analytics/storage-utils

Package Overview
Dependencies
3
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @analytics/storage-utils

Storage utilities for saving values in browser


Version published
Weekly downloads
140K
increased by5%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Analytics Storage Utils

Storage utilities for analytics

Storage tries to use localStorage then cookies then defaults to global window.

It's also possible to specify exactly where to set, get, and remove data from with the options parameter.

getItem

import { getItem } from '@analytics/storage-utils'

// Default lookup, will try resolve value from `localStorage` -> `cookies` -> `global`
const value = getItem('key')

// Get value from specifically localStorage
const getLSValue = getItem('key', 'localStorage')

// Get value from specifically a cookie
const getLSValue = getItem('key', 'cookie')

// Get value from specifically the global window (or global this in Node)
const getLSValue = getItem('key', 'global')

// Get value from all locations
const valueObj = getItem('otherKey', 'all')
// {cookie: undefined, localStorage: "hahaha", global: null}

setItem

import { setItem } from '@analytics/storage-utils'

// Will try save value to `localStorage` -> `cookies` -> `global`
setItem('key', 'value')
// -> { value: "value", oldValue: "old", location: "localStorage" }

// Set value to specifically localStorage
setItem('key', 'otherValue', 'localStorage')
// -> { value: "otherValue", oldValue: "value", location: "localStorage" }

// Set value from specifically a cookie
setItem('keyTwo', 'cookieVal', 'cookie')
// -> { value: "cookieVal", oldValue: "null", location: "cookie" }

// Set value from specifically the global window (or global this in Node)
setItem('keyThree', 'xyz', 'global')
// -> { value: "xyz", oldValue: "foobar", location: "global" }

removeItem

import { removeItem } from '@analytics/storage-utils'

// Will try save value to `localStorage` -> `cookies` -> `global`
setItem('key')
// -> { location: "localStorage" }

// Set value to specifically localStorage
setItem('key', 'otherValue', 'localStorage')
// -> { location: "localStorage" }

// Set value from specifically a cookie
setItem('keyTwo', 'cookieVal', 'cookie')
// -> { value: "cookieVal", oldValue: "null", location: "cookie" }

// Set value from specifically the global window (or global this in Node)
setItem('keyThree', 'xyz', 'global')
// -> { value: "xyz", oldValue: "foobar", location: "global" }

Keywords

FAQs

Last updated on 01 Oct 2019

Did you know?

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc