Fixed getItem() with a default value

This commit is contained in:
Simon Cambier 2022-02-24 22:10:07 +01:00
parent 3b9655f6fe
commit 246b406e07

View File

@ -4,10 +4,9 @@ export function setItem(k: string, v: string): void {
export function getItem(k: string): string | null
export function getItem(k: string, defaultValue: any): string
export function getItem(k: string, defaultValue?: any): string | null {
try {
return localStorage.getItem(k) ?? null
}
catch (e) {
return defaultValue ?? null
const val = localStorage.getItem(k)
if (defaultValue !== undefined && val === null) {
return defaultValue
}
return val
}