sunnymh/manga-site/lib/genres.ts

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();
}