Skip to content

Commit

Permalink
groupBy
Browse files Browse the repository at this point in the history
  • Loading branch information
chadluo committed Mar 8, 2024
1 parent fad5e94 commit 0d30da3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 47 deletions.
40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"style-loader": "^3.3.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"typescript": "^5.4.2",
"webpack": "^5.76.2",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^5.0.2",
Expand Down
13 changes: 0 additions & 13 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,6 @@ export function renderLink(id: string, type: ItemType, names: I18nObject) {
)}">${formatName(names)}</a>`;
}

export function groupBy<W, T>(f: (w: W) => T, ws?: W[]): Map<T, W[]> {
const map = new Map<T, W[]>();
return (
ws?.reduce((m, w) => {
const key = f(w);
const arr = m.get(key) ?? [];
arr.push(w);
m.set(key, arr);
return m;
}, map) ?? map
);
}

function renderQTableRow(
materials: Material[],
objects: (OfMaterial | [Domain, number])[],
Expand Down
4 changes: 2 additions & 2 deletions src/components/characters_table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TYPE_CHARACTER, groupBy, renderLink } from "../base";
import { TYPE_CHARACTER, renderLink } from "../base";
import { hasBookmarks } from "../bookmarks";
import { DELIMITER, I18nObject, formatName } from "../i18n";
import { Character, characters } from "../models/characters";
Expand All @@ -8,7 +8,7 @@ const title: I18nObject = { en: "Characters", "zh-CN": "角色" };
export class CharactersTable extends HTMLElement {
constructor() {
super();
const byRarity = groupBy((o) => o.rarity, characters);
const byRarity = Map.groupBy(characters, ({ rarity }) => rarity);
const rarities = Array.from(byRarity.keys()).sort().reverse();
this.innerHTML = `<details class="section" ${hasBookmarks() ? "" : "open"}>
<summary>🦸 ${formatName(title)}</summary>
Expand Down
9 changes: 4 additions & 5 deletions src/components/enemies_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
TYPE_WEEKLY_BOSS,
getTimezone,
getWeekday,
groupBy,
renderDomainLink,
renderLink,
} from "../base";
Expand Down Expand Up @@ -37,14 +36,14 @@ const regions: Record<Region, I18nObject> = {
export class EnemiesTable extends HTMLElement {
constructor() {
super();
const weeklyBosses: Map<Region, Enemies.Boss[]> = groupBy(
(b) => b.region,
const weeklyBosses: Map<Region, Enemies.Boss[]> = Map.groupBy(
Enemies.bosses.filter((b) => b.type === TYPE_WEEKLY_BOSS),
(b) => b.region,
);
const weeklyBossKeys = Array.from(weeklyBosses.keys());
const bosses: Map<Region, Enemies.Boss[]> = groupBy(
(b) => b.region,
const bosses: Map<Region, Enemies.Boss[]> = Map.groupBy(
Enemies.bosses.filter((b) => b.type === TYPE_BOSS),
({ region }) => region,
);
const bossKeys = Array.from(bosses.keys());
const talentDomains = Enemies.domains.filter(
Expand Down
10 changes: 5 additions & 5 deletions src/components/weapons_table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TYPE_WEAPON, groupBy, renderLink } from "../base";
import { TYPE_WEAPON, renderLink } from "../base";
import { hasBookmarks } from "../bookmarks";
import { DELIMITER, I18nObject, formatName } from "../i18n";
import { Category, Weapon, weapons } from "../models/weapons";
Expand All @@ -7,15 +7,15 @@ const title: I18nObject = { en: "Weapons", "zh-CN": "武器" };
export class WeaponsTable extends HTMLElement {
constructor() {
super();
const byRarity = groupBy((w) => w.rarity, weapons);
const byRarity = Map.groupBy(weapons, ({ rarity }) => rarity);
const rarities = Array.from(byRarity.keys()).sort().reverse();
this.innerHTML = `<details class="section" ${hasBookmarks() ? "" : "open"}>
<summary>🗡️ ${formatName(title)}</summary><table class="ctable">
${rarities
.map((rarity) => {
const ws2: Map<Category, Weapon[]> = groupBy(
(w) => w.category,
byRarity.get(rarity),
const ws2: Map<Category, Weapon[]> = Map.groupBy(
byRarity.get(rarity) || [],
({ category }) => category,
);
const categories = Array.from(ws2.keys());
return `<tr><th rowspan="${categories.length}">${"⭐".repeat(rarity)}</th>
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"lib": [
"DOM",
"ES2023"
"ESNext"
],
"module": "ES2020",
"moduleResolution": "node",
Expand Down

0 comments on commit 0d30da3

Please sign in to comment.