add checkMatchesObjectStructure utility function
This commit is contained in:
Vendored
+32
-2
@@ -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] }
|
||||
)
|
||||
);
|
||||
})();
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
Reference in New Issue
Block a user