refactor into methods finally

This commit is contained in:
Tyrel Souza 2023-10-20 14:18:31 -04:00
parent 61b2958b8b
commit 92937985f6
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
2 changed files with 139 additions and 106 deletions

View File

@ -1,29 +1,64 @@
const twoNewLines = /\r\n\r\n|\r\r|\n\n/; const TWO_NEW_LINES = /\r\n\r\n|\r\r|\n\n/;
const oneNewLine = /\r\n|\r|\n/; const ONE_NEW_LINE = /\r\n|\r|\n/;
const inRange = (index, value, masterValues) => { const inRange = (index, value, masterValues) => {
return ( return (masterValues[index]["Low Limit"] <= value && value <= masterValues[index]["High Limit"]);
masterValues[index]["Low Limit"] <= value && value <= masterValues[index]["High Limit"]
);
} }
const delta = (index, value, masterValues) => { const delta = (index, value, masterValues) => {
return Math.abs(masterValues[index]["Low Limit"] - value); return Math.abs(masterValues[index]["Low Limit"] - value);
} }
export default function parseTransducer(content, accuracy){ function processRemainingValues(key, transducerInfo, transducerType, val) {
accuracy = accuracy / 100.0; // Comes in as Percent // Toss anything else where it belongs
const transducerData = []; const [cleanKey, _] = key.split(/\W\d/);
if (cleanKey in transducerInfo || key.includes(`Instrument ${transducerType}`)) {
const value = parseInt(val.split(" ")[0]) * 1000;
// special case Master to get the limits
if (cleanKey.includes("Master")) {
transducerInfo[cleanKey].push({
"Low Limit": value - transducerInfo["Limit ABS"],
"Master Value": value,
"High Limit": value + transducerInfo["Limit ABS"],
});
}
// Split the content into sections based on the blank line // Turn both Instrument Pressure and Instrument Flow to Gauge Reading
const sections = content.trim().split(twoNewLines); else if (key.includes(`Instrument ${transducerType}`)) {
transducerInfo["Gauge Reading"].push(value);
} else {
transducerInfo[cleanKey].push(value);
}
}
}
for (const section of sections) { function extractInfo(filteredLines, transducerInfo, transducerType) {
// Split each section into lines // Extract other information for the transducer
const lines = section.trim().split(oneNewLine); for (const line of filteredLines) {
const filteredLines = lines.filter( const [key, val] = line.trim().split(/\s\s+/);
(line) => !line.startsWith("==") && line !== "|| Transducer Verify Report ||" if (key.includes("Verify Date")) {
); transducerInfo["Verify Date"] = val;
} else if (key.includes("Verify Time")) {
transducerInfo["Verify Time"] = val;
} else {
processRemainingValues(key, transducerInfo, transducerType, val);
}
}
}
function outOfTolerance(transducerInfo) {
// Calculate Out of Tolerances
for (const reading of transducerInfo["Gauge Reading"]) {
reading["Out Of Tolerance"] = 0;
if (!reading["In Range"]) {
reading["Out Of Tolerance"] = reading["Delta"];
}
}
}
function parseSection(section, accuracy) {
const lines = section.trim().split(ONE_NEW_LINE);
const filteredLines = lines.filter((line) => !line.startsWith("==") && line !== "|| Transducer Verify Report ||");
filteredLines.shift(); filteredLines.shift();
// Extract the Transducer number and Transducer type // Extract the Transducer number and Transducer type
@ -43,13 +78,15 @@ export default function parseTransducer(content, accuracy){
[, value, unit] = match; [, value, unit] = match;
value = parseInt(value); value = parseInt(value);
} }
// SCCM and LPM are Flow
if (unit === "SCCM" || unit === "LPM") { if (unit === "SCCM" || unit === "LPM") {
// SCCM and LPM are Flow
transducerType = "Flow"; transducerType = "Flow";
} } else if (unit === "PSIA" || unit === "PSID") {
// PSIA and PSID are pressure // PSIA and PSID are pressure
if (unit === "PSIA" || unit === "PSID") {
transducerType = "Pressure"; transducerType = "Pressure";
} else {
// Unknown Unit.
throw new Error(`Unknown Type of Test, do not know unit: ${unit}`)
} }
} }
@ -68,37 +105,7 @@ export default function parseTransducer(content, accuracy){
"Verify Time": "", "Verify Time": "",
}; };
// Extract other information for the transducer extractInfo(filteredLines, transducerInfo, transducerType);
for (const line of filteredLines) {
const [key, val] = line.trim().split(/\s\s+/);
if (key.includes("Verify Date")) {
transducerInfo["Verify Date"] = val;
} else if (key.includes("Verify Time")) {
transducerInfo["Verify Time"] = val;
} else {
// Toss anything else where it belongs
const [cleanKey, _] = key.split(/\W\d/);
if (cleanKey in transducerInfo || key.includes(`Instrument ${transducerType}`)) {
const value = parseInt(val.split(" ")[0]) * 1000;
// special case Master to get the limits
if (cleanKey.includes("Master")) {
const hi = value + transducerInfo["Limit ABS"];
const lo = value - transducerInfo["Limit ABS"];
transducerInfo[cleanKey].push({
"Low Limit": lo,
"Master Value": value,
"High Limit": hi,
});
}
// Turn both Instrument Pressure and Instrument Flow to Gauge Reading
else if (key.includes(`Instrument ${transducerType}`)) {
transducerInfo["Gauge Reading"].push(value);
} else {
transducerInfo[cleanKey].push(value);
}
}
}
}
// Once we have the readings and master values, we can do the math // Once we have the readings and master values, we can do the math
// Doing Map, so we can have the paired index between GaugeReading and Master Value // Doing Map, so we can have the paired index between GaugeReading and Master Value
@ -108,13 +115,23 @@ export default function parseTransducer(content, accuracy){
Delta: delta(idx, v, transducerInfo["Master Value"]), Delta: delta(idx, v, transducerInfo["Master Value"]),
})); }));
// Calculate Out of Tolerances outOfTolerance(transducerInfo);
for (const reading of transducerInfo["Gauge Reading"]) { return transducerInfo;
reading["Out Of Tolerance"] = 0; }
if (!reading["In Range"]) {
reading["Out Of Tolerance"] = reading["Delta"]; export default function parseTransducer(content, accuracy) {
} if (!content.includes("Transducer Verify Report")) {
throw new Error("Not a Transducer Verify Report")
} }
accuracy = accuracy / 100.0; // Comes in as Percent
const transducerData = [];
// Split the content into sections based on the blank line
const sections = content.trim().split(TWO_NEW_LINES);
for (const section of sections) {
// Split each section into lines
const transducerInfo = parseSection(section, accuracy);
transducerData.push(transducerInfo); transducerData.push(transducerInfo);
} }

View File

@ -57,3 +57,19 @@ describe("Testing actual calculations", () => {
} }
}) })
}); });
describe("Testing Errors", () => {
test("Not a Transducer Verify Report", () => {
const e = () => {
parseTransducer("I am a Fish", 0.05)
}
expect(e).toThrowError(Error("Not a Transducer Verify Report"))
})
test("Unknown Unit", () => {
const e = () => {
parseTransducer(`|| Transducer Verify Report ||\nTRANSDUCER1\n===============================================================\nTransducer 1 CTS D34-442 115FigNewtons`, 0);
}
expect(e).toThrowError(Error("Unknown Type of Test, do not know unit: FigNewtons"))
})
})