From 459e63d771d88f280aff95e014993a1ea4e476e7 Mon Sep 17 00:00:00 2001 From: Abdusshh Date: Thu, 8 May 2025 02:47:30 +0300 Subject: [PATCH] feat: improve search result formatting to handle missing data gracefully --- src/lib/utils.ts | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index e949058..24a1602 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,22 +1,40 @@ import { SearchResponse, SearchResult } from "./types.js"; /** - * Format a search result into a string representation - * @param result SearchResult to format - * @returns Formatted search result string + * Formats a search result into a human-readable string representation. + * Only shows code snippet count and GitHub stars when available (not equal to -1). + * + * @param result The SearchResult object to format + * @returns A formatted string with library information */ export function formatSearchResult(result: SearchResult): string { - return `- Title: ${result.title} -- Context7-compatible library ID: ${result.id} -- Description: ${result.description} -- Code Snippets: ${result.totalSnippets} -- GitHub Stars: ${result.stars}`; + // Always include these basic details + const formattedResult = [ + `- Title: ${result.title}`, + `- Context7-compatible library ID: ${result.id}`, + `- Description: ${result.description}`, + ]; + + // Only add code snippets count if it's a valid value + if (result.totalSnippets !== -1 && result.totalSnippets !== undefined) { + formattedResult.push(`- Code Snippets: ${result.totalSnippets}`); + } + + // Only add GitHub stars if it's a valid value + if (result.stars !== -1 && result.stars !== undefined) { + formattedResult.push(`- GitHub Stars: ${result.stars}`); + } + + // Join all parts with newlines + return formattedResult.join("\n"); } /** - * Format search results into a string representation - * @param searchResponse The search response to format - * @returns Formatted search results string + * Formats a search response into a human-readable string representation. + * Each result is formatted using formatSearchResult. + * + * @param searchResponse The SearchResponse object to format + * @returns A formatted string with search results */ export function formatSearchResults(searchResponse: SearchResponse): string { if (!searchResponse.results || searchResponse.results.length === 0) {