Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function setItem(key, value, options = {}) {
if (!key || !value) return false
const storageType = getStorageType(options)
const saveValue = JSON.stringify(value)
/* 1. Try localStorage */
if (useLocal(storageType)) {
// console.log('SET as localstorage', saveValue)
const oldValue = parse(localStorage.getItem(key))
localStorage.setItem(key, saveValue)
return { value, oldValue, location: LOCAL_STORAGE }
}
/* 2. Fallback to cookie */
if (useCookie(storageType)) {
// console.log('SET as cookie', saveValue)
const oldValue = parse(getCookie(key))
setCookie(key, saveValue)
return { value, oldValue, location: COOKIE }
}
/* 3. Fallback to window/global */
const oldValue = globalContext[key]
globalContext[key] = value
return { value, oldValue, location: GLOBAL }
}