Merge branch 'position-float'
This commit is contained in:
@@ -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');
|
||||
}
|
||||
@@ -328,8 +337,12 @@ class MeasurementContainer {
|
||||
!this.measurements[this._currentType][this._currentVariant]) {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user