Support to sort by rank

This commit is contained in:
ryo-ma 2020-08-25 23:47:15 +09:00
parent a5ea46790b
commit 3f51f2681d

View file

@ -8,7 +8,7 @@ import {
TotalRepositoryTrophy,
} from "./trophies.ts";
import { UserInfo } from "./github_api_client.ts";
import { CONSTANTS } from "./utils.ts";
import { CONSTANTS, RANK } from "./utils.ts";
export class Card {
private width = 0;
@ -21,7 +21,7 @@ export class Card {
this.width = panelSize * this.maxColumn;
}
render(userInfo: UserInfo): string {
const trophyList = new Array<Trophy>(
let trophyList = new Array<Trophy>(
new TotalStarTrophy(userInfo.totalStargazers),
new TotalCommitTrophy(userInfo.totalCommits),
new TotalFollowerTrophy(userInfo.totalFollowers),
@ -30,6 +30,12 @@ export class Card {
new TotalRepositoryTrophy(userInfo.totalRepositories),
);
// Sort by rank
const rankOrder = Object.values(RANK);
trophyList = trophyList.sort((a: Trophy, b: Trophy) =>
rankOrder.indexOf(a.rank) - rankOrder.indexOf(b.rank)
);
let row = Math.floor((trophyList.length - 1) / this.maxColumn) + 1;
if (row > this.maxRow) {
row = this.maxRow;