Add distance float position handling with backward compatibility #1

Merged
p.vanderwilt merged 22 commits from :main into main 2025-09-26 11:41:53 +00:00
2 changed files with 50 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ const outputUtils = require('./src/helper/outputUtils.js');
const logger = require('./src/helper/logger.js');
const validation = require('./src/helper/validationUtils.js');
const configUtils = require('./src/helper/configUtils.js');
const assertions = require('./src/helper/assertionUtils.js')
// Domain-specific modules
const { MeasurementContainer } = require('./src/measurements/index.js');
@@ -34,6 +35,7 @@ module.exports = {
configUtils,
logger,
validation,
assertions,
MeasurementContainer,
nrmse,
state,

View File

@@ -88,11 +88,18 @@ class MeasurementContainer {
return this;
}
position(positionName) {
position(positionValue) {
if (!this._currentVariant) {
throw new Error('Variant must be specified before position');
}
this._currentPosition = positionName;
// Turn string positions into numeric values
if (typeof positionValue == "string") {
positionValue = this._convertPositionStr2Num(positionValue);
}
this._currentPosition = positionValue;
return this;
}
@@ -243,10 +250,12 @@ class MeasurementContainer {
const savedPosition = this._currentPosition;
// Get upstream and downstream measurements
this._currentPosition = 'upstream';
const positions = this.getPositions();
this._currentPosition = Math.min(...positions);
const upstream = this.get();
this._currentPosition = 'downstream';
this._currentPosition = Math.max(...positions);
const downstream = this.get();
this._currentPosition = savedPosition;
@@ -319,7 +328,7 @@ class MeasurementContainer {
Object.keys(this.measurements[this._currentType]) : [];
}
getPositions() {
getPositions(asNumber = false) {
if (!this._currentType || !this._currentVariant) {
throw new Error('Type and variant must be specified before listing positions');
}
@@ -329,7 +338,11 @@ class MeasurementContainer {
return [];
}
return Object.keys(this.measurements[this._currentType][this._currentVariant]);
if (asNumber) {
return Object.keys(this.measurements[this._currentType][this._currentVariant]);
}
return Object.keys(this.measurements[this._currentType][this._currentVariant]).map(this._convertPositionNum2Str);
}
clear() {
@@ -404,6 +417,34 @@ class MeasurementContainer {
}
}
_convertPositionStr2Num(positionString) {
switch(positionString) {
case "atEquipment":
return 0;
case "upstream":
return Number.POSITIVE_INFINITY;
case "downstream":
return Number.NEGATIVE_INFINITY;
default:
this.logger.error(`Invalid positionVsParent provided: ${positionString}`);
return;
}
}
_convertPositionNum2Str(positionValue) {
if (positionValue === 0) {
return "atEquipment";
}
if (positionValue < 0) {
return "upstream";
}
if (positionValue > 0) {
return "downstream";
}
this.logger.error(`Invalid position provided: ${positionValue}`);
}
}
module.exports = MeasurementContainer;