diff --git a/frontend/src/container/GridTableComponent/utils.ts b/frontend/src/container/GridTableComponent/utils.ts index 5cc7fa79bc..52a4a7810b 100644 --- a/frontend/src/container/GridTableComponent/utils.ts +++ b/frontend/src/container/GridTableComponent/utils.ts @@ -40,7 +40,7 @@ function evaluateCondition( * @param thresholdValue - The threshold value to compare against. * @param thresholdOperator - The operator used for comparison (e.g., '>', '<', '=='). * @param thresholdUnit - The unit to which the value should be converted. - * @param columnUnits - The current unit of the value. + * @param columnUnit - The current unit of the value. * @returns A boolean indicating whether the value meets the threshold condition. */ function evaluateThresholdWithConvertedValue( @@ -48,9 +48,9 @@ function evaluateThresholdWithConvertedValue( thresholdValue: number, thresholdOperator?: string, thresholdUnit?: string, - columnUnits?: string, + columnUnit?: string, ): boolean { - const convertedValue = convertUnit(value, columnUnits, thresholdUnit); + const convertedValue = convertUnit(value, columnUnit, thresholdUnit); if (convertedValue) { return evaluateCondition(thresholdOperator, convertedValue, thresholdValue); @@ -63,7 +63,7 @@ export function findMatchingThreshold( thresholds: ThresholdProps[], label: string, value: number, - columnUnits?: string, + columnUnit?: string, ): { threshold: ThresholdProps; hasMultipleMatches: boolean; @@ -80,7 +80,7 @@ export function findMatchingThreshold( threshold?.thresholdValue, threshold.thresholdOperator, threshold.thresholdUnit, - columnUnits, + columnUnit, ) ) { matchingThresholds.push(threshold); diff --git a/frontend/src/container/NewWidget/RightContainer/dataFormatCategories.ts b/frontend/src/container/NewWidget/RightContainer/dataFormatCategories.ts index 5ecf534f53..ea5bc2ab55 100644 --- a/frontend/src/container/NewWidget/RightContainer/dataFormatCategories.ts +++ b/frontend/src/container/NewWidget/RightContainer/dataFormatCategories.ts @@ -570,20 +570,19 @@ export function convertUnit( fromUnitId?: string, toUnitId?: string, ): number | null { - // Finds the category that contains the specified units + let fromUnit: string | undefined; + let toUnit: string | undefined; + + // Finds the category that contains the specified units and extracts fromUnit and toUnit using array methods const category = dataTypeCategories.find((category) => - category.formats.some( - (format) => format.id === fromUnitId || format.id === toUnitId, - ), + category.formats.some((format) => { + if (format.id === fromUnitId) fromUnit = format.id; + if (format.id === toUnitId) toUnit = format.id; + return fromUnit && toUnit; // Break out early if both units are found + }), ); - if (!category) return null; - - const fromUnit = category.formats.find((format) => format.id === fromUnitId) - ?.id; - const toUnit = category.formats.find((format) => format.id === toUnitId)?.id; - - if (!fromUnit || !toUnit) return null; + if (!category || !fromUnit || !toUnit) return null; // Return null if category or units are not found // Gets the conversion factor for the specified units const conversionFactor = getConversionFactor( @@ -591,7 +590,7 @@ export function convertUnit( toUnit, category.name as any, ); - if (conversionFactor === null) return null; // Returns null if conversion is not possible + if (conversionFactor === null) return null; // Return null if conversion is not possible return value * conversionFactor; }