diff --git a/readme.md b/readme.md index ba3f42df0a3438..2eafdc57fd6522 100644 --- a/readme.md +++ b/readme.md @@ -216,7 +216,7 @@ You can use [GitHub's theme context](https://github.blog/changelog/2021-11-24-sp ##### Use GitHub's new media feature You can use [GitHub's new media feature](https://github.blog/changelog/2022-05-19-specify-theme-context-for-images-in-markdown-beta/) in HTML to specify whether to display images for light or dark themes. This is done using the HTML `` element in combination with the `prefers-color-scheme` media feature. - + ```html **Warning** > Language names should be URI-escaped, as specified in [Percent Encoding](https://en.wikipedia.org/wiki/Percent-encoding) @@ -347,26 +347,6 @@ Endpoint: `api/pin?username=anuraghazra&repo=github-readme-stats` [![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats)](https://github.com/anuraghazra/github-readme-stats) ``` -### Ranking configuration - -You can use the `&p=` and `&q=` options to change the method used to rank the languages used. The values must be positive real numbers. - -The algorithm used is - -```javascript -ranking_index = (byte_count ^ p) * (repo_count ^ q) -``` - -[Details here.](https://github.com/anuraghazra/github-readme-stats/issues/1600#issuecomment-1046056305) - -- `&p=1&q=0` - _(default)_ Orders by byte count -- `&p=0.5&q=0.5` - _(recommended)_ Uses both byte and repo count for ranking -- `&p=0&q=1` - Orders by repo count - -```md -[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&p=0.5&q=0.5)](https://github.com/anuraghazra/github-readme-stats) -``` - ### Demo [![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats)](https://github.com/anuraghazra/github-readme-stats) @@ -382,6 +362,23 @@ The top languages card shows a GitHub user's most frequently used top language. > **Note** > 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. +### Language stats algorithm + +We use the following algorithm to calculate the languages percentages on the language card: + +```js +ranking_index = (byte_count ^ p) * (repo_count ^ q) +``` +By default, only the byte count is used for determining the languages percentages shown on the language card (i.e. `p=1` and `q=0`). You can, however, use the `&p=` and `&q=` options to weight the language usage calculation. The values must be positive real numbers. More details about the algorithm can be found [here](Details here.](https://github.com/anuraghazra/github-readme-stats/issues/1600#issuecomment-1046056305)). + +- `&p=1&q=0` - _(default)_ Orders by byte count. +- `&p=0.5&q=0.5` - _(recommended)_ Uses both byte and repo count for ranking +- `&p=0&q=1` - Orders by repo count + +```md +[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&p=0.5&q=0.5)](https://github.com/anuraghazra/github-readme-stats) +``` + ### Usage Copy-paste this code into your readme and change the links. @@ -440,7 +437,7 @@ You can use the `&hide_progress=true` option to hide the percentages and the pro [![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&layout=compact)](https://github.com/anuraghazra/github-readme-stats) -- Hidden progress bars +- Hidden progress bars [![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&hide_progress=true)](https://github.com/anuraghazra/github-readme-stats) @@ -581,14 +578,14 @@ Since the GitHub API only allows 5k requests per hour, my `https://github-readme
:hammer_and_wrench: Step-by-step guide for deploying on other platforms -1. Fork or clone this repo as per your needs -2. Add `express` to the dependencies section of `package.json` -https://github.com/anuraghazra/github-readme-stats/blob/ba7c2f8b55eac8452e479c8bd38b044d204d0424/package.json#L54-L61 -3. Run `npm i` if needed (initial setup) -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 -https://github.com/anuraghazra/github-readme-stats/blob/ba7c2f8b55eac8452e479c8bd38b044d204d0424/package.json#L11 -5. You're done 🎉 -
+1. Fork or clone this repo as per your needs +2. Add `express` to the dependencies section of `package.json` + +3. Run `npm i` if needed (initial setup) +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 + +5. You're done 🎉 + ### Keep your fork up to date diff --git a/src/fetchers/top-languages-fetcher.js b/src/fetchers/top-languages-fetcher.js index 0fb9add7fb1156..235e3c66bd38d7 100644 --- a/src/fetchers/top-languages-fetcher.js +++ b/src/fetchers/top-languages-fetcher.js @@ -116,11 +116,10 @@ const fetchTopLanguages = async (username, exclude_repo = [], p = 1, q = 0) => { // add the size to the language size and increase repoCount. if (acc[prev.node.name] && prev.node.name === acc[prev.node.name].name) { langSize = prev.size + acc[prev.node.name].size; - repoCount = repoCount + 1; - } - else { + repoCount += 1; + } else { // reset repoCount to 1 - // language must exist in atleast one repo to be detected + // language must exist in at least one repo to be detected repoCount = 1; } return { @@ -134,12 +133,11 @@ const fetchTopLanguages = async (username, exclude_repo = [], p = 1, q = 0) => { }; }, {}); - - Object.keys(repoNodes) - .forEach((name) => { - // comparison index calculation - repoNodes[name].size = Math.pow(repoNodes[name].size, p) * Math.pow(repoNodes[name].count, q); - }) + Object.keys(repoNodes).forEach((name) => { + // comparison index calculation + repoNodes[name].size = + Math.pow(repoNodes[name].size, p) * Math.pow(repoNodes[name].count, q); + }); const topLangs = Object.keys(repoNodes) .sort((a, b) => repoNodes[b].size - repoNodes[a].size) diff --git a/tests/fetchTopLanguages.test.js b/tests/fetchTopLanguages.test.js index e3a33abca2cf38..c3f558bf4236ff 100644 --- a/tests/fetchTopLanguages.test.js +++ b/tests/fetchTopLanguages.test.js @@ -63,19 +63,19 @@ describe("FetchTopLanguages", () => { it("should fetch correct language data while using the new calculation", async () => { mock.onPost("https://api.github.com/graphql").reply(200, data_langs); - let repo = await fetchTopLanguages("anuraghazra", p = 0.5, q = 0.5); + let repo = await fetchTopLanguages("anuraghazra", [], 0.5, 0.5); expect(repo).toStrictEqual({ HTML: { color: "#0f0", count: 2, name: "HTML", - size: 20, + size: 20.000000000000004, }, javascript: { color: "#0ff", count: 2, name: "javascript", - size: 20, + size: 20.000000000000004, }, }); }); @@ -103,7 +103,7 @@ describe("FetchTopLanguages", () => { it("should fetch correct language data while using the old calculation", async () => { mock.onPost("https://api.github.com/graphql").reply(200, data_langs); - let repo = await fetchTopLanguages("anuraghazra", p = 1, q = 0); + let repo = await fetchTopLanguages("anuraghazra", [], 1, 0); expect(repo).toStrictEqual({ HTML: { color: "#0f0", @@ -123,7 +123,7 @@ describe("FetchTopLanguages", () => { it("should rank languages by the number of repositories they appear in", async () => { mock.onPost("https://api.github.com/graphql").reply(200, data_langs); - let repo = await fetchTopLanguages("anuraghazra", exclude_repo = [], p = 0, q = 1); + let repo = await fetchTopLanguages("anuraghazra", [], 0, 1); expect(repo).toStrictEqual({ HTML: { color: "#0f0",