From 5117df14db0b72b74e70e33e6b7ad126de18df9f Mon Sep 17 00:00:00 2001 From: "p.vanderwilt" Date: Fri, 3 Oct 2025 15:55:20 +0200 Subject: [PATCH] Fix number comparisons for position values --- src/measurements/MeasurementBuilder.js | 2 +- src/measurements/MeasurementContainer.js | 96 ++++++++++++------------ 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/measurements/MeasurementBuilder.js b/src/measurements/MeasurementBuilder.js index bead9e3..55b818a 100644 --- a/src/measurements/MeasurementBuilder.js +++ b/src/measurements/MeasurementBuilder.js @@ -40,7 +40,7 @@ class MeasurementBuilder { if (!this.variant) { throw new Error('Measurement variant is required'); } - if (!this.position) { + if (Number.isNaN(this.position)) { throw new Error('Measurement position is required'); } diff --git a/src/measurements/MeasurementContainer.js b/src/measurements/MeasurementContainer.js index 135729a..32b8396 100644 --- a/src/measurements/MeasurementContainer.js +++ b/src/measurements/MeasurementContainer.js @@ -105,60 +105,60 @@ class MeasurementContainer { // ENHANCED: Update your existing value method value(val, timestamp = Date.now(), sourceUnit = null) { - if (!this._ensureChainIsValid()) return this; - - const measurement = this._getOrCreateMeasurement(); - const targetUnit = this._getTargetUnit(this._currentType); - - let convertedValue = val; - let finalUnit = sourceUnit || targetUnit; + if (!this._ensureChainIsValid()) return this; + + const measurement = this._getOrCreateMeasurement(); + const targetUnit = this._getTargetUnit(this._currentType); + + let convertedValue = val; + let finalUnit = sourceUnit || targetUnit; - // Auto-convert if enabled and units are specified - if (this.autoConvert && sourceUnit && targetUnit && sourceUnit !== targetUnit) { - try { - convertedValue = convertModule(val).from(sourceUnit).to(targetUnit); - finalUnit = targetUnit; - - if (this.logger) { - this.logger.debug(`Auto-converted ${val} ${sourceUnit} to ${convertedValue} ${targetUnit}`); - } - } catch (error) { - if (this.logger) { - this.logger.warn(`Auto-conversion failed from ${sourceUnit} to ${targetUnit}: ${error.message}`); - } - convertedValue = val; - finalUnit = sourceUnit; + // Auto-convert if enabled and units are specified + if (this.autoConvert && sourceUnit && targetUnit && sourceUnit !== targetUnit) { + try { + convertedValue = convertModule(val).from(sourceUnit).to(targetUnit); + finalUnit = targetUnit; + + if (this.logger) { + this.logger.debug(`Auto-converted ${val} ${sourceUnit} to ${convertedValue} ${targetUnit}`); } + } catch (error) { + if (this.logger) { + this.logger.warn(`Auto-conversion failed from ${sourceUnit} to ${targetUnit}: ${error.message}`); + } + convertedValue = val; + finalUnit = sourceUnit; } + } - measurement.setValue(convertedValue, timestamp); - - if (finalUnit && !measurement.unit) { - measurement.setUnit(finalUnit); - } + measurement.setValue(convertedValue, timestamp); + + if (finalUnit && !measurement.unit) { + measurement.setUnit(finalUnit); + } - // ENHANCED: Emit event with rich context - const eventData = { - value: convertedValue, - originalValue: val, - unit: finalUnit, - sourceUnit: sourceUnit, - timestamp, - position: this._currentPosition, - variant: this._currentVariant, - type: this._currentType, - // NEW: Enhanced context - childId: this.childId, - childName: this.childName, - parentRef: this.parentRef - }; + // ENHANCED: Emit event with rich context + const eventData = { + value: convertedValue, + originalValue: val, + unit: finalUnit, + sourceUnit: sourceUnit, + timestamp, + position: this._currentPosition, + variant: this._currentVariant, + type: this._currentType, + // NEW: Enhanced context + childId: this.childId, + childName: this.childName, + parentRef: this.parentRef + }; - // Emit the exact event your parent expects - this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._convertPositionNum2Str(this._currentPosition)}`, eventData); - this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData); - //console.log(`Emitted event: ${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData); + // Emit the exact event your parent expects + this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._convertPositionNum2Str(this._currentPosition)}`, eventData); + this.emitter.emit(`${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData); + // console.log(`Emitted event: ${this._currentType}.${this._currentVariant}.${this._currentPosition}`, eventData); - return this; + return this; } @@ -284,7 +284,7 @@ class MeasurementContainer { // Helper methods _ensureChainIsValid() { - if (!this._currentType || !this._currentVariant || !this._currentPosition) { + if (!this._currentType || !this._currentVariant || Number.isNaN(this._currentPosition)) { if (this.logger) { this.logger.error('Incomplete measurement chain, required: type, variant, and position'); }