Skip to content

Instantly share code, notes, and snippets.

@geowarin
Created August 29, 2016 21:05
Show Gist options
  • Save geowarin/ac20b82406a1d8366c976909f84f6684 to your computer and use it in GitHub Desktop.
Save geowarin/ac20b82406a1d8366c976909f84f6684 to your computer and use it in GitHub Desktop.
const tablify = require('tablify');
const path = require('path');
const fs = require('fs');
const modules = fs.readdirSync('node_modules');
const pkgAndAuthor = modules
.map(m => path.join(__dirname, 'node_modules', m, 'package.json'))
.filter(pkg => fs.existsSync(pkg))
.map(pkg => require(pkg))
.map(p => ({author: p.author, pkgName: p.name}))
.filter(d => d.author)
.map(d => ({author: d.author.name ? d.author.name : d.author, pkgName: d.pkgName}))
;
const total = pkgAndAuthor.length;
const groupedByAuthor = groupBy(pkgAndAuthor, 'author');
const howMany = mapObject(groupedByAuthor, modules => modules.length);
const table = [];
Object.keys(howMany).forEach(author => {
const count = howMany[author];
table.push({author, count})
});
table
.sort((d1, d2) => d2.count - d1.count);
const percents = table.map(r => {
const percent = ((r.count / total) * 100).toFixed(2);
return {
author: r.author,
modules: `${r.count}/${total} (${percent}%)`
}
});
console.log(tablify(percents));
function mapObject(obj, fn) {
return Object.keys(obj).reduce(
(res, key) => {
res[key] = fn(obj[key]);
return res;
},
{}
)
}
function groupBy(collection, attribute) {
return collection.reduce(
(res, item) => {
const value = item[attribute];
if (!res[value]) {
res[value] = [];
}
res[value].push(item);
return res;
},
{}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment