Skip to content

refactor(calculate): extract safe percentage rounding logic into a single reusable helper function#6155

Open
ChetanSenta wants to merge 1 commit into
JhaSourav07:mainfrom
ChetanSenta:refactor/extract-percentage-calculator
Open

refactor(calculate): extract safe percentage rounding logic into a single reusable helper function#6155
ChetanSenta wants to merge 1 commit into
JhaSourav07:mainfrom
ChetanSenta:refactor/extract-percentage-calculator

Conversation

@ChetanSenta

Copy link
Copy Markdown
Contributor

Description

Fixes #6148

Pillar

  • 🎨 Pillar 1 — New Theme Design
  • 📐 Pillar 2 — Geometric SVG Improvement
  • 🕐 Pillar 3 — Timezone Logic Optimization
  • 🛠️ Other (Refactoring)

Visual Preview

Zero behavioral or visual outcome changes. All computed statistical outputs remain identical to previous calculations.

What this PR does

Cleans up redundant, duplicated code patterns in lib/calculate.ts. The codebase contained several identical inline ternary calculations (total === 0 ? 0 : Math.round(...)) designed to prevent NaN or zero division errors when computing ratios like weekendRatio.

This refactor bundles this pattern into a robust calculateSafePercentage() utility function, improving codebase maintainability and readability.

Changes

File Change
lib/calculate.ts Created calculateSafePercentage() helper and replaced redundant inline ternaries.
lib/calculate.test.ts Added regression tests verifying zero-division protection and accurate rounding precision.

Checklist before requesting a review:

  • I have read the CONTRIBUTING.md file.
  • I have tested these changes locally.
  • I have run npm run format and npm run lint locally and resolved all errors.
  • My commits follow the Conventional Commits format.
  • My branch has only one clean commit to merge.

@vercel

vercel Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Someone is attempting to deploy a commit to the jhasourav07's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Next.js Bundle Size Report (Gzipped Sizes)

✨ No significant bundle size changes detected.

📊 Summary of Totals

Category PR Size Base Size Difference
Total JS 3694.34 KB 3694.34 KB 0 B
Total CSS 296.06 KB 296.06 KB 0 B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactor]: Extract duplicate inline safe percentage rounding logic in calculate.ts into a utility function

1 participant