/** * TEXT NORMALIZATION UTILITIES * * Consolidates string cleanup operations into a single, maintainable module. * Replaces fragile .replace().replace().replace() chains throughout the codebase. */ /** * Normalize review language by removing weak phrases and standardizing terminology */ export function normalizeReviewLanguage(text: string): string { if (!text) return '' let normalized = text // Remove only the most problematic weak/hedging phrases const weakPhrases = [ /\bmay cause (?:unexpected )?(?:issues?|problems?|behavior)\b/gi, /\bplease note,?\s*/gi, /\bit's recommended that\s*/gi, /\bit is recommended that\s*/gi ] for (const phrase of weakPhrases) { normalized = normalized.replace(phrase, '') } // Standardize terminology normalized = normalized.replace( /\binconsistent indentation\b/gi, 'incorrect indentation' ) // Remove excessive whitespace normalized = normalized.replace(/\s+/g, ' ').trim() return normalized } /** * Remove markdown formatting for plain text comparison */ export function stripMarkdown(text: string): string { if (!text) return '' return text .replace(/```[\s\S]*?```/g, '') // Remove code blocks .replace(/\*\*/g, '') // Remove bold .replace(/\*/g, '') // Remove italic .replace(/`/g, '') // Remove inline code .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1') // Remove links .trim() } /** * Extract key terms from text (first 5 meaningful words) */ export function extractKeyTerms(text: string, limit: number = 5): string { if (!text) return '' const normalized = normalizeReviewLanguage(text) const words = normalized.split(' ').filter(w => w.length > 3) return words.slice(0, limit).join(' ') } /** * Clean description for display (removes code blocks, weak phrases) */ export function cleanDescription(text: string): string { if (!text) return '' let cleaned = stripMarkdown(text) cleaned = normalizeReviewLanguage(cleaned) // Remove confidence statements (handled separately) cleaned = cleaned.replace(/confidence\s*:\s*(high|medium|low)/gi, '') return cleaned.trim() } /** * Generate minimal, actionable description */ export function generateMinimalDescription( description: string, type: string, context?: {variableName?: string; functionName?: string} ): string { let minimal = cleanDescription(description) // Add context if available if (context?.functionName) { minimal = `In \`${context.functionName}\`, ${minimal.charAt(0).toLowerCase() + minimal.slice(1)}` } // Ensure it starts with capital letter minimal = minimal.charAt(0).toUpperCase() + minimal.slice(1) // Remove trailing period if present minimal = minimal.replace(/\.$/, '') return minimal }