apply checkMatchesObjectStructure in models

This commit is contained in:
marlon-erler
2024-07-22 01:38:08 +02:00
parent 3c8245fca0
commit d85ced4e36
5 changed files with 38 additions and 32 deletions
+9 -5
View File
@@ -1,9 +1,8 @@
// this file is responsible for reading and writing persistent data; all storage shall be handled by this file.
import { DATA_VERSION, ValidObject } from "./Utility/typeSafety";
import { DATA_VERSION, ValidObject, checkMatchesObjectStructure } from "./Utility/typeSafety";
import {
localeCompare,
parse,
parseValidObject,
stringify,
} from "./Utility/utility";
@@ -43,7 +42,7 @@ export default class StorageModel {
}
};
removeRecursive = (pathComponentsOfEntityToDelete: string[]): void => {
removeRecursively = (pathComponentsOfEntityToDelete: string[]): void => {
a: for (const key of Object.keys(localStorage)) {
const pathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key);
@@ -78,11 +77,16 @@ export default class StorageModel {
this.write(pathComponents, valueString);
};
readStringifiable = <T extends ValidObject>(pathComponents: string[]): T | null => {
readStringifiable = <T extends ValidObject>(pathComponents: string[], reference: T): T | null => {
const valueString: string | null = this.read(pathComponents);
if (!valueString) return null;
const object: T | null = parseValidObject(valueString);
const object: any | null = parseValidObject(valueString);
if (object == null) return null;
const doesMatchReference: boolean = checkMatchesObjectStructure(object, reference);
if (doesMatchReference == false) return null;
return object;
};