(items, selector)
| 101 | } |
| 102 | |
| 103 | function countBy(items, selector) { |
| 104 | const counts = new Map(); |
| 105 | for (const item of items) { |
| 106 | const key = selector(item); |
| 107 | if (!key) { |
| 108 | continue; |
| 109 | } |
| 110 | counts.set(key, (counts.get(key) || 0) + 1); |
| 111 | } |
| 112 | return Array.from(counts.entries()) |
| 113 | .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])) |
| 114 | .map(([label, value]) => ({ label, value })); |
| 115 | } |
| 116 | |
| 117 | function toBarRows(rows, limit = 6) { |
| 118 | const subset = rows.slice(0, limit); |