refactor into methods finally
This commit is contained in:
parent
61b2958b8b
commit
92937985f6
@ -1,29 +1,64 @@
|
||||
const twoNewLines = /\r\n\r\n|\r\r|\n\n/;
|
||||
const oneNewLine = /\r\n|\r|\n/;
|
||||
const TWO_NEW_LINES = /\r\n\r\n|\r\r|\n\n/;
|
||||
const ONE_NEW_LINE = /\r\n|\r|\n/;
|
||||
|
||||
const inRange = (index, value, masterValues) => {
|
||||
return (
|
||||
masterValues[index]["Low Limit"] <= value && value <= masterValues[index]["High Limit"]
|
||||
);
|
||||
return (masterValues[index]["Low Limit"] <= value && value <= masterValues[index]["High Limit"]);
|
||||
}
|
||||
|
||||
const delta = (index, value, masterValues) => {
|
||||
return Math.abs(masterValues[index]["Low Limit"] - value);
|
||||
}
|
||||
|
||||
export default function parseTransducer(content, accuracy){
|
||||
accuracy = accuracy / 100.0; // Comes in as Percent
|
||||
const transducerData = [];
|
||||
function processRemainingValues(key, transducerInfo, transducerType, val) {
|
||||
// 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")) {
|
||||
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
|
||||
const sections = content.trim().split(twoNewLines);
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const section of sections) {
|
||||
// Split each section into lines
|
||||
const lines = section.trim().split(oneNewLine);
|
||||
const filteredLines = lines.filter(
|
||||
(line) => !line.startsWith("==") && line !== "|| Transducer Verify Report ||"
|
||||
);
|
||||
function extractInfo(filteredLines, transducerInfo, transducerType) {
|
||||
// Extract other information for the transducer
|
||||
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 {
|
||||
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();
|
||||
|
||||
// Extract the Transducer number and Transducer type
|
||||
@ -43,13 +78,15 @@ export default function parseTransducer(content, accuracy){
|
||||
[, value, unit] = match;
|
||||
value = parseInt(value);
|
||||
}
|
||||
// SCCM and LPM are Flow
|
||||
if (unit === "SCCM" || unit === "LPM") {
|
||||
// SCCM and LPM are Flow
|
||||
transducerType = "Flow";
|
||||
}
|
||||
} else if (unit === "PSIA" || unit === "PSID") {
|
||||
// PSIA and PSID are pressure
|
||||
if (unit === "PSIA" || unit === "PSID") {
|
||||
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": "",
|
||||
};
|
||||
|
||||
// Extract other information for the transducer
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
extractInfo(filteredLines, transducerInfo, transducerType);
|
||||
|
||||
// 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
|
||||
@ -108,13 +115,23 @@ export default function parseTransducer(content, accuracy){
|
||||
Delta: delta(idx, v, transducerInfo["Master Value"]),
|
||||
}));
|
||||
|
||||
// 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"];
|
||||
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);
|
||||
}
|
||||
|
@ -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"))
|
||||
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user