From cb52222de5a65fb4226f0adf1947363767b6d118 Mon Sep 17 00:00:00 2001 From: Syed Ghufran Hassan Date: Fri, 3 Jan 2025 16:28:39 +0500 Subject: [PATCH] Update startup.js Check for localStorage availability and invalid JSON edge case added --- startup.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/startup.js b/startup.js index f9d58a6..dcee327 100644 --- a/startup.js +++ b/startup.js @@ -61,7 +61,7 @@ // DOM Manipulation // ====================== - // 6. Simple DOM selector like jQuery's $ + // 6. Simple DOM selector like jQuery's startup.$ = function (selector) { return document.querySelector(selector); }; @@ -137,12 +137,18 @@ }; // 13. Get a value from local storage - startup.getLocalStorage = function (key) { - if (key) { - return JSON.parse(localStorage.getItem(key)); - } + startup.getLocalStorage = function (key) { + if (!key || typeof key !== 'string') { + console.warn('Invalid key for localStorage'); return null; - }; + } + try { + return JSON.parse(localStorage.getItem(key)); + } catch (e) { + console.error('Failed to parse JSON from localStorage for key:', key, e); + return null; + } +}; // 14. Remove a value from local storage startup.removeLocalStorage = function (key) {