diff --git a/index.js b/index.js index dded213..54bc8de 100644 --- a/index.js +++ b/index.js @@ -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, diff --git a/src/measurements/MeasurementContainer.js b/src/measurements/MeasurementContainer.js index 20099f8..516e182 100644 --- a/src/measurements/MeasurementContainer.js +++ b/src/measurements/MeasurementContainer.js @@ -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;