• Find the best matching AniList manga for a Kenmei entry based on title similarity.

    Parameters

    • kenmeiManga: KenmeiManga

      The Kenmei manga to match.

    • anilistManga: AniListManga[]

      Array of potential AniList matches to evaluate.

    • threshold: number = 0.7

      Minimum similarity threshold (0-1, default: 0.7).

    Returns null | { manga: AniListManga; score: number }

    Best match above threshold with confidence score, or null if no match qualifies.

    export function findBestMatch(
    kenmeiManga: KenmeiManga,
    anilistManga: AniListManga[],
    threshold = 0.7,
    ): { manga: AniListManga; score: number } | null {
    if (!anilistManga?.length) return null;

    // Score all entries and filter above threshold early
    const scored = [] as { manga: AniListManga; score: number }[];

    for (const manga of anilistManga) {
    const score = scoreMatch(kenmeiManga, manga);
    if (score <= 0) continue;
    scored.push({ manga, score });
    }

    if (!scored.length) return null;

    // Find highest scored match without allocating extra arrays
    let best = scored[0];
    for (let i = 1; i < scored.length; i++) {
    if (scored[i].score > best.score) best = scored[i];
    }

    return best.score >= threshold ? best : null;
    }