From 22399df264023ecf4b8ff690f218d68cca7e4f77 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 28 Jan 2018 14:56:25 -0800 Subject: [PATCH] refactor LS Store --- routes/_store/LocalStorageStore.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/routes/_store/LocalStorageStore.js b/routes/_store/LocalStorageStore.js index ac404f51..38245cab 100644 --- a/routes/_store/LocalStorageStore.js +++ b/routes/_store/LocalStorageStore.js @@ -2,6 +2,10 @@ import { Store } from 'svelte/store' const LS = process.browser && localStorage +function safeParse(str) { + return !str ? undefined : (str === 'undefined' ? undefined : JSON.parse(str)) +} + export class LocalStorageStore extends Store { constructor(state, keysToWatch) { super(state) @@ -15,7 +19,7 @@ export class LocalStorageStore extends Store { let key = LS.key(i) if (key.startsWith('store_')) { let item = LS.getItem(key) - newState[key.substring(6)] = item === 'undefined' ? undefined : JSON.parse(item) + newState[key.substring(6)] = safeParse(item) } } this.set(newState)