Skip to content

Commit e9019c1

Browse files
feat: added number of languages param for top langs card (#445)
* feat: num_langs param for top langs * fix: top-langs card bottom padding with num_langs param * fix: clamp toplangs num_langs rather than throwing error * feat: changed param name & fixed minor issues, added tests Co-authored-by: anuraghazra <[email protected]>
1 parent 0f3bed5 commit e9019c1

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ node_modules
44
package-lock.json
55
*.lock
66
.vscode/
7+
.idea/
78
coverage

api/top-langs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = async (req, res) => {
2323
theme,
2424
cache_seconds,
2525
layout,
26+
langs_count,
2627
} = req.query;
2728
let topLangs;
2829

@@ -33,7 +34,7 @@ module.exports = async (req, res) => {
3334
}
3435

3536
try {
36-
topLangs = await fetchTopLanguages(username);
37+
topLangs = await fetchTopLanguages(username, langs_count);
3738

3839
const cacheSeconds = clampValue(
3940
parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10),

readme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ You can provide multiple comma separated values in bg_color option to render a g
165165
- `hide_title` - _(boolean)_
166166
- `layout` - Switch between two available layouts `default` & `compact`
167167
- `card_width` - Set the card's width manually _(number)_
168+
- `langs_count` - Show more languages on the card, between 1-10, defaults to 5 _(number)_
168169

169170
> :warning: **Important:**
170171
> Language names should be uri-escaped, as specified in [Percent Encoding](https://en.wikipedia.org/wiki/Percent-encoding)
@@ -200,7 +201,7 @@ Use [show_owner](#customization) variable to include the repo's owner username
200201

201202
Top languages card shows github user's top languages which have been mostly used.
202203

203-
_NOTE: Top languages does not indicate my skill level or something like that, it's a github metric of which languages i have the most code on github, it's a new feature of github-readme-stats_
204+
_NOTE: Top languages does not indicate my skill level or something like that, it's a github metric of which languages have the most code on github, it's a new feature of github-readme-stats_
204205

205206
### Usage
206207

@@ -220,6 +221,14 @@ You can use `?hide=language1,language2` parameter to hide individual languages.
220221
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&hide=javascript,html)](https://github.com/anuraghazra/github-readme-stats)
221222
```
222223

224+
### Show more languages
225+
226+
You can use the `&langs_count=` option to increase or decrease the number of languages shown on the card. Valid values are integers between 1 and 10 (inclusive), and the default is 5.
227+
228+
```md
229+
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&langs_count=8)](https://github.com/anuraghazra/github-readme-stats)
230+
```
231+
223232
### Compact Language Card Layout
224233

225234
You can use the `&layout=compact` option to change the card design.

src/cards/top-languages-card.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
112112
// RENDER COMPACT LAYOUT
113113
if (layout === "compact") {
114114
width = width + 50;
115-
height = 30 + (langs.length / 2 + 1) * 40;
115+
height = 90 + Math.round(langs.length / 2) * 25;
116116

117117
// progressOffset holds the previous language's width and used to offset the next language
118118
// so that we can stack them one after another, like this: [--][----][---]

src/fetchers/top-languages-fetcher.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { request, logger } = require("../common/utils");
1+
const { request, logger, clampValue } = require("../common/utils");
22
const retryer = require("../common/retryer");
33
require("dotenv").config();
44

@@ -33,10 +33,12 @@ const fetcher = (variables, token) => {
3333
);
3434
};
3535

36-
async function fetchTopLanguages(username) {
36+
async function fetchTopLanguages(username, langsCount = 5) {
3737
if (!username) throw Error("Invalid username");
3838

39-
let res = await retryer(fetcher, { login: username });
39+
langsCount = clampValue(parseInt(langsCount), 1, 10);
40+
41+
const res = await retryer(fetcher, { login: username });
4042

4143
if (res.data.errors) {
4244
logger.error(res.data.errors);
@@ -73,7 +75,7 @@ async function fetchTopLanguages(username) {
7375
}, {});
7476

7577
const topLangs = Object.keys(repoNodes)
76-
.slice(0, 5)
78+
.slice(0, langsCount)
7779
.reduce((result, key) => {
7880
result[key] = repoNodes[key];
7981
return result;

tests/fetchTopLanguages.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ describe("FetchTopLanguages", () => {
7474
});
7575
});
7676

77+
it("should fetch langs with specified langs_count", async () => {
78+
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);
79+
80+
let repo = await fetchTopLanguages("anuraghazra", 1);
81+
expect(repo).toStrictEqual({
82+
javascript: {
83+
color: "#0ff",
84+
name: "javascript",
85+
size: 200,
86+
},
87+
});
88+
});
89+
7790
it("should throw error", async () => {
7891
mock.onPost("https://api.github.com/graphql").reply(200, error);
7992

0 commit comments

Comments
 (0)