Skip to content

Commit

Permalink
fix: resolved comments and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SagarRajput-7 committed Oct 18, 2024
1 parent 9d1c523 commit b92740c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
10 changes: 5 additions & 5 deletions frontend/src/container/GridTableComponent/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ 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(
value: number,
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);
Expand All @@ -63,7 +63,7 @@ export function findMatchingThreshold(
thresholds: ThresholdProps[],
label: string,
value: number,
columnUnits?: string,
columnUnit?: string,
): {
threshold: ThresholdProps;
hasMultipleMatches: boolean;
Expand All @@ -80,7 +80,7 @@ export function findMatchingThreshold(
threshold?.thresholdValue,
threshold.thresholdOperator,
threshold.thresholdUnit,
columnUnits,
columnUnit,
)
) {
matchingThresholds.push(threshold);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,28 +570,27 @@ 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(
fromUnit,
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;
}
Expand Down

0 comments on commit b92740c

Please sign in to comment.