Skip to content

Commit d8244a7

Browse files
CaedenPHrickstaa
andauthored
Add format stats option (#2155)
* feat: added `format_stats` option (#2128) * refactor: change `format_stats` to `short_values` (#2128) * test: create shorten values test (#2128) * Update readme.md Co-authored-by: Rick Staa <[email protected]> * refactor: rename ``short_values`` to ``number_format`` * Update readme.md Co-authored-by: Rick Staa <[email protected]> * Update src/cards/stats-card.js Co-authored-by: Rick Staa <[email protected]> * refactor: format codebase --------- Co-authored-by: Rick Staa <[email protected]>
1 parent b928f51 commit d8244a7

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

api/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default async (req, res) => {
3535
locale,
3636
disable_animations,
3737
border_radius,
38+
number_format,
3839
border_color,
3940
} = req.query;
4041
res.setHeader("Content-Type", "image/svg+xml");
@@ -88,6 +89,7 @@ export default async (req, res) => {
8889
custom_title,
8990
border_radius,
9091
border_color,
92+
number_format,
9193
locale: locale ? locale.toLowerCase() : null,
9294
disable_animations: parseBoolean(disable_animations),
9395
}),

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ You can provide multiple comma-separated values in the bg_color option to render
289289
- `text_bold` - Use bold text _(boolean)_. Default: `true`.
290290
- `disable_animations` - Disables all animations in the card _(boolean)_. Default: `false`.
291291
- `ring_color` - Color of the rank circle _(hex color)_. Defaults to the theme ring color if it exists and otherwise the title color.
292+
- `number_format` - Switch between two available formats for displaying the card values `short` (i.e. `6.6k`) and `long` (i.e. `6626`). Default: `short`.
292293

293294
> **Note**
294295
> When hide_rank=`true`, the minimum card width is 270 px + the title length and padding.

src/cards/stats-card.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ const createTextNode = ({
3939
showIcons,
4040
shiftValuePos,
4141
bold,
42+
number_format,
4243
}) => {
43-
const kValue = kFormatter(value);
44+
const kValue =
45+
number_format.toLowerCase() === "long" ? value : kFormatter(value);
4446
const staggerDelay = (index + 3) * 150;
4547

4648
const labelOffset = showIcons ? `x="25"` : "";
@@ -103,6 +105,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
103105
custom_title,
104106
border_radius,
105107
border_color,
108+
number_format = "short",
106109
locale,
107110
disable_animations = false,
108111
} = options;
@@ -192,6 +195,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
192195
showIcons: show_icons,
193196
shiftValuePos: 79.01 + (isLongLocale ? 50 : 0),
194197
bold: text_bold,
198+
number_format,
195199
}),
196200
);
197201

src/cards/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type StatCardOptions = CommonOptions & {
2222
line_height: number | string;
2323
custom_title: string;
2424
disable_animations: boolean;
25+
number_format: string;
2526
};
2627

2728
export type RepoCardOptions = CommonOptions & {

tests/renderStatsCard.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,13 @@ describe("Test renderStatsCard", () => {
357357
document.body.innerHTML = renderStatsCard(stats, {});
358358
expect(document.querySelector("rect")).toHaveAttribute("rx", "4.5");
359359
});
360+
361+
it("should shorten values", () => {
362+
stats["totalCommits"] = 1999;
363+
364+
document.body.innerHTML = renderStatsCard(stats);
365+
expect(getByTestId(document.body, "commits").textContent).toBe("2k");
366+
document.body.innerHTML = renderStatsCard(stats, { number_format: "long" });
367+
expect(getByTestId(document.body, "commits").textContent).toBe("1999");
368+
});
360369
});

0 commit comments

Comments
 (0)