@analytics/global-storage-utils — Tiny global storage utility library. One of the small, focused utility packages that power the analytics library, usable standalone.
Full documentation, synced from the package README:
A tiny window storage util library in 413 bytes.
Exposes globalContext, get, set, remove, wrap, & hasSupport functions.
This library will work with analytics or as a standalone import in your code.
Install @analytics/global-storage-utils from npm.
npm install @analytics/global-storage-utilsBelow is the api for @analytics/global-storage-utils. You can import only what you need & the rest will be tree-shaken out of your bundle.
globalContextReference to the global object (self, global, or this depending on the environment).
import { globalContext } from '@analytics/global-storage-utils'
console.log(globalContext)getGet a value from the global context by key.
import { get } from '@analytics/global-storage-utils'
const value = get('key')
console.log(value)setSet a value on the global context by key. Returns the value that was set.
import { set } from '@analytics/global-storage-utils'
set('key', 'value')removeRemove a value from the global context by key.
import { remove } from '@analytics/global-storage-utils'
remove('key')wrapWrap a localStorage or sessionStorage operation, falling back to a provided function when the storage type is unsupported.
import { wrap } from '@analytics/global-storage-utils'
const getItem = wrap('localStorage', 'getItem', () => null)
const value = getItem('key')hasSupportCheck whether a given storage type (localStorage or sessionStorage) is supported. Results are cached per type.
import { hasSupport } from '@analytics/global-storage-utils'
if (hasSupport('localStorage')) {
console.log('localStorage is available')
}