UNPKG

@analytics/storage-utils

Version:
1 lines 10.3 kB
{"version":3,"file":"analytics-util-storage.cjs","sources":["../src/utils/parse.js","../src/index.js"],"sourcesContent":["import { isObject } from '@analytics/type-utils'\n/**\n * Safe JSON parse\n * @param {*} input - value to parse\n * @return {*} parsed input\n */\nexport default function parse(input) {\n let value = input\n try {\n value = JSON.parse(input)\n if (value === 'true') return true\n if (value === 'false') return false\n if (isObject(value)) return value\n if (parseFloat(value) === value) {\n value = parseFloat(value)\n }\n } catch (e) { }\n if (value === null || value === \"\") {\n return\n }\n return value\n}\n","import { set, get, remove, globalContext, GLOBAL } from '@analytics/global-storage-utils'\nimport { getCookie, setCookie, removeCookie, hasCookies, COOKIE } from '@analytics/cookie-utils'\nimport { hasLocalStorage, LOCAL_STORAGE } from '@analytics/localstorage-utils'\nimport { hasSessionStorage, SESSION_STORAGE } from '@analytics/session-storage-utils'\nimport { isUndefined, isString, ANY, ALL } from '@analytics/type-utils'\nimport parse from './utils/parse'\n\n// Verify support\nconst hasStorage = hasLocalStorage()\nconst hasSessionSupport = hasSessionStorage()\nconst hasCookiesSupport = hasCookies()\n\n/**\n * Get storage item from localStorage, cookie, or window\n * @param {string} key - key of item to get\n * @param {object|string} [options] - storage options. If string location of where to get storage\n * @param {string} [options.storage] - Define type of storage to pull from.\n * @return {Any} the value of key\n */\nexport function getItem(key, options) {\n if (!key) return\n const type = getStorageType(options)\n const getFirst = !useAll(type)\n\n /* 1. Try localStorage */\n const localValue = useLocal(type) ? parse(localStorage.getItem(key)) : undefined\n if (getFirst && !isUndefined(localValue)) {\n return localValue\n }\n\n /* 2. Fallback to cookie */\n const cookieVal = useCookie(type) ? parse(getCookie(key)) : undefined\n if (getFirst && cookieVal) {\n return cookieVal\n }\n\n /* 3. Fallback to sessionStorage */\n const sessionVal = useSession(type) ? parse(sessionStorage.getItem(key)) : undefined\n if (getFirst && sessionVal) {\n return sessionVal\n }\n\n /* 4. Fallback to window/global. */\n const globalValue = get(key)\n\n return getFirst ? globalValue : {\n localStorage: localValue,\n sessionStorage: sessionVal,\n cookie: cookieVal,\n global: globalValue\n }\n}\n\n/**\n * Store values in localStorage, cookie, or window\n * @param {string} key - key of item to set\n * @param {*} value - value of item to set\n * @param {object|string} [options] - storage options. If string location of where to get storage\n * @param {string} [options.storage] - Define type of storage to pull from.\n * @returns {object} returns old value, new values, & location of storage\n */\nexport function setItem(key, value, options) {\n if (!key || isUndefined(value)) {\n return\n }\n const data = {}\n const type = getStorageType(options)\n const saveValue = JSON.stringify(value)\n const setFirst = !useAll(type)\n\n /* 1. Try localStorage */\n if (useLocal(type)) {\n // console.log('SET as localstorage', saveValue)\n data[LOCAL_STORAGE] = format(LOCAL_STORAGE, value, parse(localStorage.getItem(key)))\n // Set LocalStorage item\n localStorage.setItem(key, saveValue)\n if (setFirst) {\n return data[LOCAL_STORAGE]\n }\n }\n\n /* 2. Fallback to cookie */\n if (useCookie(type)) {\n // console.log('SET as cookie', saveValue)\n data[COOKIE] = format(COOKIE, value, parse(getCookie(key)))\n // Set Cookie\n setCookie(key, saveValue)\n if (setFirst) {\n return data[COOKIE]\n }\n }\n\n /* 3. Try sessionStorage */\n if (useSession(type)) {\n // console.log('SET as localstorage', saveValue)\n data[SESSION_STORAGE] = format(SESSION_STORAGE, value, parse(sessionStorage.getItem(key)))\n // Set sessionStorage item\n sessionStorage.setItem(key, saveValue)\n if (setFirst) {\n return data[SESSION_STORAGE]\n }\n }\n\n /* 4. Fallback to window/global */\n data[GLOBAL] = format(GLOBAL, value, get(key))\n // Set global value\n set(key, value)\n // Return set value(s)\n return (setFirst) ? data[GLOBAL] : data\n}\n\n/**\n * Remove values from localStorage, cookie, or window\n * @param {string} key - key of item to set\n * @param {object|string} [options] - storage options. If string location of where to get storage\n * @param {string} [options.storage] - Define type of storage to pull from.\n */\nexport function removeItem(key, options) {\n if (!key) return\n const type = getStorageType(options)\n const values = getItem(key, ALL)\n\n const data = {}\n /* 1. Try localStorage */\n if (!isUndefined(values.localStorage) && useLocal(type)) {\n localStorage.removeItem(key)\n data[LOCAL_STORAGE] = values.localStorage\n }\n /* 2. Fallback to cookie */\n if (!isUndefined(values.cookie) && useCookie(type)) {\n removeCookie(key)\n data[COOKIE] = values.cookie\n }\n /* 3. Try sessionStorage */\n if (!isUndefined(values.sessionStorage) && useSession(type)) {\n sessionStorage.removeItem(key)\n data[SESSION_STORAGE] = values.sessionStorage\n }\n /* 4. Fallback to window/global */\n if (!isUndefined(values.global) && useGlobal(type)) {\n remove(key)\n data[GLOBAL] = values.global\n }\n return data\n}\n\nfunction getStorageType(opts) {\n if (!opts) return ANY\n return isString(opts) ? opts : opts.storage\n}\n\nfunction useGlobal(storage) {\n return useType(storage, GLOBAL)\n}\n\nfunction useLocal(storage) {\n // If has localStorage and storage option not defined, or is set to 'localStorage' or '*'\n return hasStorage && useType(storage, LOCAL_STORAGE)\n}\n\nfunction useCookie(storage) {\n // If has cookies and storage option not defined, or is set to 'cookies' or '*'\n return hasCookiesSupport && useType(storage, COOKIE)\n}\n\nfunction useSession(storage) {\n // If has sessionStorage and storage option not defined, or is set to 'sessionStorage' or '*'\n return hasSessionSupport && useType(storage, SESSION_STORAGE)\n}\n\nfunction useAll(storage) {\n return storage === ALL || storage === 'all'\n}\n\nfunction useType(storage, type) {\n return (storage === ANY || storage === type || useAll(storage))\n}\n\n/**\n * Format response\n * @param {string} location \n * @param {*} current - current value\n * @param {*} previous - previous value\n * @returns \n */\nfunction format(location, current, previous) {\n return { location, current, previous }\n}\n\n// const TYPES = {\n// ALL,\n// ANY,\n// GLOBAL,\n// COOKIE,\n// LOCAL_STORAGE,\n// SESSION_STORAGE, \n// }\n\nexport {\n ALL,\n ANY,\n GLOBAL,\n COOKIE,\n LOCAL_STORAGE,\n SESSION_STORAGE,\n getCookie,\n setCookie,\n removeCookie,\n globalContext,\n hasSessionStorage,\n hasLocalStorage,\n hasCookies\n}\n\nexport default {\n setItem,\n getItem,\n removeItem\n}"],"names":["parse","input","value","JSON","isObject","parseFloat","e","hasStorage","hasLocalStorage","hasSessionSupport","hasSessionStorage","hasCookiesSupport","hasCookies","getItem","key","options","type","getStorageType","getFirst","useAll","localValue","useLocal","localStorage","undefined","isUndefined","cookieVal","useCookie","getCookie","sessionVal","useSession","sessionStorage","globalValue","get","cookie","global","setItem","data","saveValue","stringify","setFirst","LOCAL_STORAGE","format","COOKIE","setCookie","SESSION_STORAGE","GLOBAL","set","removeItem","values","ALL","removeCookie","useType","remove","opts","isString","storage","ANY","location","current","previous"],"mappings":"kNAMe,SAASA,EAAMC,GAC5B,IAAIC,EAAQD,EACZ,IAEE,GAAc,UADdC,EAAQC,KAAKH,MAAMC,IACG,OAAO,EAC7B,GAAc,UAAVC,EAAmB,OAAY,EACnC,GAAIE,EAAQA,SAACF,GAAQ,OAAOA,EACxBG,WAAWH,KAAWA,IACxBA,EAAQG,WAAWH,GAEvB,CAAE,MAAOI,GACT,CAAA,GAAc,OAAVJ,GAA4B,KAAVA,EAGtB,OAAOA,CACT,CCbA,IAAMK,EAAaC,EAAAA,kBACbC,EAAoBC,EAAAA,oBACpBC,EAAoBC,wBASVC,EAAQC,EAAKC,GAC3B,GAAKD,EAAL,CACA,IAAME,EAAOC,EAAeF,GACtBG,GAAYC,EAAOH,GAGnBI,EAAaC,EAASL,GAAQhB,EAAMsB,aAAaT,QAAQC,SAAQS,EACvE,GAAIL,IAAaM,EAAWA,YAACJ,GAC3B,OAAOA,EAIT,IAAMK,EAAYC,EAAUV,GAAQhB,EAAM2B,YAAUb,SAAQS,EAC5D,GAAIL,GAAYO,EACd,OAAOA,EAIT,IAAMG,EAAaC,EAAWb,GAAQhB,EAAM8B,eAAejB,QAAQC,SAAQS,EAC3E,GAAIL,GAAYU,EACd,OAAOA,EAIT,IAAMG,EAAcC,EAAGA,IAAClB,GAExB,OAAOI,EAAWa,EAAc,CAC9BT,aAAcF,EACdU,eAAgBF,EAChBK,OAAQR,EACRS,OAAQH,EA5BV,CA8BF,CAUO,SAASI,EAAQrB,EAAKZ,EAAO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