From 536569b6ad715eea7d15c23d09aa1828c504b27e Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Fri, 19 Jul 2024 18:26:04 +0200 Subject: [PATCH] add storageModel methods --- src/Model/storageModel.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Model/storageModel.ts b/src/Model/storageModel.ts index 39360f95..434c79bd 100644 --- a/src/Model/storageModel.ts +++ b/src/Model/storageModel.ts @@ -1 +1,28 @@ -// this file is responsible for reading and writing persistent data; all storage shall be handled by this file. \ No newline at end of file +// this file is responsible for reading and writing persistent data; all storage shall be handled by this file. + +import { parse, stringify } from "../utility"; + +// basic +export function store(key: string, value: string): void { + localStorage.setItem(key, value); +} + +export function restore(key: string): string | null { + return localStorage.getItem(key); +} + +// array +export function storeArray(key: string, value: any[]): void { + const valueString = stringify(value); + store(key, valueString); +} + +export function restoreArray(key: string): any[] | null { + const valueString = restore(key); + if (!valueString) return null; + + const parsed = parse(valueString); + if (!Array.isArray(parsed)) return null; + + return parsed; +} \ No newline at end of file