Skip to content

feat: add number helpers - #5145

Closed
deformator852 wants to merge 2 commits into
adonisjs:7.xfrom
deformator852:feat/number-helpers
Closed

feat: add number helpers#5145
deformator852 wants to merge 2 commits into
adonisjs:7.xfrom
deformator852:feat/number-helpers

Conversation

@deformator852

Copy link
Copy Markdown

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Adds a number helpers module, available as @adonisjs/core/helpers/number.

Methods:

  • clamp(value, min, max) — constrain a number to the given bounds
  • between(value, min, max) — check whether a number is inside an inclusive range (bounds may be passed in either order)
  • toFinite(value, fallback?) — convert a value to a finite number, or return the fallback (default 0)
  • parse(value) — convert a value to a finite number, or return null
  • format(value, options?) — format a number via Intl.NumberFormat (digits, compact)

The same helpers are registered as the Edge global number.

Unit and Edge tests cover all methods, including format.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@RomainLanz

Copy link
Copy Markdown
Member

Hey! 👋🏻

Thanks for the contribution.

I think these generic number helpers would fit better in @poppinss/utils, alongside the existing string and other low-level utilities. Core could re-export them later if needed.

I would also remove format. Number formatting is locale-dependent, and hardcoding en bypasses the application and request locale. This behavior is already owned by @adonisjs/i18n through i18n.formatNumber.

@deformator852

Copy link
Copy Markdown
Author

Hi! Thank you! I'll take that into account

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.

2 participants