GlobalStorage Utils
Utility library for managing global values
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.
How to install
Install @analytics/global-storage-utils from npm.
npm install @analytics/global-storage-utilsAPI
Below 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.
globalContext
Reference to the global object (self, global, or this depending on the environment).
import { globalContext } from '@analytics/global-storage-utils'
console.log(globalContext)get
Get a value from the global context by key.
import { get } from '@analytics/global-storage-utils'
const value = get('key')
console.log(value)set
Set a value on the global context by key. Returns the value that was set.
import { set } from '@analytics/global-storage-utils'
set('key', 'value')remove
Remove a value from the global context by key.
import { remove } from '@analytics/global-storage-utils'
remove('key')wrap
Wrap 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')hasSupport
Check 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')
}