Skip to content

Commit

Permalink
Migrate repo-sorter.js to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
jq1836 committed Oct 7, 2023
1 parent 7bc056a commit b16439b
Showing 1 changed file with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
function getTotalCommits(total, group) {
return total + group.checkedFileTypeContribution;
import { User } from '../types/types';
import { FilterGroupSelection } from '../types/summary';

function getTotalCommits(total: number, group: User) {
// If group.checkedFileTypeContribution === undefined, then we treat it as 0 contribution
return total + (group.checkedFileTypeContribution ?? 0);
}

function getGroupCommitsVariance(total, group) {
function getGroupCommitsVariance(total: number, group: User) {
return total + group.variance;
}

function sortingHelper(element, sortingOption) {
function checkKeyAndGetValue(element: any, key: string | undefined) {

Check warning on line 13 in frontend/src/utils/repo-sorter.ts

View workflow job for this annotation

GitHub Actions / Cypress frontend tests

Unexpected any. Specify a different type

Check warning on line 13 in frontend/src/utils/repo-sorter.ts

View workflow job for this annotation

GitHub Actions / ubuntu-20.04 JDK 8

Unexpected any. Specify a different type

Check warning on line 13 in frontend/src/utils/repo-sorter.ts

View workflow job for this annotation

GitHub Actions / ubuntu-20.04 JDK 8

Unexpected any. Specify a different type

Check warning on line 13 in frontend/src/utils/repo-sorter.ts

View workflow job for this annotation

GitHub Actions / ubuntu-22.04 JDK 8

Unexpected any. Specify a different type

Check warning on line 13 in frontend/src/utils/repo-sorter.ts

View workflow job for this annotation

GitHub Actions / macos-11 JDK 8

Unexpected any. Specify a different type

Check warning on line 13 in frontend/src/utils/repo-sorter.ts

View workflow job for this annotation

GitHub Actions / macos-12 JDK 8

Unexpected any. Specify a different type

Check warning on line 13 in frontend/src/utils/repo-sorter.ts

View workflow job for this annotation

GitHub Actions / windows-2019 JDK 8

Unexpected any. Specify a different type

Check warning on line 13 in frontend/src/utils/repo-sorter.ts

View workflow job for this annotation

GitHub Actions / windows-2022 JDK 8

Unexpected any. Specify a different type
if (key === undefined || element === undefined || !(key in element)) {
return undefined;
}
return element[key];
}

function sortingHelper(element: User[], sortingOption?: string): string | number {
if (sortingOption === 'totalCommits') {
return element.reduce(getTotalCommits, 0);
}
Expand All @@ -16,21 +27,28 @@ function sortingHelper(element, sortingOption) {
if (sortingOption === 'displayName') {
return window.getAuthorDisplayName(element);
}
return element[0][sortingOption];
return checkKeyAndGetValue(element[0], sortingOption);
}

function groupByRepos(repos, sortingControl) {
const sortedRepos = [];
function groupByRepos(
repos: User[][],
sortingControl: {
sortingOption: string;
sortingWithinOption: string;
isSortingDsc: string;
isSortingWithinDsc: string; },
) {
const sortedRepos: User[][] = [];
const {
sortingWithinOption, sortingOption, isSortingDsc, isSortingWithinDsc,
} = sortingControl;
const sortWithinOption = sortingWithinOption === 'title' ? 'displayName' : sortingWithinOption;
const sortOption = sortingOption === 'groupTitle' ? 'searchPath' : sortingOption;
repos.forEach((users) => {
if (sortWithinOption === 'totalCommits') {
users.sort(window.comparator((ele) => ele.checkedFileTypeContribution));
users.sort(window.comparator((ele) => ele.checkedFileTypeContribution ?? 0));
} else {
users.sort(window.comparator((ele) => ele[sortWithinOption]));
users.sort(window.comparator((ele) => checkKeyAndGetValue(ele, sortWithinOption)));
}

if (isSortingWithinDsc) {
Expand All @@ -45,8 +63,13 @@ function groupByRepos(repos, sortingControl) {
return sortedRepos;
}

function groupByNone(repos, sortingControl) {
const sortedRepos = [];
function groupByNone(
repos: User[][],
sortingControl: {
sortingOption: string;
isSortingDsc: string; },
) {
const sortedRepos: User[] = [];
const { sortingOption, isSortingDsc } = sortingControl;
const isSortingGroupTitle = sortingOption === 'groupTitle';
repos.forEach((users) => {
Expand All @@ -61,7 +84,7 @@ function groupByNone(repos, sortingControl) {
if (sortingOption === 'totalCommits') {
return repo.checkedFileTypeContribution;
}
return repo[sortingOption];
return checkKeyAndGetValue(repo, sortingOption);
}));
if (isSortingDsc) {
sortedRepos.reverse();
Expand All @@ -70,9 +93,16 @@ function groupByNone(repos, sortingControl) {
return sortedRepos;
}

function groupByAuthors(repos, sortingControl) {
const authorMap = {};
const filtered = [];
function groupByAuthors(
repos: User[][],
sortingControl: {
sortingOption: string;
sortingWithinOption: string;
isSortingDsc: string;
isSortingWithinDsc: string; },
) {
const authorMap: { [userName: string]: User[] } = {};
const filtered: User[][] = [];
const {
sortingWithinOption, sortingOption, isSortingDsc, isSortingWithinDsc,
} = sortingControl;
Expand All @@ -89,9 +119,9 @@ function groupByAuthors(repos, sortingControl) {
});
Object.keys(authorMap).forEach((author) => {
if (sortWithinOption === 'totalCommits') {
authorMap[author].sort(window.comparator((repo) => repo.checkedFileTypeContribution));
authorMap[author].sort(window.comparator((repo) => repo.checkedFileTypeContribution ?? 0));
} else {
authorMap[author].sort(window.comparator((repo) => repo[sortWithinOption]));
authorMap[author].sort(window.comparator((repo) => checkKeyAndGetValue(repo, sortingWithinOption)));
}
if (isSortingWithinDsc) {
authorMap[author].reverse();
Expand All @@ -106,7 +136,15 @@ function groupByAuthors(repos, sortingControl) {
return filtered;
}

function sortFiltered(filtered, filterControl) {
function sortFiltered(
filtered: User[][],
filterControl: {
filterGroupSelection: FilterGroupSelection;
sortingOption: string;
sortingWithinOption: string;
isSortingDsc: string;
isSortingWithinDsc: string; },
) {
const { filterGroupSelection } = filterControl;
let full = [];

Expand Down

0 comments on commit b16439b

Please sign in to comment.