Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)_ | `<current year> (one year to date)` |

Expand Down
15 changes: 11 additions & 4 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

Expand Down
Loading