Add a number formatter with multiple options (and use it in a chart)
What does this MR do?
Adds a number formatter that supports:
- numbers
- percentage (0 - 1) and (0 - 100)
- digital units (bytes, kilobytes, etc...)
Use it in a chart.
API
const formatNumber = getFormatter('number');
// format...
expect(formatNumber(0.1)).toEqual('0.1');
expect(formatNumber(0.1, 0)).toEqual('0');
expect(formatNumber(0.1, 2)).toEqual('0.10');
expect(formatNumber(1000)).toEqual('1,000');
expect(formatNumber(10000)).toEqual('10,000');
const formatPercent = getFormatter(SUPPORTED_FORMATS.percent);
// format...
expect(formatPercent(1)).toEqual('1%');
expect(formatPercent(1, 2)).toEqual('1.00%');
expect(formatPercent(88.8888)).toEqual('88.889%');
expect(formatPercent(88.8888, 2)).toEqual('88.89%');
const formatBytes = getFormatter(SUPPORTED_FORMATS.bytes);
// format...
expect(formatBytes(10 ** 3)).toEqual('1kB');
expect(formatBytes(10 ** 4)).toEqual('10kB');
expect(formatBytes(10 ** 5)).toEqual('100kB');
expect(formatBytes(10 ** 6)).toEqual('1MB');
expect(formatBytes(10 ** 7)).toEqual('10MB');
expect(formatBytes(10 ** 8)).toEqual('100MB');
expect(formatBytes(10 ** 9)).toEqual('1GB');
Screenshots
No changes in UI or user behavior, hance no changelog.
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
Related to #201999 (closed)
Edited by 🤖 GitLab Bot 🤖