| 37 | * sorted by count in descending order, formatted as a string. |
| 38 | */ |
| 39 | export function countAndSortItems(items: string[], topN: number = 20): string { |
| 40 | const counts = new Map<string, number>() |
| 41 | for (const item of items) { |
| 42 | counts.set(item, (counts.get(item) || 0) + 1) |
| 43 | } |
| 44 | return Array.from(counts.entries()) |
| 45 | .sort((a, b) => b[1] - a[1]) |
| 46 | .slice(0, topN) |
| 47 | .map(([item, count]) => `${count.toString().padStart(6)} ${item}`) |
| 48 | .join('\n') |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Picks up to `want` basenames from a frequency-sorted list of paths, |