diff --git a/readme.md b/readme.md index 6562c676a2369..ebb53e76c0674 100644 --- a/readme.md +++ b/readme.md @@ -384,7 +384,7 @@ If we don't support your language, please consider contributing! You can find mo | `text_bold` | Uses bold text. | boolean | `true` | | `disable_animations` | Disables all animations in the card. | boolean | `false` | | `ring_color` | Color of the rank circle. | string (hex color) | `2f80ed` | -| `number_format` | Switches between two available formats for displaying the card values `short` (i.e. `6.6k`) and `long` (i.e. `6626`). | enum | `short` | +| `number_format` | Switches between two available formats for displaying the card values `short` (i.e. `6.62k`) and `long` (i.e. `6626`). | enum | `short` | | `show` | Shows [additional items](#showing-additional-individual-stats) on stats card (i.e. `reviews`, `discussions_started`, `discussions_answered`, `prs_merged` or `prs_merged_percentage`). | string (comma-separated values) | `null` | | `commits_year` | Filters and counts only commits made in the specified year. | integer _(YYYY)_ | ` (one year to date)` | diff --git a/src/common/utils.js b/src/common/utils.js index 04b02e2c8215f..019c2f0ef4dd2 100644 --- a/src/common/utils.js +++ b/src/common/utils.js @@ -110,15 +110,22 @@ const iconWithLabel = (icon, label, testid, iconSize) => { }; /** - * Retrieves num with suffix k(thousands) precise to 1 decimal if greater than 999. + * Retrieves num with suffix k(thousands) precise to 2 decimals if between 1000-9999, and 1 decimal if greater than 9999. * * @param {number} num The number to format. * @returns {string|number} The formatted number. */ const kFormatter = (num) => { - return Math.abs(num) > 999 - ? Math.sign(num) * parseFloat((Math.abs(num) / 1000).toFixed(1)) + "k" - : Math.sign(num) * Math.abs(num); + const abs = Math.abs(num); + const sign = Math.sign(num); + + if (abs > 9999) { + return sign * parseFloat((abs / 1000).toFixed(1)) + "k"; + } else if (abs > 999) { + return sign * parseFloat((abs / 1000).toFixed(2)) + "k"; + } else { + return sign * abs; + } }; /** diff --git a/tests/utils.test.js b/tests/utils.test.js index 7277b95d4d2cb..8f8f466b870c1 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -16,9 +16,14 @@ describe("Test utils.js", () => { expect(kFormatter(1)).toBe(1); expect(kFormatter(-1)).toBe(-1); expect(kFormatter(500)).toBe(500); + expect(kFormatter(999)).toBe(999); expect(kFormatter(1000)).toBe("1k"); + expect(kFormatter(1200)).toBe("1.2k"); + expect(kFormatter(1250)).toBe("1.25k"); + expect(kFormatter(9990)).toBe("9.99k"); expect(kFormatter(10000)).toBe("10k"); expect(kFormatter(12345)).toBe("12.3k"); + expect(kFormatter(99900)).toBe("99.9k"); expect(kFormatter(9900000)).toBe("9900k"); });