fix: clamp calorie deficit targets to a safe floor and guard BMI math#909
Open
Anexus5919 wants to merge 1 commit into
Open
fix: clamp calorie deficit targets to a safe floor and guard BMI math#909Anexus5919 wants to merge 1 commit into
Anexus5919 wants to merge 1 commit into
Conversation
|
@Anexus5919 is attempting to deploy a commit to the somiljain2024-4175's projects Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Author
|
@Somil450 @diksha78dev Kindly have a review on this pr. Thanks! |
getCalorieRecommendations subtracted a flat 300/500 from TDEE with no lower bound, so smaller or sedentary users were shown deficit targets below the 1200 kcal safe minimum. Clamp both deficits to MIN_SAFE_CALORIES and coerce a non-finite TDEE to 0. calculateBMI divided by height with no guard, returning Infinity for a zero height and NaN for non-numeric input. Return 0 for those cases so the result and gauge stay finite.
27b4106 to
739aaad
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 Related Issue
Fixes #872
📝 Description
Two unguarded numeric paths in
src/utils/fitnessCalculations.ts:getCalorieRecommendationssubtracted a flat 300/500 from TDEE with no lower bound, so smaller/sedentary users were shown deficit targets below the ~1200 kcal safe minimum (e.g. TDEE 1427 gives an aggressive deficit of 927), and these render in the Deficit tab.calculateBMIdivided by height with no guard (Infinityfor height 0,NaNfor non-numeric input). Both current callers already validateheight >= 100, so this is a robustness gap in the exported helper rather than a live UI bug.🔹 What has been changed?
MIN_SAFE_CALORIES = 1200and clampeddeficitMild/deficitAggressiveto it; coerced a non-finite TDEE to 0.calculateBMIso non-finite weight or non-positive height yields0, keeping the result and gauge finite.src/utils/__tests__/fitnessCalculations.test.ts).🔹 Why are these changes needed?
🛠️ Type of Change
🧪 Testing
✅ Tests Performed
npx vitest run src/utils/__tests__/fitnessCalculations.test.ts-> 6 tests pass.npx tsc --noEmit: changed file clean;npx eslinton the changed files: 0 problems.🌐 Browsers Tested
Not applicable (pure calculation logic; verified via unit tests).
📷 Screenshots / Demo (if applicable)
Not applicable.
📋 Checklist
💬 Additional Notes
Branched from latest
upstream/main(5464425). The deficit clamp is the reachable safety fix; thecalculateBMIguard is defensive (both current callers already validate height), framed as such above.