analytics-util-storage

0.1.1 • Public • Published

Analytics Storage Utils

Note: This package has moved to @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" }

/analytics-util-storage/

    Package Sidebar

    Install

    npm i analytics-util-storage

    Weekly Downloads

    2

    Version

    0.1.1

    License

    MIT

    Unpacked Size

    87.8 kB

    Total Files

    10

    Last publish

    Collaborators

    • davidwells