Added Language_count parameter

Signed-off-by: Florian Bouillon <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2020-09-08 14:16:28 +02:00
parent 1b9e41ba1b
commit c81b6da0a9
6 changed files with 35 additions and 117 deletions

View File

@ -20,6 +20,7 @@ module.exports = async (req, res) => {
title_color, title_color,
text_color, text_color,
bg_color, bg_color,
language_count,
theme, theme,
cache_seconds, cache_seconds,
layout, layout,
@ -49,6 +50,7 @@ module.exports = async (req, res) => {
hide_border: parseBoolean(hide_border), hide_border: parseBoolean(hide_border),
card_width: parseInt(card_width, 10), card_width: parseInt(card_width, 10),
hide: parseArray(hide), hide: parseArray(hide),
language_count: parseInt(language_count),
title_color, title_color,
text_color, text_color,
bg_color, bg_color,

View File

@ -29,10 +29,5 @@
"emoji-name-map": "^1.2.8", "emoji-name-map": "^1.2.8",
"github-username-regex": "^1.0.0", "github-username-regex": "^1.0.0",
"word-wrap": "^1.2.3" "word-wrap": "^1.2.3"
},
"husky": {
"hooks": {
"pre-commit": "npm test"
}
} }
} }

View File

@ -1,25 +1,29 @@
const { getCardColors, FlexLayout, clampValue } = require("../common/utils"); const { getCardColors, FlexLayout, clampValue } = require("../common/utils");
const Card = require("../common/Card"); const Card = require("../common/Card");
const createProgressNode = ({ width, color, name, progress }) => { const createProgressNode = ({ width, color, name, progress, progress2 }) => {
const paddingRight = 95; const paddingRight = 60;
const progressTextX = width - paddingRight + 10;
const progressWidth = width - paddingRight; const progressWidth = width - paddingRight;
const progressPercentage = clampValue(progress, 2, 100); const progressPercentage = clampValue(progress, 2, 100);
const progress2Percentage = clampValue(progress2, 2, 100);
return ` return `
<text data-testid="lang-name" x="2" y="15" class="lang-name">${name}</text> <text data-testid="lang-name" x="2" y="15" class="lang-name">${name} ${progress}%${progress2 > progress ? ` + ${progress2 - progress}%` : ''}</text>
<text x="${progressTextX}" y="34" class="lang-name">${progress}%</text>
<svg width="${progressWidth}"> <svg width="${progressWidth}">
<rect rx="5" ry="5" x="0" y="25" width="${progressWidth}" height="8" fill="#ddd"></rect> <rect rx="5" ry="5" x="0" y="25" width="${progressWidth}" height="8" fill="#ddd"></rect>
<rect
height="8"
fill="#f2b866"
rx="5" ry="5" x="1" y="25"
width="calc(${progress2Percentage}% - 1px)"
></rect>
<rect <rect
height="8" height="8"
fill="${color}" fill="${color}"
rx="5" ry="5" x="0" y="25" rx="5" ry="5" x="0" y="25"
data-testid="lang-progress" data-testid="lang-progress"
width="${progressPercentage}%" width="${progressPercentage}%"
> ></rect>
</rect>
</svg> </svg>
`; `;
}; };
@ -70,6 +74,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
text_color, text_color,
bg_color, bg_color,
hide, hide,
language_count,
theme, theme,
layout, layout,
} = options; } = options;
@ -90,7 +95,8 @@ const renderTopLanguages = (topLangs, options = {}) => {
.sort((a, b) => b.size - a.size) .sort((a, b) => b.size - a.size)
.filter((lang) => { .filter((lang) => {
return !langsToHide[lowercaseTrim(lang.name)]; return !langsToHide[lowercaseTrim(lang.name)];
}); })
.slice(0, language_count || 5);
const totalLanguageSize = langs.reduce((acc, curr) => { const totalLanguageSize = langs.reduce((acc, curr) => {
return acc + curr.size; return acc + curr.size;
@ -165,6 +171,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
name: lang.name, name: lang.name,
color: lang.color || "#858585", color: lang.color || "#858585",
progress: ((lang.size / totalLanguageSize) * 100).toFixed(2), progress: ((lang.size / totalLanguageSize) * 100).toFixed(2),
progress2: ((lang.recentSize / totalLanguageSize) * 100).toFixed(2),
}); });
}), }),
gap: 40, gap: 40,

