Strip trailing zeros after an interior zero in engineeringNotation

What does this MR do?

engineeringNotation removes the trailing zeros of the scaled mantissa with the pattern /\.([1-9]*)0+$/ (number_utils.js#L88). The capture group only accepts the digits 1 to 9, so the pattern cannot match a fractional part that has a zero before a nonzero digit, and the trailing zeros stay: engineeringNotation(1.05, 3) returns 1.050, engineeringNotation(1.005, 4) returns 1.0050, engineeringNotation(12000.5, 4) returns 12.0010k and engineeringNotation(0.00105, 4) returns 1.0500m, while engineeringNotation(1.5, 4) already returns 1.5.

This MR changes the group to (\d*?), a lazy match of any digits, so it keeps every digit up to the last nonzero one and drops only the zeros at the end. The four new rows in number_utils.spec.js fail without the change and pass with it. The changeset asks for a @gitlab/ui patch release.

Only a precision of 3 or more can show the bug, because the fraction needs an interior zero, a nonzero digit and a trailing zero. The charts in GitLab UI call the function with a precision of 2, passed explicitly or by default, so their output does not change. GitLab imports the function from @gitlab/ui/src/utils/number_utils, and the engineering format of getFormatter returns it as it is (unit_format/index.js#L124-125), so a caller there that passes a precision of 3 or more gets the extra zeros today.

How I verified it

On main at 4b7ad87adbf4950b01e295497c6d19c67617f0aa, with Node 22.23.3 and Yarn 1.22.22 as mise.toml pins them, after yarn install --frozen-lockfile. Test commands ran in packages/gitlab-ui, lint from the repository root.

Without the fix (only the new spec rows applied), NODE_ENV=test npx jest src/utils/number_utils.spec.js -t engineeringNotation fails the four new rows and passes the others:

Failing test Expected Received
with args [1.05, 3], returns 1.05 1.05 1.050
with args [1.005, 4], returns 1.005 1.005 1.0050
with args [12000.5, 4], returns 12.001k 12.001k 12.0010k
with args [0.00105, 4], returns 1.05m 1.05m 1.0500m
Tests:       4 failed, 59 skipped, 18 passed, 81 total

With the fix:

  • NODE_ENV=test npx jest src/utils/number_utils.spec.js: 81 passed.
  • yarn test:unit: 242 suites, 4,382 tests passed and 1 skipped.
  • yarn test:unit-vue3: 241 suites passed and 1 skipped, 4,376 tests passed and 3 skipped.
  • yarn lint, which runs eslint, markdownlint, prettier, stylelint and yarn-deduplicate as the lint job does: all five pass.
  • ./bin/check_translations_dict.sh: the dictionary is up to date.
  • npx changeset status --since=upstream/main: @gitlab/ui at patch.
  • The commit went through the lefthook pre-commit and commit-msg hooks.
  • The spec fails for a greedy (\d*), a single-digit (\d?) and a single-zero 0$ variant of the new pattern, each time on at least one of the new rows.
  • For a precision of 0, 1 and 2, the old and new functions gave the same string in all 840,021 comparisons I ran (280,007 values between about -2e10 and 2e10 at each precision), so callers that keep the default precision see no change.

Screenshots or screen recordings

None. No component renders differently: GitLab UI's own charts format their numbers with a precision of 2.

Integrations

The function's signature and its output for a precision of 2 or less do not change, so no project needs a migration.

Does this MR meet the acceptance criteria?

This checklist encourages the authors, reviewers, and maintainers of merge requests (MRs) to confirm changes were analyzed for conformity with the project's guidelines, security and accessibility.

Toggle the acceptance checklist

Conformity

  • The "What does this MR do?" section in the MR description is filled out, explaining the reasons for and scope of the proposed changes, per "Say why not just what".
    • For example, if the MR is focused on usage guidelines, addressing accessibility challenges could be added in a separate MR.
  • Relevant label(s) are applied to the MR.
  • The MR is added to a milestone.
  • Added the ~"component:*" label(s) if applicable.
  • A changeset is added when this MR will do a patch, minor, or major update. More information can be found in doc/changesets.md.

Components

Documentation

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
  • Security reports checked/validated by a reviewer from the AppSec team

Accessibility

If this MR adds or modifies a component, take a few moments to review the following:

  • All actions and functionality can be done with a keyboard.
  • Links, buttons, and controls have a visible focus state.
  • All content is presented in text or with a text equivalent. For example, alt text for SVG, or aria-label for icons that have meaning or perform actions.
  • Changes in a component's state are announced by a screen reader. For example, changing aria-expanded="false" to aria-expanded="true" when an accordion is expanded.
  • Color combinations have sufficient contrast.

Merge request reports

Loading
Loading