learning_ai_notes/backend/src/lib/reading-time.ts

17 lines
546 B
TypeScript

/**
* Reading time estimation utility.
*
* Pure calculation — no LLM needed.
* Average adult reading speed: ~238 words per minute.
*/
/**
* Estimate reading time for HTML or plain-text content.
* Strips HTML tags before counting words.
*/
export function estimateReadingTime(content: string): { minutes: number; words: number } {
const plain = content.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim();
const words = plain.split(/\s+/).filter(Boolean).length;
return { minutes: Math.max(1, Math.ceil(words / 238)), words };
}