mirror of
https://github.com/BradNut/awesome-uses
synced 2025-09-08 17:40:31 +00:00
51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
import { name } from 'country-emoji';
|
|
import people from '../data.js';
|
|
|
|
function merge(prop) {
|
|
return function(acc, obj) {
|
|
return [...obj[prop], ...acc];
|
|
};
|
|
}
|
|
|
|
function countInstances(acc, tag) {
|
|
acc[tag] = acc[tag] ? acc[tag] + 1 : 1;
|
|
return acc;
|
|
}
|
|
|
|
export function countries() {
|
|
const data = people
|
|
.map(person => ({
|
|
name: name(person.country),
|
|
emoji: person.country,
|
|
}))
|
|
.reduce((acc, country) => {
|
|
if (acc[country.name]) {
|
|
// exists, update
|
|
acc[country.name].count = acc[country.name].count + 1;
|
|
} else {
|
|
acc[country.name] = {
|
|
...country,
|
|
count: 1,
|
|
};
|
|
}
|
|
return acc;
|
|
}, {});
|
|
|
|
const sorted = Object.entries(data)
|
|
.map(([, country]) => country)
|
|
.sort((a, b) => b.count - a.count);
|
|
|
|
return sorted;
|
|
}
|
|
|
|
export function tags() {
|
|
const allTags = people.reduce(merge('tags'), []);
|
|
const counts = allTags.reduce(countInstances, {});
|
|
// sort and filter for any tags that only have 1
|
|
const tags = Object.entries(counts)
|
|
.sort(([, countA], [, countB]) => countB - countA)
|
|
.filter(([, count]) => count > 1)
|
|
.map(([name, count]) => ({ name, count }));
|
|
|
|
return [{ name: 'all', count: people.length }, ...tags];
|
|
}
|