Define easier to use interface for unit formatter
What does this MR do?
formatNumber
fn as part of locale
1. Provides a new This formatter will be useful for most users to render numbers in a locale friendly manner.
This function is a wrapper of toLocaleString() that can be used in templates.
import { formatNumber } from '~/locale';
// Does basic number formatting, for most use cases
formatNumber(12345) // 12,345
2. Makes advanced formatter easier to use
When I initially designed the unit formatters I had a very specific use case in mind: user configured the charts axis and we could select the appropriate format for them accordingly. This created a very flexible but hard to use interface for the unit formatter:
Before:
import { SUPPORTED_FORMATS, getFormatter } from '~/lib/utils/unit_format';
...
const formatNumber = getFormatter(SUPPORTED_FORMATS.number)
const formatted = formatNumber(1000.1, 2) // formatted to `1,000.10`
After:
This change helps users find the right formatter easily and add it to their code, and its is built on top of formatNumber
from ~/locale
:
import { number } from '~/lib/utils/unit_format';
...
const formatted = number(1000.1, 2) // formatted to `1,000.10`
Note: getFormatter
is still supported to cover dynamic formatting cases where needed.
Screenshots (strongly suggested)
N/A
Does this MR meet the acceptance criteria?
Conformity
-
Changelog entry -
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
Security
If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:
-
Label as security and @ mention @gitlab-com/gl-security/appsec
-
The MR includes necessary changes to maintain consistency between UI, API, email, or other methods -
Security reports checked/validated by a reviewer from the AppSec team