add checkMatchesObjectStructure utility function

This commit is contained in:
marlon-erler
2024-07-22 01:11:50 +02:00
parent 42619fb017
commit 40a608af45
2 changed files with 55 additions and 4 deletions
+32 -2
View File
@@ -281,6 +281,20 @@
function checkIsValidObject(object) { function checkIsValidObject(object) {
return object.dataVersion == DATA_VERSION; 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 // src/Model/Utility/utility.ts
function createTimestamp() { function createTimestamp() {
@@ -444,6 +458,7 @@
handleStringifiedFile = (stringifiedFile) => { handleStringifiedFile = (stringifiedFile) => {
const file = parseValidObject(stringifiedFile); const file = parseValidObject(stringifiedFile);
if (file == null) return; if (file == null) return;
this.storeFile(file);
}; };
// methods // methods
addFile = (file) => { addFile = (file) => {
@@ -461,6 +476,8 @@
file, file,
fileContentName fileContentName
); );
const existingFileContent = this.storageModel.read(fileContentPath);
if (existingFileContent != null) return;
const stringifiedContent = stringify(fileContent); const stringifiedContent = stringify(fileContent);
this.storageModel.write(fileContentPath, stringifiedContent); this.storageModel.write(fileContentPath, stringifiedContent);
}; };
@@ -500,6 +517,13 @@
const filePath = _FileModel.getFilePath(file.id); const filePath = _FileModel.getFilePath(file.id);
return [...filePath, fileContentName]; return [...filePath, fileContentName];
}; };
static createFile = () => {
return {
dataVersion: DATA_VERSION,
id: v4_default(),
contentVersions: []
};
};
}; };
// src/Model/Utility/crypto.ts // src/Model/Utility/crypto.ts
@@ -757,7 +781,7 @@
// utility // utility
static generateChatInfo = (primaryChannel) => { static generateChatInfo = (primaryChannel) => {
return { return {
dataVersion: "v2", dataVersion: DATA_VERSION,
primaryChannel, primaryChannel,
secondaryChannels: [], secondaryChannels: [],
encryptionKey: "", encryptionKey: "",
@@ -766,7 +790,7 @@
}; };
static createChatMessage = async (channel, sender, encryptionKey, body, file) => { static createChatMessage = async (channel, sender, encryptionKey, body, file) => {
const chatMessage = { const chatMessage = {
dataVersion: "v2", dataVersion: DATA_VERSION,
id: v4_default(), id: v4_default(),
channel, channel,
sender, sender,
@@ -2364,4 +2388,10 @@
ConnectionModal(connectionViewModel), ConnectionModal(connectionViewModel),
StorageModal(storageViewModel) StorageModal(storageViewModel)
); );
console.log(
checkMatchesObjectStructure(
{ a: 4263, b: "bla", d: true, c: [1, 2] },
{ a: 123, b: "hello", d: false, c: [1, 3] }
)
);
})(); })();
+22 -1
View File
@@ -1,9 +1,30 @@
export const DATA_VERSION = "v2" export const DATA_VERSION = "v2";
export function checkIsValidObject(object: any): boolean { export function checkIsValidObject(object: any): boolean {
return object.dataVersion == DATA_VERSION; 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 { export interface ValidObject {
dataVersion: "v2"; dataVersion: "v2";
} }