From 246b406e07c29d11fe30b5eb9dc53c78f9854807 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Thu, 24 Feb 2022 22:10:07 +0100 Subject: [PATCH] Fixed getItem() with a default value --- src/storage.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/storage.ts b/src/storage.ts index d846ac7..36cf7b1 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -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 }