Created
December 23, 2019 21:51
-
-
Save nathansmith/c3597a5a955a80e9327e3f4305f18681 to your computer and use it in GitHub Desktop.
Helper for working with `window.localStorage`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================ | |
// Flag for `localStorage` support. | |
// ================================ | |
const flag = (() => { | |
// Assume no support. | |
let bool = false; | |
// Used in the `try`. | |
const key = 'TEST_KEY'; | |
const val = 'TEST_VAL'; | |
// Rigorously test for support. | |
try { | |
// Set the key/value. | |
window.localStorage.setItem(key, val); | |
// Are we able to read it? | |
bool = window.localStorage.getItem(key) === val; | |
// Delete the key/value. | |
window.localStorage.removeItem(key); | |
} catch (e) { | |
bool = false; | |
} | |
// Return the boolean. | |
return bool; | |
})(); | |
// ======================= | |
// Cache: Alias to object. | |
// ======================= | |
const hasProp = Object.prototype.hasOwnProperty; | |
const cacheFallback = { | |
clear: function() { | |
for (const key in cache) { | |
if (key !== 'clear' && hasProp.call(cache, key)) { | |
delete cache[key]; | |
} | |
} | |
} | |
}; | |
const cache = flag ? window.localStorage : cacheFallback; | |
// ======================= | |
// Cache: Clear key/value. | |
// ======================= | |
const clear = () => { | |
cache.clear(); | |
}; | |
// ============== | |
// Cache: Getter. | |
// ============== | |
const get = key => { | |
let data = cache[key]; | |
// Exit, if no data. | |
if (!data) { | |
return; | |
} | |
try { | |
// Attempt to parse. | |
data = JSON.parse(data); | |
} catch (e) { | |
// Set to original. | |
data = cache[key]; | |
} | |
return data; | |
}; | |
// ============== | |
// Cache: Setter. | |
// ============== | |
const set = (key, data) => { | |
if ( | |
key === 'clear' || | |
key === 'getItem' || | |
key === 'key' || | |
key === 'length' || | |
key === 'removeItem' || | |
key === 'setItem' | |
) { | |
throw new Error(`Cannot overwrite method: window.localStorage.${key}`); | |
} | |
if (typeof data === 'object') { | |
data = JSON.stringify(data); | |
} | |
cache[key] = data; | |
}; | |
// ========================== | |
// Cache: Remove single item. | |
// ========================== | |
const remove = key => { | |
delete cache[key]; | |
}; | |
// ============== | |
// Export object. | |
// ============== | |
const storage = { | |
clear, | |
get, | |
remove, | |
set | |
}; | |
// Expose methods. | |
export { storage }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment