Skip to content

Commit

Permalink
simplify sunlight and difficulty filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezryr committed Nov 18, 2024
1 parent 52eb495 commit 4e25567
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,27 @@ export function checkSunlight(
]);

// For each sunlight selected, check if plant's min_hours and max_hours are
// within that enum's range
const sunlightBoolean: boolean[] = [];
// within that enum's range, return true if so
for (const sunlight of sunlightFilterValue) {
const [minHours, maxHours] = sunlightToHours.get(sunlight.value)!;
// if max_hours is null then plant can only receive min_hours of sunlight
if (plant.sunlight_max_hours === null) {
sunlightBoolean.push(
if (
plant.sunlight_min_hours >= minHours &&
plant.sunlight_min_hours <= maxHours,
);
} else {
sunlightBoolean.push(
plant.sunlight_min_hours >= minHours &&
plant.sunlight_max_hours <= maxHours,
);
plant.sunlight_min_hours <= maxHours
) {
return true;
}
} else if (
plant.sunlight_min_hours >= minHours &&
plant.sunlight_max_hours <= maxHours
) {
return true;
}
}

// Return true if any of the sunlightBooleans are true
return sunlightBoolean.includes(true);
// Return false if no matches found
return false;
}

export function checkDifficulty(
Expand All @@ -200,13 +201,14 @@ export function checkDifficulty(
return true;
}

// For each difficulty selected,
// check if plant's difficulty_level matches difficulty
const difficultyBoolean: boolean[] = [];
// For each difficulty selected check if plant's difficulty_level matches difficulty
// If it does, return true
for (const difficulty of difficultyFilterValue) {
difficultyBoolean.push(plant.difficulty_level === difficulty.value);
if (plant.difficulty_level === difficulty.value) {
return true;
}
}

// Return true if any of the difficultyBooleans are true
return difficultyBoolean.includes(true);
// Return false if no matches found
return false;
}

0 comments on commit 4e25567

Please sign in to comment.