From 83854206d69eac5859d3683ec30c27755c636a24 Mon Sep 17 00:00:00 2001 From: Andreas Nygren <91531258+andnygIPG@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:46:24 +0200 Subject: [PATCH 1/5] Exposed erg infofile validation function and added error handling if erg reader reads 0 quantities. --- index.ts | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 484b658..aa4a681 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,36 @@ import fs from "fs"; +function validateInfoHeader(infoFile: string) { + // read infofile + const info: string[] = fs + .readFileSync(infoFile, "utf8") + .replace(/\r/g, "") + .split("\n"); + + let validFormat = false; + let validInfofile = false; + + // check that first line of infofile is valid + if (info[0].startsWith("#INFOFILE1.1")) { + validInfofile = true; + } + + // Check that file format is erg + info.forEach((line) => { + if (line.startsWith("File.Format")) { + if (line.includes("erg")) { + validFormat = true; + } + } + }); + + // Return True if valid, false if invalid + if (validFormat && validInfofile) { + return true; + } else { + return false; + } +} // returns header information from info file function readInfoHeader(infoFile: string) { // read the info file @@ -75,6 +106,11 @@ function read(ergFile: string, infoFile: string) { // read info file let quants = readInfoQuants(infoFile); + // error if there are no quantities in erg infofile + if (quants.length < 1) { + throw new Error("ERG File does not contain any quantities"); + } + // read erg file let ergBuffer = fs.readFileSync(ergFile); @@ -213,5 +249,5 @@ function read(ergFile: string, infoFile: string) { return quants; } -export { read, readInfoHeader, readInfoQuants }; -export default { read, readInfoHeader, readInfoQuants }; +export { read, readInfoHeader, readInfoQuants, validateInfoHeader }; +export default { read, readInfoHeader, readInfoQuants, validateInfoHeader }; From 3e4806bf4304bcf76423775145381e055430cc4f Mon Sep 17 00:00:00 2001 From: Andreas Nygren <91531258+andnygIPG@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:59:22 +0200 Subject: [PATCH 2/5] Added jsdoc string to all functions. Added break to for loop when validating erg infofile. --- index.ts | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/index.ts b/index.ts index aa4a681..a5ac5e3 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,10 @@ import fs from "fs"; +/** + * Validates a CarMaker erg Infofile + * @param infoFile Path to CarMaker erg InfoFile + * @returns true if valid erg infofile, false if invalid + */ function validateInfoHeader(infoFile: string) { // read infofile const info: string[] = fs @@ -16,22 +21,21 @@ function validateInfoHeader(infoFile: string) { } // Check that file format is erg - info.forEach((line) => { - if (line.startsWith("File.Format")) { - if (line.includes("erg")) { - validFormat = true; - } + for (const line of info) { + if (line.startsWith("File.Format") && line.includes("erg")) { + validFormat = true; + break; } - }); - - // Return True if valid, false if invalid - if (validFormat && validInfofile) { - return true; - } else { - return false; } + + return validFormat && validInfofile; } -// returns header information from info file + +/** + * Read the erg infofile header. + * @param infoFile Path to CarMaker erg InfoFile. + * @returns Object containing erg file metadata. + */ function readInfoHeader(infoFile: string) { // read the info file const info: string[] = fs @@ -60,7 +64,11 @@ function readInfoHeader(infoFile: string) { return headerInfo; } -// returns quantity information from info file +/** + * Read list of quantities from erg infofile. + * @param infoFile Path to CarMaker erg InfoFile. + * @returns Array of objects containing erg file quantity names. + */ function readInfoQuants(infoFile: string) { // read the info file const info: string[] = fs @@ -100,8 +108,12 @@ function readInfoQuants(infoFile: string) { }); return quants; } - -// read erg and info file and return data values for each quantity +/** + * Read erg and info file and return data values for each quantity. + * @param ergFile Path to CarMaker erg file. + * @param infoFile Path to CarMaker erg InfoFile. + * @returns Array of CarMaker quantities + */ function read(ergFile: string, infoFile: string) { // read info file let quants = readInfoQuants(infoFile); From 687d9e473a075bd139b6d4e12820a991e9f9d35d Mon Sep 17 00:00:00 2001 From: Andreas Nygren <91531258+andnygIPG@users.noreply.github.com> Date: Mon, 2 Oct 2023 13:02:52 +0200 Subject: [PATCH 3/5] Update README.md to include validate function. --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 2061472..aa173fe 100644 --- a/README.md +++ b/README.md @@ -37,13 +37,26 @@ Read the data for all quantities ``` let data = erg.read(ergFile, infoFile); ``` + Read quantity definitions - no data values are returned, just the name and unit of each quantity ``` let quants = erg.readInfoQuants(infoFile); ``` + Read header information - Date, Testrun Name, CarMaker Version ``` let header = erg.readInfoHeader(infoFile); ``` + +Validate ERG InfoFile + +``` +if(erg.validateInfoHeader){ + console.log("Valid ERG InfoFile) +} +else{ + console.log("Invalid ERG InfoFile) +} +``` From 9b5ff1cecf636f4959a6409c29adf1d8f017404b Mon Sep 17 00:00:00 2001 From: Andreas Nygren <91531258+andnygIPG@users.noreply.github.com> Date: Mon, 2 Oct 2023 13:04:03 +0200 Subject: [PATCH 4/5] Fixed forgotten function argument in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aa173fe..4d71404 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ let header = erg.readInfoHeader(infoFile); Validate ERG InfoFile ``` -if(erg.validateInfoHeader){ +if(erg.validateInfoHeader(infoFile)){ console.log("Valid ERG InfoFile) } else{ From 740d73c982f36e31bc882b451a5807c2140d11bf Mon Sep 17 00:00:00 2001 From: Andreas Nygren <91531258+andnygIPG@users.noreply.github.com> Date: Mon, 2 Oct 2023 13:04:37 +0200 Subject: [PATCH 5/5] forgot semicolon in README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d71404..c94ed9b 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,9 @@ Validate ERG InfoFile ``` if(erg.validateInfoHeader(infoFile)){ - console.log("Valid ERG InfoFile) + console.log("Valid ERG InfoFile); } else{ - console.log("Invalid ERG InfoFile) + console.log("Invalid ERG InfoFile); } ```