From 216904fd6ecc8467d5d6647eff2a5bc6cc3a5faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefano=20Borz=C3=AC?= Date: Fri, 6 May 2022 20:24:22 +0200 Subject: [PATCH] chore: minor improvements in for cycle (#18) --- src/armory/Armory.ts | 6 ++---- src/armory/Config.ts | 14 +++++--------- src/tools/fetchdata.ts | 26 ++++++++++---------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/armory/Armory.ts b/src/armory/Armory.ts index 7f457da..7020b8c 100644 --- a/src/armory/Armory.ts +++ b/src/armory/Armory.ts @@ -85,10 +85,8 @@ export class Armory { websiteRoot: this.config.websiteRoot, iframeMode: this.config.iframeMode, }; - for (const key in locals) { - if (locals.hasOwnProperty(key)) { - app.locals[key] = locals[key]; - } + for (const key of Object.keys(locals)) { + app.locals[key] = locals[key]; } app.locals.locals = locals; diff --git a/src/armory/Config.ts b/src/armory/Config.ts index c3af755..170c638 100644 --- a/src/armory/Config.ts +++ b/src/armory/Config.ts @@ -115,7 +115,7 @@ export class Config { } else if (typeof model === "object") { const obj = {}; Config.loadObjFromEnv(logger, obj, model, parentName + i); - if (Object.keys(obj).length > 0) { + if (Object.keys(obj).length) { arr.push(obj); } } else if (process.env.hasOwnProperty(key)) { @@ -161,7 +161,7 @@ export class Config { for (const field of missing) { logger.warn(`Field ${parentName}${field} is missing from config.json!`); } - for (const key in model) { + for (const key of Object.keys(model)) { if (typeof model[key] === "object" && obj.hasOwnProperty(key)) { Config.checkAllMissingFields(logger, obj[key], model[key], parentName + key); } @@ -169,12 +169,8 @@ export class Config { } private static hasMissingFields(obj: object, model: object): string[] { - const missing = []; - for (const key in model) { - if (!obj.hasOwnProperty(key)) { - missing.push(key); - } - } - return missing; + const objProp = Object.keys(obj); + const missingProps = Object.keys(model).filter((key) => !objProp.includes(key)); + return missingProps; } } diff --git a/src/tools/fetchdata.ts b/src/tools/fetchdata.ts index 10335c5..e32fa40 100644 --- a/src/tools/fetchdata.ts +++ b/src/tools/fetchdata.ts @@ -125,21 +125,17 @@ async function download(dir: string, file: string): Promise { function queueTexturesAndModels(item: any): void { if (item.TextureFiles !== null) { - for (const key in item.TextureFiles) { - for (const file of item.TextureFiles[key]) { - if (file.FileDataId !== 0) { - texturesDownloadQueue.add(file.FileDataId); - } + for (const file in Object.values(item.TextureFiles)) { + if (file["FileDataId"] !== 0) { + texturesDownloadQueue.add(file["FileDataId"]); } } } if (item.ModelFiles !== null) { - for (const key in item.ModelFiles) { - for (const file of item.ModelFiles[key]) { - if (file.FileDataId !== 0) { - modelsDownloadQueue.add(file.FileDataId); - } + for (const file of Object.values(item.ModelFiles)) { + if (file["FileDataId"] !== 0) { + modelsDownloadQueue.add(file["FileDataId"]); } } } @@ -149,7 +145,7 @@ function queueTexturesAndModels(item: any): void { } if (item.Textures !== null) { - for (const key in item.Textures) { + for (const key of Object.keys(item.Textures)) { if (item.Textures[key] !== 0) { texturesDownloadQueue.add(item.Textures[key]); } @@ -157,7 +153,7 @@ function queueTexturesAndModels(item: any): void { } if (item.Textures2 !== null) { - for (const key in item.Textures2) { + for (const key of Object.keys(item.Textures2)) { if (item.Textures2[key] !== 0) { texturesDownloadQueue.add(item.Textures2[key]); } @@ -196,11 +192,9 @@ async function downloadRaces(): Promise { } } - const textureFiles = Object.keys(customizationJson.TextureFiles) - .map((key) => customizationJson.TextureFiles[key]) - .flat(); + const textureFiles = Object.values(customizationJson.TextureFiles).flat(); for (const file of textureFiles) { - texturesDownloadQueue.add(file.FileDataId); + texturesDownloadQueue.add(file["FileDataId"]); } progress.increment();