From 40a608af456f00f468b1db8dd497f26a4c027267 Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Mon, 22 Jul 2024 01:11:50 +0200 Subject: [PATCH] add checkMatchesObjectStructure utility function --- dist/index.js | 34 +++++++++++++++++++++++++++++++-- src/Model/Utility/typeSafety.ts | 25 ++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 3ffd50eb..d8139f5d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -281,6 +281,20 @@ function checkIsValidObject(object) { return object.dataVersion == DATA_VERSION; } + function checkMatchesObjectStructure(objectToCheck, reference) { + for (const key of Object.keys(reference)) { + const requiredType = typeof reference[key]; + const actualType = typeof objectToCheck[key]; + if (requiredType != actualType) return false; + if (requiredType != "object") continue; + const doesNestedObjectMatch = checkMatchesObjectStructure( + reference[key], + objectToCheck[key] + ); + if (doesNestedObjectMatch == false) return false; + } + return true; + } // src/Model/Utility/utility.ts function createTimestamp() { @@ -444,6 +458,7 @@ handleStringifiedFile = (stringifiedFile) => { const file = parseValidObject(stringifiedFile); if (file == null) return; + this.storeFile(file); }; // methods addFile = (file) => { @@ -461,6 +476,8 @@ file, fileContentName ); + const existingFileContent = this.storageModel.read(fileContentPath); + if (existingFileContent != null) return; const stringifiedContent = stringify(fileContent); this.storageModel.write(fileContentPath, stringifiedContent); }; @@ -500,6 +517,13 @@ const filePath = _FileModel.getFilePath(file.id); return [...filePath, fileContentName]; }; + static createFile = () => { + return { + dataVersion: DATA_VERSION, + id: v4_default(), + contentVersions: [] + }; + }; }; // src/Model/Utility/crypto.ts @@ -757,7 +781,7 @@ // utility static generateChatInfo = (primaryChannel) => { return { - dataVersion: "v2", + dataVersion: DATA_VERSION, primaryChannel, secondaryChannels: [], encryptionKey: "", @@ -766,7 +790,7 @@ }; static createChatMessage = async (channel, sender, encryptionKey, body, file) => { const chatMessage = { - dataVersion: "v2", + dataVersion: DATA_VERSION, id: v4_default(), channel, sender, @@ -2364,4 +2388,10 @@ ConnectionModal(connectionViewModel), StorageModal(storageViewModel) ); + console.log( + checkMatchesObjectStructure( + { a: 4263, b: "bla", d: true, c: [1, 2] }, + { a: 123, b: "hello", d: false, c: [1, 3] } + ) + ); })(); diff --git a/src/Model/Utility/typeSafety.ts b/src/Model/Utility/typeSafety.ts index c8906471..67d6d038 100644 --- a/src/Model/Utility/typeSafety.ts +++ b/src/Model/Utility/typeSafety.ts @@ -1,9 +1,30 @@ -export const DATA_VERSION = "v2" +export const DATA_VERSION = "v2"; export function checkIsValidObject(object: any): boolean { return object.dataVersion == DATA_VERSION; } +export function checkMatchesObjectStructure( + objectToCheck: any, + reference: any +): boolean { + for (const key of Object.keys(reference)) { + const requiredType = typeof reference[key]; + const actualType = typeof objectToCheck[key]; + if (requiredType != actualType) return false; + + // recurse into objects + if (requiredType != "object") continue; + const doesNestedObjectMatch = checkMatchesObjectStructure( + reference[key], + objectToCheck[key] + ); + if (doesNestedObjectMatch == false) return false; + } + + return true; +} + export interface ValidObject { dataVersion: "v2"; -} \ No newline at end of file +}