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,123 +1,140 @@
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;
filteredLines.shift(); } else if (key.includes("Verify Time")) {
transducerInfo["Verify Time"] = val;
} else {
processRemainingValues(key, transducerInfo, transducerType, val);
}
}
}
// Extract the Transducer number and Transducer type function outOfTolerance(transducerInfo) {
const transducerLine = filteredLines.shift().trim(); // Calculate Out of Tolerances
let [transducerName, partNumber] = transducerLine.split(/\s\s+/); for (const reading of transducerInfo["Gauge Reading"]) {
reading["Out Of Tolerance"] = 0;
if (!reading["In Range"]) {
reading["Out Of Tolerance"] = reading["Delta"];
}
}
}
// Get part number and values function parseSection(section, accuracy) {
let value = null; const lines = section.trim().split(ONE_NEW_LINE);
let unit = null; const filteredLines = lines.filter((line) => !line.startsWith("==") && line !== "|| Transducer Verify Report ||");
let transducerType = null; filteredLines.shift();
if (partNumber !== "Custom") {
partNumber = partNumber.split(" ")
value = partNumber.pop();
partNumber = partNumber.join(" ");
const match = value.match(/([0-9]+)([A-Z]+)/i);
if (match) {
[, value, unit] = match;
value = parseInt(value);
}
// SCCM and LPM are Flow
if (unit === "SCCM" || unit === "LPM") {
transducerType = "Flow";
}
// PSIA and PSID are pressure
if (unit === "PSIA" || unit === "PSID") {
transducerType = "Pressure";
}
}
// Create an object to store the data for each transducer // Extract the Transducer number and Transducer type
const transducerInfo = { const transducerLine = filteredLines.shift().trim();
Accuracy: accuracy, let [transducerName, partNumber] = transducerLine.split(/\s\s+/);
"Part Number": partNumber,
Value: value,
Unit: unit,
"Limit ABS": value * accuracy * 1000,
"Transducer Name": transducerName,
"Transducer Type": transducerType,
"Gauge Reading": [],
"Master Value": [],
"Verify Date": "",
"Verify Time": "",
};
// Extract other information for the transducer // Get part number and values
for (const line of filteredLines) { let value = null;
const [key, val] = line.trim().split(/\s\s+/); let unit = null;
if (key.includes("Verify Date")) { let transducerType = null;
transducerInfo["Verify Date"] = val; if (partNumber !== "Custom") {
} else if (key.includes("Verify Time")) { partNumber = partNumber.split(" ")
transducerInfo["Verify Time"] = val; value = partNumber.pop();
} else { partNumber = partNumber.join(" ");
// Toss anything else where it belongs const match = value.match(/([0-9]+)([A-Z]+)/i);
const [cleanKey, _] = key.split(/\W\d/); if (match) {
if (cleanKey in transducerInfo || key.includes(`Instrument ${transducerType}`)) { [, value, unit] = match;
const value = parseInt(val.split(" ")[0]) * 1000; value = parseInt(value);
// special case Master to get the limits }
if (cleanKey.includes("Master")) { if (unit === "SCCM" || unit === "LPM") {
const hi = value + transducerInfo["Limit ABS"]; // SCCM and LPM are Flow
const lo = value - transducerInfo["Limit ABS"]; transducerType = "Flow";
transducerInfo[cleanKey].push({ } else if (unit === "PSIA" || unit === "PSID") {
"Low Limit": lo, // PSIA and PSID are pressure
"Master Value": value, transducerType = "Pressure";
"High Limit": hi, } else {
}); // Unknown Unit.
} throw new Error(`Unknown Type of Test, do not know unit: ${unit}`)
// 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 // Create an object to store the data for each transducer
// Doing Map, so we can have the paired index between GaugeReading and Master Value const transducerInfo = {
transducerInfo["Gauge Reading"] = transducerInfo["Gauge Reading"].map((v, idx) => ({ Accuracy: accuracy,
Value: v, "Part Number": partNumber,
"In Range": inRange(idx, v, transducerInfo["Master Value"]), Value: value,
Delta: delta(idx, v, transducerInfo["Master Value"]), Unit: unit,
})); "Limit ABS": value * accuracy * 1000,
"Transducer Name": transducerName,
"Transducer Type": transducerType,
"Gauge Reading": [],
"Master Value": [],
"Verify Date": "",
"Verify Time": "",
};
// Calculate Out of Tolerances extractInfo(filteredLines, transducerInfo, transducerType);
for (const reading of transducerInfo["Gauge Reading"]) {
reading["Out Of Tolerance"] = 0;
if (!reading["In Range"]) {
reading["Out Of Tolerance"] = reading["Delta"];
}
}
transducerData.push(transducerInfo); // 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
transducerInfo["Gauge Reading"] = transducerInfo["Gauge Reading"].map((v, idx) => ({
Value: v,
"In Range": inRange(idx, v, transducerInfo["Master Value"]),
Delta: delta(idx, v, transducerInfo["Master Value"]),
}));
return transducerData; outOfTolerance(transducerInfo);
return transducerInfo;
}
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);
}
return transducerData;
} }

View File

@ -56,4 +56,20 @@ describe("Testing actual calculations", () => {
expect(anyOOT).toBeTruthy(); expect(anyOOT).toBeTruthy();
} }
}) })
}); });
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"))
})
})