Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5577bbf

Browse files
kitswasrickstaa
andauthored
New top language algorithm implementation (anuraghazra#1732)
* Reduced vercel maxDuration* Implemented new algorithm for Top Langs* Revert "Reduced vercel maxDuration"This reverts commitb0bc626.* Added documentation* Fixed broken implementation* Update fetchTopLanguages.test.jsChanged tests* Now uses the general formulaThe parameters p and q can be set by the user.* Updated tests and added new test* Added new testNew test for order by repo count.* Updated documentationAdded explanation and examples for new options.* Updated documentationThis was overwritten in the merge commit.* docs: improve docs and fix tests* Renamed parametersRenamed `p` and `q` to `size_weight` and `count_weight`, respectively.* Updated the documentationChanges introduced inf2516d6---------Co-authored-by: rickstaa <rick.staa@outlook.com>
1 parentda46a50 commit5577bbf

File tree

4 files changed

+104
-16
lines changed

4 files changed

+104
-16
lines changed

‎api/top-langs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default async (req, res) => {
2525
layout,
2626
langs_count,
2727
exclude_repo,
28+
size_weight,
29+
count_weight,
2830
custom_title,
2931
locale,
3032
border_radius,
@@ -46,6 +48,8 @@ export default async (req, res) => {
4648
consttopLangs=awaitfetchTopLanguages(
4749
username,
4850
parseArray(exclude_repo),
51+
size_weight,
52+
count_weight,
4953
);
5054

5155
constcacheSeconds=clampValue(

‎readme.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ You can provide multiple comma-separated values in the bg_color option to render
310310
-`custom_title` - Sets a custom title for the card_(string)_. Default`Most Used Languages`.
311311
-`disable_animations` - Disables all animations in the card_(boolean)_. Default:`false`.
312312
-`hide_progress` - It uses the compact layout option, hides percentages, and removes the bars. Default:`false`.
313+
-`size_weight` - Configures language stats algorithm_(number)_ (see[Language stats algorithm](#Language-stats-algorithm)), defaults to 1.
314+
-`count_weight` - Configures language stats algorithm_(number)_ (see[Language stats algorithm](#Language-stats-algorithm)), defaults to 0.
313315

314316
>**Warning**
315317
>Language names should be URI-escaped, as specified in[Percent Encoding](https://en.wikipedia.org/wiki/Percent-encoding)
@@ -359,7 +361,25 @@ Use [show_owner](#customization) variable to include the repo's owner username
359361
The top languages card shows a GitHub user's most frequently used top language.
360362

361363
>**Note**
362-
>Top Languages does not indicate my skill level or anything like that; it's a GitHub metric to determine which languages have the most code on GitHub. It is a new feature of github-readme-stats.
364+
>Top Languages does not indicate the user's skill level or anything like that; it's a GitHub metric to determine which languages have the most code on GitHub. It is a new feature of github-readme-stats.
365+
366+
###Language stats algorithm
367+
368+
We use the following algorithm to calculate the languages percentages on the language card:
369+
370+
```js
371+
ranking_index= (byte_count^ size_weight)* (repo_count^ count_weight)
372+
```
373+
374+
By default, only the byte count is used for determining the languages percentages shown on the language card (i.e.`size_weight=1` and`count_weight=0`). You can, however, use the`&size_weight=` and`&count_weight=` options to weight the language usage calculation. The values must be positive real numbers.[More details about the algorithm can be found here](https://github.com/anuraghazra/github-readme-stats/issues/1600#issuecomment-1046056305).
375+
376+
-`&size_weight=1&count_weight=0` -_(default)_ Orders by byte count.
377+
-`&size_weight=0.5&count_weight=0.5` -_(recommended)_ Uses both byte and repo count for ranking
378+
-`&size_weight=0&count_weight=1` - Orders by repo count
379+
380+
```md
381+
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&size_weight=0.5&count_weight=0.5)](https://github.com/anuraghazra/github-readme-stats)
382+
```
363383

364384
###Usage
365385

@@ -419,7 +439,7 @@ You can use the `&hide_progress=true` option to hide the percentages and the pro
419439

420440
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&layout=compact)](https://github.com/anuraghazra/github-readme-stats)
421441

422-
- Hidden progress bars
442+
-Hidden progress bars
423443

424444
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&hide_progress=true)](https://github.com/anuraghazra/github-readme-stats)
425445

@@ -564,14 +584,14 @@ Since the GitHub API only allows 5k requests per hour, my `https://github-readme
564584
<details>
565585
<summary><b>:hammer_and_wrench: Step-by-step guide for deploying on other platforms</b></summary>
566586

567-
1. Fork or clone this repo as per your needs
568-
2. Add`express` to the dependencies section of`package.json`
569-
https://github.com/anuraghazra/github-readme-stats/blob/ba7c2f8b55eac8452e479c8bd38b044d204d0424/package.json#L54-L61
570-
3. Run`npm i` if needed (initial setup)
571-
4. Run`node express.js` to start the server, or set the entry point to`express.js` in`package.json` if you're deploying on a managed service
572-
https://github.com/anuraghazra/github-readme-stats/blob/ba7c2f8b55eac8452e479c8bd38b044d204d0424/package.json#L11
573-
5. You're done 🎉
574-
</details>
587+
1.Fork or clone this repo as per your needs
588+
2.Add`express` to the dependencies section of`package.json`
589+
<https://github.com/anuraghazra/github-readme-stats/blob/ba7c2f8b55eac8452e479c8bd38b044d204d0424/package.json#L54-L61>
590+
3.Run`npm i` if needed (initial setup)
591+
4.Run`node express.js` to start the server, or set the entry point to`express.js` in`package.json` if you're deploying on a managed service
592+
<https://github.com/anuraghazra/github-readme-stats/blob/ba7c2f8b55eac8452e479c8bd38b044d204d0424/package.json#L11>
593+
5.You're done 🎉
594+
</details>
575595

576596
###Keep your fork up to date
577597

‎src/fetchers/top-languages-fetcher.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ const fetcher = (variables, token) => {
5454
*@param {string[]} exclude_repo List of repositories to exclude.
5555
*@returns {Promise<import("./types").TopLangData>} Top languages data.
5656
*/
57-
constfetchTopLanguages=async(username,exclude_repo=[])=>{
57+
constfetchTopLanguages=async(
58+
username,
59+
exclude_repo=[],
60+
size_weight=1,
61+
count_weight=0,
62+
)=>{
5863
if(!username)thrownewMissingParamError(["username"]);
5964

6065
constres=awaitretryer(fetcher,{login:username});
@@ -101,6 +106,8 @@ const fetchTopLanguages = async (username, exclude_repo = []) => {
101106
.sort((a,b)=>b.size-a.size)
102107
.filter((name)=>!repoToHide[name.name]);
103108

109+
letrepoCount=0;
110+
104111
repoNodes=repoNodes
105112
.filter((node)=>node.languages.edges.length>0)
106113
// flatten the list of language nodes
@@ -111,20 +118,33 @@ const fetchTopLanguages = async (username, exclude_repo = []) => {
111118

112119
// if we already have the language in the accumulator
113120
// & the current language name is same as previous name
114-
// add the size to the language size.
121+
// add the size to the language size and increase repoCount.
115122
if(acc[prev.node.name]&&prev.node.name===acc[prev.node.name].name){
116123
langSize=prev.size+acc[prev.node.name].size;
124+
repoCount+=1;
125+
}else{
126+
// reset repoCount to 1
127+
// language must exist in at least one repo to be detected
128+
repoCount=1;
117129
}
118130
return{
119131
...acc,
120132
[prev.node.name]:{
121133
name:prev.node.name,
122134
color:prev.node.color,
123135
size:langSize,
136+
count:repoCount,
124137
},
125138
};
126139
},{});
127140

141+
Object.keys(repoNodes).forEach((name)=>{
142+
// comparison index calculation
143+
repoNodes[name].size=
144+
Math.pow(repoNodes[name].size,size_weight)*
145+
Math.pow(repoNodes[name].count,count_weight);
146+
});
147+
128148
consttopLangs=Object.keys(repoNodes)
129149
.sort((a,b)=>repoNodes[b].size-repoNodes[a].size)
130150
.reduce((result,key)=>{

‎tests/fetchTopLanguages.test.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,22 @@ const error = {
6060
};
6161

6262
describe("FetchTopLanguages",()=>{
63-
it("should fetch correct language data",async()=>{
63+
it("should fetch correct language data while using the new calculation",async()=>{
6464
mock.onPost("https://api.github.com/graphql").reply(200,data_langs);
6565

66-
letrepo=awaitfetchTopLanguages("anuraghazra");
66+
letrepo=awaitfetchTopLanguages("anuraghazra",[],0.5,0.5);
6767
expect(repo).toStrictEqual({
6868
HTML:{
6969
color:"#0f0",
70+
count:2,
7071
name:"HTML",
71-
size:200,
72+
size:20.000000000000004,
7273
},
7374
javascript:{
7475
color:"#0ff",
76+
count:2,
7577
name:"javascript",
76-
size:200,
78+
size:20.000000000000004,
7779
},
7880
});
7981
});
@@ -85,17 +87,59 @@ describe("FetchTopLanguages", () => {
8587
expect(repo).toStrictEqual({
8688
HTML:{
8789
color:"#0f0",
90+
count:1,
8891
name:"HTML",
8992
size:100,
9093
},
9194
javascript:{
9295
color:"#0ff",
96+
count:2,
97+
name:"javascript",
98+
size:200,
99+
},
100+
});
101+
});
102+
103+
it("should fetch correct language data while using the old calculation",async()=>{
104+
mock.onPost("https://api.github.com/graphql").reply(200,data_langs);
105+
106+
letrepo=awaitfetchTopLanguages("anuraghazra",[],1,0);
107+
expect(repo).toStrictEqual({
108+
HTML:{
109+
color:"#0f0",
110+
count:2,
111+
name:"HTML",
112+
size:200,
113+
},
114+
javascript:{
115+
color:"#0ff",
116+
count:2,
93117
name:"javascript",
94118
size:200,
95119
},
96120
});
97121
});
98122

123+
it("should rank languages by the number of repositories they appear in",async()=>{
124+
mock.onPost("https://api.github.com/graphql").reply(200,data_langs);
125+
126+
letrepo=awaitfetchTopLanguages("anuraghazra",[],0,1);
127+
expect(repo).toStrictEqual({
128+
HTML:{
129+
color:"#0f0",
130+
count:2,
131+
name:"HTML",
132+
size:2,
133+
},
134+
javascript:{
135+
color:"#0ff",
136+
count:2,
137+
name:"javascript",
138+
size:2,
139+
},
140+
});
141+
});
142+
99143
it("should throw error",async()=>{
100144
mock.onPost("https://api.github.com/graphql").reply(200,error);
101145

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp