Skip to content

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

Merged
JhaSourav07 merged 1 commit into
JhaSourav07:mainfrom
ChetanSenta:refactor/extract-percentage-calculator
Jun 21, 2026
Merged

refactor(calculate): extract safe percentage rounding logic into a single reusable helper function#6155
JhaSourav07 merged 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

@Aamod-Dev Aamod-Dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good extraction of redundant logic into a clean helper function. The calculateSafePercentage in lib/calculate.ts helps clean up the complexity around the weekendRatio calculation. Adding test coverage in lib/calculate.test.ts for zero-division guarantees is an excellent practice. Approved!

@Aamod-Dev Aamod-Dev added GSSoC 2026 mentor:Aamod007 level:beginner Small changes Usually isolated fixes or simple UI/text updates. quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. refactor labels Jun 21, 2026
@JhaSourav07 JhaSourav07 added the gssoc:approved PR has been reviewed and accepted for valid contribution points label Jun 21, 2026
@JhaSourav07 JhaSourav07 merged commit 8d86bb7 into JhaSourav07:main Jun 21, 2026
10 of 11 checks passed
@github-actions github-actions Bot added this to the GSSoC 2026 milestone Jun 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Congratulations @ChetanSenta! Your PR has been successfully merged. 🚀

Thank you for contributing to CommitPulse. Your work helps us build a better tool for the community.

⚠️ Important for GSSoC Contributors:
You are strictly advised to join our Discord Server as it is mandatory for all GSSoC participants. All important announcements, point claims, and community discussions happen there.

Keep building! 💻✨

@github-actions github-actions Bot added the type:refactor Code changes that neither fix a bug nor add a feature label Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved PR has been reviewed and accepted for valid contribution points GSSoC 2026 level:beginner Small changes Usually isolated fixes or simple UI/text updates. mentor:Aamod007 quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. refactor type:refactor Code changes that neither fix a bug nor add a feature

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

3 participants