Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@analytics/storage-utils

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@analytics/storage-utils

Storage utility with fallbacks

  • 0.2.15
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
66K
decreased by-56.36%
Maintainers
1
Weekly downloads
 
Created
Source

Analytics Storage Utils

A tiny storage utility library with fallback mechanism in 904 bytes.

By default, @analytics/storage-utils will persist values in browser in this order:

  1. localStorage
  2. If no localStorage, use cookies
  3. If no cookies, use global window

If you want to specify which storage mechanism to use, use the options parameter.

See live demo.

setItem

Set a value.

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

/** 
* Basic usage 
*/

/* Save value to `localStorage` or `cookies` or `global` */
setItem('key', 'value')
// { value: "value", oldValue: "old", location: "localStorage" }

/** Setting values to specific location */

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

/* Set value to specifically cookie */
setItem('keyTwo', 'cookieVal',  { storage: 'cookie' })
// { value: "cookieVal", oldValue: "null", location: "cookie" }

/* Set value from specifically the global window (or global this in node.js) */
setItem('keyThree', 'xyz', { storage: 'global' })
// { value: "cookieVal", oldValue: "null", location: "cookie" }

getItem

Get a value.

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

/* Basic usage */

/* Lookup value from `localStorage` or `cookies` or `global` */
const value = getItem('key')

/** 
 * Getting values to specific locations
 */

// Get value to specifically localStorage
const getLocalStorageValue = getItem('key', { storage: 'localStorage' })

/* Get value to specifically cookie */
const getCookieValue = getItem('key', { storage: 'cookie' })

/* Get value from specifically the global window (or global this in node.js) */
const getGlobalValue = getItem('key', { storage: 'global' })

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

removeItem

Remote a value.

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

/* Basic usage */

// Will try remove value from `localStorage` -> `cookies` -> `global`
removeItem('key')

/** Removing values to specific locations */

/* Remove value to specifically localStorage */
removeItem('key', { storage: 'localStorage' })

/* Remove value to specifically cookie */
removeItem('keyTwo', { storage: 'cookie' })

/* Remove value to specifically global */
removeItem('keyThree', { storage: 'global' })

/* Remove value from all locations */
removeItem('otherKey', { storage: '*' })

Keywords

FAQs

Package last updated on 12 Dec 2021

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

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc