Skip to content

Define easier to use interface for unit formatter

Miguel Rincon requested to merge more-helpful-number-formatting into master

What does this MR do?

1. Provides a new formatNumber fn as part of locale

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

Availability and Testing

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
Edited by Miguel Rincon

Merge request reports