Skip to content

Commit

Permalink
refactor: simplify and handle label lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Oct 3, 2024
1 parent 4dd8e61 commit c66036e
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/home/sorting/sort-issues-by-priority.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { GitHubIssue } from "../github-types";

export function sortIssuesByPriority(issues: GitHubIssue[]) {
return issues.sort((a, b) => {
const priorityRegex = /Priority: (\d+)/;
const aPriorityMatch =
a.labels.find((label): label is { name: string } => typeof label === "object" && label !== null && "name" in label && priorityRegex.test(label.name))
?.name || "No Priority";
const bPriorityMatch =
b.labels.find((label): label is { name: string } => typeof label === "object" && label !== null && "name" in label && priorityRegex.test(label.name))
?.name || "No Priority";

const priorityA = aPriorityMatch.match(priorityRegex);
const priorityB = bPriorityMatch.match(priorityRegex);
const priorityRegex = /Priority: (\d+)/;

const aPriority = priorityA && priorityA[1] ? parseInt(priorityA[1], 10) : 0;
const bPriority = priorityB && priorityB[1] ? parseInt(priorityB[1], 10) : 0;
return issues.sort((a, b) => {
function getPriority(issue: GitHubIssue) {
const priorityLabel = issue.labels.find(
(label): label is { name: string } => typeof label === "object" && "name" in label && typeof label.name === "string" && priorityRegex.test(label.name)
);
const match = priorityLabel?.name.match(priorityRegex);
return match ? parseInt(match[1], 10) : -1;
}

return bPriority - aPriority;
return getPriority(b) - getPriority(a);
});
}

0 comments on commit c66036e

Please sign in to comment.