View File

@ -27,7 +27,9 @@ async function fetchTopLanguages(username) {
const item = repoNodes[key] const item = repoNodes[key]
list.push({ list.push({
name: key, name: key,
xp: item.xps color: languageColor[key] ? languageColor[key].color : '#000000',
xp: item.xps,
recentXp: item.new_xps + item.xps
}) })
} }
@ -37,27 +39,19 @@ async function fetchTopLanguages(username) {
}) })
.sort((a, b) => b.xp - a.xp) .sort((a, b) => b.xp - a.xp)
.reduce((acc, prev) => { .reduce((acc, prev) => {
// get the size of the language (bytes)
let langSize = prev.xp;
// if we already have the language in the accumulator
// & the current language name is same as previous name
// add the size to the language size.
if (acc[prev.name] && prev.name === acc[prev.name].name) {
langSize = prev.size + acc[prev.name].size;
}
return { return {
...acc, ...acc,
[prev.name]: { [prev.name]: {
name: prev.name, name: prev.name,
color: languageColor[prev.name] ? languageColor[prev.name].color : '#000000', color: prev.color,
size: langSize, size: prev.xp,
recentSize: prev.recentXp
}, },
}; };
}, {}); }, {});
const topLangs = Object.keys(repoNodes) const topLangs = Object.keys(repoNodes)
.slice(0, 5) // .slice(0, 5)
.reduce((result, key) => { .reduce((result, key) => {
result[key] = repoNodes[key]; result[key] = repoNodes[key];
return result; return result;

View File

@ -1,84 +0,0 @@
require("@testing-library/jest-dom");
const axios = require("axios");
const MockAdapter = require("axios-mock-adapter");
const fetchTopLanguages = require("../src/fetchers/top-languages-fetcher");
const mock = new MockAdapter(axios);
afterEach(() => {
mock.reset();
});
const data_langs = {
data: {
user: {
repositories: {
nodes: [
{
languages: {
edges: [{ size: 100, node: { color: "#0f0", name: "HTML" } }],
},
},
{
languages: {
edges: [{ size: 100, node: { color: "#0f0", name: "HTML" } }],
},
},
{
languages: {
edges: [
{ size: 100, node: { color: "#0ff", name: "javascript" } },
],
},
},
{
languages: {
edges: [
{ size: 100, node: { color: "#0ff", name: "javascript" } },
],
},
},
],
},
},
},
};
const error = {
errors: [
{
type: "NOT_FOUND",
path: ["user"],
locations: [],
message: "Could not resolve to a User with the login of 'noname'.",
},
],
};
describe("FetchTopLanguages", () => {
it("should fetch correct language data", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);
let repo = await fetchTopLanguages("anuraghazra");
expect(repo).toStrictEqual({
HTML: {
color: "#0f0",
name: "HTML",
size: 200,
},
javascript: {
color: "#0ff",
name: "javascript",
size: 200,
},
});
});
it("should throw error", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, error);
await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow(
"Could not resolve to a User with the login of 'noname'."
);
});
});

View File

@ -1539,6 +1539,10 @@ module.exports = { // https://github.com/ozh/github-colors
"color": "#2b7489", "color": "#2b7489",
"url": "https://github.com/trending?l=TypeScript" "url": "https://github.com/trending?l=TypeScript"
}, },
"TypeScript (JSX)": {
"color": "#2b7489",
"url": "https://github.com/trending?l=TypeScript"
},
"Unified Parallel C": { "Unified Parallel C": {
"color": null, "color": null,
"url": "https://github.com/trending?l=Unified-Parallel-C" "url": "https://github.com/trending?l=Unified-Parallel-C"