Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@analytics/storage-utils

DavidWells484.6kMIT0.4.2

Storage utility with fallbacks

analytics, analytics-project, analytics-util, storage, cookies, localStorage, sessionStorage, persistance

readme

Analytics Storage Utils

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

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

  1. Try localStorage
  2. If no localStorage, use cookies
  3. If no cookies, use sessionStorage
  4. If no sessionStorage, 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 sessionStorage */
setItem('keyThree', 'xyz', { storage: 'sessionStorage' })
// { value: "cookieVal", oldValue: "null", location: "sessionStorage" }

/* 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 to specifically sessionStorage
const getSessionStorageValue = getItem('key', { storage: 'sessionStorage' })

/* 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 sessionStorage */
removeItem('key', { storage: 'sessionStorage' })

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

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

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

0.4.2 (2023-05-27)

Note: Version bump only for package @analytics/storage-utils

0.4.1 (2023-05-27)

Note: Version bump only for package @analytics/storage-utils

0.4.0 (2022-03-18)

Features

  • export SESSION_STORAGE and hasSessionStorage fn (98affb6)

0.3.0 (2022-02-05)

Features

  • add sessionStorage support to storage utils (0b7373b)

0.2.17 (2022-01-03)

Note: Version bump only for package @analytics/storage-utils

0.2.16 (2022-01-02)

Note: Version bump only for package @analytics/storage-utils

0.2.15 (2021-12-12)

Note: Version bump only for package @analytics/storage-utils

0.2.14 (2021-10-24)

Note: Version bump only for package @analytics/storage-utils

0.2.13 (2021-10-17)

Note: Version bump only for package @analytics/storage-utils

0.2.12 (2021-08-05)

Note: Version bump only for package @analytics/storage-utils

0.2.11 (2021-08-05)

Note: Version bump only for package @analytics/storage-utils

0.2.10 (2021-07-31)

Note: Version bump only for package @analytics/storage-utils

0.2.9 (2021-07-28)

Note: Version bump only for package @analytics/storage-utils

0.2.8 (2021-07-26)

Note: Version bump only for package @analytics/storage-utils

0.2.7 (2021-07-20)

Note: Version bump only for package @analytics/storage-utils

0.2.6 (2021-07-20)

Note: Version bump only for package @analytics/storage-utils

0.2.5 (2021-03-11)

Note: Version bump only for package @analytics/storage-utils

0.2.4 (2020-07-14)

Note: Version bump only for package @analytics/storage-utils

0.2.3 (2020-04-16)

Note: Version bump only for package @analytics/storage-utils

0.2.2 (2019-10-03)

Note: Version bump only for package @analytics/storage-utils

0.2.0 (2019-10-01)

Features

  • storage: export cookie utils & add getItem(‘key’, ‘all') (3ed5b93)

0.1.1 (2019-10-01)

Note: Version bump only for package analytics-util-storage

0.1.0 (2019-09-23)

Features

  • storage: export cookie utils & add getItem(‘key’, ‘all') (3ed5b93)

0.0.6 (2019-09-10)

Note: Version bump only for package analytics-util-storage

0.0.5 (2019-09-10)

Note: Version bump only for package analytics-util-storage

0.0.4 (2019-08-14)

Note: Version bump only for package analytics-util-storage

0.0.3 (2019-08-14)

Note: Version bump only for package analytics-util-storage

0.0.2 (2019-08-14)

Note: Version bump only for package analytics-util-storage