Skip to content

Commit

Permalink
feat: added "exclude_repo" option to Top Langs (anuraghazra#493)
Browse files Browse the repository at this point in the history
* 🚀 Added "exclude_repo" option to Top Langs

* chore: code style update

Co-authored-by: Anurag <hazru.anurag@gmail.com>
  • Loading branch information
Bas950 and anuraghazra authored Sep 27, 2020
1 parent 3443b37 commit d4e2a1b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
7 changes: 6 additions & 1 deletion api/top-langs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = async (req, res) => {
cache_seconds,
layout,
langs_count,
exclude_repo,
} = req.query;
let topLangs;

Expand All @@ -34,7 +35,11 @@ module.exports = async (req, res) => {
}

try {
topLangs = await fetchTopLanguages(username, langs_count);
topLangs = await fetchTopLanguages(
username,
langs_count,
parseArray(exclude_repo),
);

const cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10),
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ You can provide multiple comma-separated values in bg_color option to render a g
- `layout` - Switch between two available layouts `default` & `compact`
- `card_width` - Set the card's width manually _(number)_
- `langs_count` - Show more languages on the card, between 1-10, defaults to 5 _(number)_
- `exclude_repo` - Exclude specified repositories _(Comma-separated values)_

> :warning: **Important:**
> Language names should be uri-escaped, as specified in [Percent Encoding](https://en.wikipedia.org/wiki/Percent-encoding)
Expand Down Expand Up @@ -221,6 +222,14 @@ Endpoint: `api/top-langs?username=anuraghazra`
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra)](https://github.com/anuraghazra/github-readme-stats)
```

### Exclude individual repositories

You can use `?exclude_repo=repo1,repo2` parameter to exclude individual repositories.

```md
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&exclude_repo=github-readme-stats,anuraghazra.github.io)](https://github.com/anuraghazra/github-readme-stats)
```

### Hide individual languages

You can use `?hide=language1,language2` parameter to hide individual languages.
Expand Down
19 changes: 18 additions & 1 deletion src/fetchers/top-languages-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const fetcher = (variables, token) => {
# fetch only owner repos & not forks
repositories(ownerAffiliations: OWNER, isFork: false, first: 100) {
nodes {
name
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
edges {
size
Expand All @@ -33,7 +34,7 @@ const fetcher = (variables, token) => {
);
};

async function fetchTopLanguages(username, langsCount = 5) {
async function fetchTopLanguages(username, langsCount = 5, exclude_repo = []) {
if (!username) throw Error("Invalid username");

langsCount = clampValue(parseInt(langsCount), 1, 10);
Expand All @@ -46,6 +47,22 @@ async function fetchTopLanguages(username, langsCount = 5) {
}

let repoNodes = res.data.data.user.repositories.nodes;
let repoToHide = {};

// populate repoToHide map for quick lookup
// while filtering out
if (exclude_repo) {
exclude_repo.forEach((repoName) => {
repoToHide[repoName] = true;
});
}

// filter out repositories to be hidden
repoNodes = repoNodes
.sort((a, b) => b.size - a.size)
.filter((name) => {
return !repoToHide[name.name];
});

repoNodes = repoNodes
.filter((node) => {
Expand Down

0 comments on commit d4e2a1b

Please sign in to comment.