Skip to content

Commit

Permalink
Enable sorting by open issues
Browse files Browse the repository at this point in the history
Now we can sort by an numeric value it's trivial to sort by open issue
count.
We need to use the macro for the header, add a sortByType function to
the server and use the sortBy value to switch on which field we want to
sort on.
  • Loading branch information
rich committed Sep 13, 2024
1 parent 134cfb5 commit 00cc36c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import nunjucks from "nunjucks";
import { getReposFromJson, mapRepoFromStorageToUi } from "./utils/index.js";
import { getQueryParams } from "./utils/queryParams.js";
import { OctokitApp } from "./octokitApp.js";
import { sortByOpenPrs } from "./utils/sorting.js";
import { sortByType } from "./utils/sorting.js";

nunjucks.configure({
autoescape: true,
Expand All @@ -29,7 +29,7 @@ const httpServer = createServer(async (request, response) => {
sortBy,
sortDirection,
...reposForUi,
repos: sortByOpenPrs(reposForUi.repos, sortDirection),
repos: sortByType(reposForUi.repos, sortDirection, sortBy),
});

return response.end(template);
Expand Down
2 changes: 1 addition & 1 deletion index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<th class="py-2 pl-2" scope="col">Description</th>
<th class="py-2 pl-2" scope="col">Language</th>
<th class="py-2 pl-2" scope="col">Topics</th>
<th class="py-2 pl-2" scope="col">Open issues count</th>
{{macros.sortableTableHeader("Open issues count", "openIssues", sortDirection, sortBy)}}
{{macros.sortableTableHeader("Open PRs count", "openPrsCount", sortDirection, sortBy)}}
<th class="py-2 pl-2 last:pr-2" scope="col">Last updated</th>
<th class="py-2 pl-2 last:pr-2" scope="col">Dependencies</th>
Expand Down
20 changes: 20 additions & 0 deletions utils/sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ export const sortByNumericValue = (repos, sortDirection, key) => {
}
return repos.sort((a, b) => b[key] - a[key]);
};

/**
* Sorts repositories based on the specified type and direction.
* @param {"openPrsCount"|"openIssues"} sortBy - The type of sorting to apply, either by open PRs count or open issues.
* @param {SortDirection} sortDirection - The direction to sort, either "asc" for ascending or "desc" for descending.
* @param {UiRepo[]} repos - An array of repository objects to sort.
* @returns {UiRepo[]} The sorted array of repository objects.
*/
export const sortByType = (repos, sortDirection, sortBy) => {
switch (sortBy) {
case "openPrsCount":
return sortByNumericValue(repos, sortDirection, "openPrsCount");

case "openIssues":
return sortByNumericValue(repos, sortDirection, "openIssues");
default:
return repos;
break;
}
};
40 changes: 39 additions & 1 deletion utils/sorting.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it } from "node:test";
import expect from "node:assert";
import { sortByOpenPrs, sortByNumericValue } from "./sorting.js";
import { sortByOpenPrs, sortByNumericValue, sortByType } from "./sorting.js";

describe("sortByOpenPrs", () => {
it("returns the original array if sortDirection is not provided", () => {
Expand Down Expand Up @@ -93,3 +93,41 @@ describe("sortByNumericValue", () => {
]);
});
});

describe("sortByType", () => {
it("sorts the repos by number of open PRs", () => {
const reposToSort = [
{ name: "Repo 1", openPrsCount: 5 },
{ name: "Repo 2", openPrsCount: 3 },
{ name: "Repo 3", openPrsCount: 7 },
];

expect.deepEqual(
sortByType(reposToSort, "asc", "openPrsCount"),
sortByNumericValue(reposToSort, "asc", "openPrsCount")
);
});

it("sorts the repos by number of open issues", () => {
const reposToSort = [
{ name: "Repo 1", openIssues: 5 },
{ name: "Repo 2", openIssues: 3 },
{ name: "Repo 3", openIssues: 7 },
];

expect.deepEqual(
sortByType(reposToSort, "asc", "openIssues"),
sortByNumericValue(reposToSort, "asc", "openIssues")
);
});

it("returns the original array if the sortBy parameter is passed", () => {
const reposToSort = [
{ name: "Repo 1", openIssues: 5 },
{ name: "Repo 2", openIssues: 3 },
{ name: "Repo 3", openIssues: 7 },
];

expect.deepEqual(sortByType(reposToSort, "asc", null), reposToSort);
});
});

0 comments on commit 00cc36c

Please sign in to comment.