sunnymh/lib/genres.ts
yiekheng 4f5d74e1c8 Squashed 'manga-site/' content from commit f2ef775
git-subtree-dir: manga-site
git-subtree-split: f2ef775f7095dc2b107b576cd4053593e89dd887
2026-04-12 18:47:51 +08:00

32 lines
799 B
TypeScript

/**
* A manga's `genre` field may hold multiple comma-separated genres
* (e.g. "冒险, 恋爱, 魔幻"). This normalizes the raw string into a
* deduped, trimmed list.
*/
export function parseGenres(raw: string): string[] {
if (!raw) return [];
const seen = new Set<string>();
const out: string[] = [];
for (const part of raw.split(",")) {
const g = part.trim();
if (!g) continue;
if (seen.has(g)) continue;
seen.add(g);
out.push(g);
}
return out;
}
/**
* Flatten all genres across a collection of manga into a sorted unique list.
*/
export function collectGenres(
mangas: { genre: string }[]
): string[] {
const seen = new Set<string>();
for (const m of mangas) {
for (const g of parseGenres(m.genre)) seen.add(g);
}
return [...seen].sort();
}