refactor into methods finally
This commit is contained in:
parent
61b2958b8b
commit
92937985f6
@ -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);
|
||||||
for (const section of sections) {
|
} else {
|
||||||
// Split each section into lines
|
transducerInfo[cleanKey].push(value);
|
||||||
const lines = section.trim().split(oneNewLine);
|
}
|
||||||
const filteredLines = lines.filter(
|
}
|
||||||
(line) => !line.startsWith("==") && line !== "|| Transducer Verify Report ||"
|
}
|
||||||
);
|
|
||||||
filteredLines.shift();
|
function extractInfo(filteredLines, transducerInfo, transducerType) {
|
||||||
|
// Extract other information for the transducer
|
||||||
// Extract the Transducer number and Transducer type
|
for (const line of filteredLines) {
|
||||||
const transducerLine = filteredLines.shift().trim();
|
const [key, val] = line.trim().split(/\s\s+/);
|
||||||
let [transducerName, partNumber] = transducerLine.split(/\s\s+/);
|
if (key.includes("Verify Date")) {
|
||||||
|
transducerInfo["Verify Date"] = val;
|
||||||
// Get part number and values
|
} else if (key.includes("Verify Time")) {
|
||||||
let value = null;
|
transducerInfo["Verify Time"] = val;
|
||||||
let unit = null;
|
} else {
|
||||||
let transducerType = null;
|
processRemainingValues(key, transducerInfo, transducerType, val);
|
||||||
if (partNumber !== "Custom") {
|
}
|
||||||
partNumber = partNumber.split(" ")
|
}
|
||||||
value = partNumber.pop();
|
}
|
||||||
partNumber = partNumber.join(" ");
|
|
||||||
const match = value.match(/([0-9]+)([A-Z]+)/i);
|
function outOfTolerance(transducerInfo) {
|
||||||
if (match) {
|
// Calculate Out of Tolerances
|
||||||
[, value, unit] = match;
|
for (const reading of transducerInfo["Gauge Reading"]) {
|
||||||
value = parseInt(value);
|
reading["Out Of Tolerance"] = 0;
|
||||||
}
|
if (!reading["In Range"]) {
|
||||||
// SCCM and LPM are Flow
|
reading["Out Of Tolerance"] = reading["Delta"];
|
||||||
if (unit === "SCCM" || unit === "LPM") {
|
}
|
||||||
transducerType = "Flow";
|
}
|
||||||
}
|
}
|
||||||
// PSIA and PSID are pressure
|
|
||||||
if (unit === "PSIA" || unit === "PSID") {
|
function parseSection(section, accuracy) {
|
||||||
transducerType = "Pressure";
|
const lines = section.trim().split(ONE_NEW_LINE);
|
||||||
}
|
const filteredLines = lines.filter((line) => !line.startsWith("==") && line !== "|| Transducer Verify Report ||");
|
||||||
}
|
filteredLines.shift();
|
||||||
|
|
||||||
// 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,
|
// Get part number and values
|
||||||
Unit: unit,
|
let value = null;
|
||||||
"Limit ABS": value * accuracy * 1000,
|
let unit = null;
|
||||||
"Transducer Name": transducerName,
|
let transducerType = null;
|
||||||
"Transducer Type": transducerType,
|
if (partNumber !== "Custom") {
|
||||||
"Gauge Reading": [],
|
partNumber = partNumber.split(" ")
|
||||||
"Master Value": [],
|
value = partNumber.pop();
|
||||||
"Verify Date": "",
|
partNumber = partNumber.join(" ");
|
||||||
"Verify Time": "",
|
const match = value.match(/([0-9]+)([A-Z]+)/i);
|
||||||
};
|
if (match) {
|
||||||
|
[, value, unit] = match;
|
||||||
// Extract other information for the transducer
|
value = parseInt(value);
|
||||||
for (const line of filteredLines) {
|
}
|
||||||
const [key, val] = line.trim().split(/\s\s+/);
|
if (unit === "SCCM" || unit === "LPM") {
|
||||||
if (key.includes("Verify Date")) {
|
// SCCM and LPM are Flow
|
||||||
transducerInfo["Verify Date"] = val;
|
transducerType = "Flow";
|
||||||
} else if (key.includes("Verify Time")) {
|
} else if (unit === "PSIA" || unit === "PSID") {
|
||||||
transducerInfo["Verify Time"] = val;
|
// PSIA and PSID are pressure
|
||||||
} else {
|
transducerType = "Pressure";
|
||||||
// Toss anything else where it belongs
|
} else {
|
||||||
const [cleanKey, _] = key.split(/\W\d/);
|
// Unknown Unit.
|
||||||
if (cleanKey in transducerInfo || key.includes(`Instrument ${transducerType}`)) {
|
throw new Error(`Unknown Type of Test, do not know unit: ${unit}`)
|
||||||
const value = parseInt(val.split(" ")[0]) * 1000;
|
}
|
||||||
// special case Master to get the limits
|
}
|
||||||
if (cleanKey.includes("Master")) {
|
|
||||||
const hi = value + transducerInfo["Limit ABS"];
|
// Create an object to store the data for each transducer
|
||||||
const lo = value - transducerInfo["Limit ABS"];
|
const transducerInfo = {
|
||||||
transducerInfo[cleanKey].push({
|
Accuracy: accuracy,
|
||||||
"Low Limit": lo,
|
"Part Number": partNumber,
|
||||||
"Master Value": value,
|
Value: value,
|
||||||
"High Limit": hi,
|
Unit: unit,
|
||||||
});
|
"Limit ABS": value * accuracy * 1000,
|
||||||
}
|
"Transducer Name": transducerName,
|
||||||
// Turn both Instrument Pressure and Instrument Flow to Gauge Reading
|
"Transducer Type": transducerType,
|
||||||
else if (key.includes(`Instrument ${transducerType}`)) {
|
"Gauge Reading": [],
|
||||||
transducerInfo["Gauge Reading"].push(value);
|
"Master Value": [],
|
||||||
} else {
|
"Verify Date": "",
|
||||||
transducerInfo[cleanKey].push(value);
|
"Verify Time": "",
|
||||||
}
|
};
|
||||||
}
|
|
||||||
}
|
extractInfo(filteredLines, transducerInfo, transducerType);
|
||||||
}
|
|
||||||
|
// 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
|
transducerInfo["Gauge Reading"] = transducerInfo["Gauge Reading"].map((v, idx) => ({
|
||||||
transducerInfo["Gauge Reading"] = transducerInfo["Gauge Reading"].map((v, idx) => ({
|
Value: v,
|
||||||
Value: v,
|
"In Range": inRange(idx, v, transducerInfo["Master Value"]),
|
||||||
"In Range": inRange(idx, v, transducerInfo["Master Value"]),
|
Delta: delta(idx, v, transducerInfo["Master Value"]),
|
||||||
Delta: delta(idx, v, transducerInfo["Master Value"]),
|
}));
|
||||||
}));
|
|
||||||
|
outOfTolerance(transducerInfo);
|
||||||
// Calculate Out of Tolerances
|
return transducerInfo;
|
||||||
for (const reading of transducerInfo["Gauge Reading"]) {
|
}
|
||||||
reading["Out Of Tolerance"] = 0;
|
|
||||||
if (!reading["In Range"]) {
|
export default function parseTransducer(content, accuracy) {
|
||||||
reading["Out Of Tolerance"] = reading["Delta"];
|
if (!content.includes("Transducer Verify Report")) {
|
||||||
}
|
throw new Error("Not a Transducer Verify Report")
|
||||||
}
|
}
|
||||||
|
accuracy = accuracy / 100.0; // Comes in as Percent
|
||||||
transducerData.push(transducerInfo);
|
const transducerData = [];
|
||||||
}
|
|
||||||
|
// Split the content into sections based on the blank line
|
||||||
return transducerData;
|
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;
|
||||||
}
|
}
|
@ -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