Skip to content

Commit

Permalink
feat: improved top lang fetching & changed title
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraghazra committed Jul 28, 2020
1 parent f7f8ee7 commit 826f020
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
14 changes: 5 additions & 9 deletions src/fetchTopLanguages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const fetcher = (variables, token) => {
query: `
query userInfo($login: String!) {
user(login: $login) {
repositories(isFork: false, first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) {
# fetch only owner repos & not forks
repositories(ownerAffiliations: OWNER, isFork: false, first: 100) {
nodes {
languages(first: 1, orderBy: {field: SIZE, direction: DESC}) {
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
edges {
size
node {
Expand Down Expand Up @@ -49,18 +50,13 @@ async function fetchTopLanguages(username) {
.filter((node) => {
return node.languages.edges.length > 0;
})
.sort((a, b) => {
return b.languages.edges[0].size - a.languages.edges[0].size;
})
.map((node) => {
return node.languages.edges[0];
})
.reduce((acc, curr) => curr.languages.edges.concat(acc), [])
.sort((a, b) => b.size - a.size)
.reduce((acc, prev) => {
let langSize = prev.size;
if (acc[prev.node.name] && prev.node.name === acc[prev.node.name].name) {
langSize = prev.size + acc[prev.node.name].size;
}

return {
...acc,
[prev.node.name]: {
Expand Down
2 changes: 1 addition & 1 deletion src/renderTopLanguages.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
${
hide_title
? ""
: `<text data-testid="header" x="25" y="35" class="header">Top Languages</text>`
: `<text data-testid="header" x="25" y="35" class="header">Most Used Languages</text>`
}
<svg data-testid="lang-items" x="25" y="${hide_title ? 25 : 55}">
Expand Down
4 changes: 2 additions & 2 deletions tests/renderTopLanguages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("Test renderTopLanguages", () => {
document.body.innerHTML = renderTopLanguages(langs);

expect(queryByTestId(document.body, "header")).toHaveTextContent(
"Top Languages"
"Most Used Languages"
);

expect(queryAllByTestId(document.body, "lang-name")[0]).toHaveTextContent(
Expand Down Expand Up @@ -211,7 +211,7 @@ describe("Test renderTopLanguages", () => {
it('should render with layout compact', () => {
document.body.innerHTML = renderTopLanguages(langs, {layout: 'compact'});

expect(queryByTestId(document.body, "header")).toHaveTextContent("Top Languages");
expect(queryByTestId(document.body, "header")).toHaveTextContent("Most Used Languages");

expect(queryAllByTestId(document.body, "lang-name")[0]).toHaveTextContent("HTML 40.00%");
expect(queryAllByTestId(document.body, "lang-progress")[0]).toHaveAttribute("width","120.00");
Expand Down
4 changes: 2 additions & 2 deletions tests/top-langs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const data_langs = {
nodes: [
{
languages: {
edges: [{ size: 100, node: { color: "#0f0", name: "HTML" } }],
edges: [{ size: 150, node: { color: "#0f0", name: "HTML" } }],
},
},
{
Expand Down Expand Up @@ -55,7 +55,7 @@ const langs = {
HTML: {
color: "#0f0",
name: "HTML",
size: 200,
size: 250,
},
javascript: {
color: "#0ff",
Expand Down

0 comments on commit 826f020

Please sign in to comment.