Skip to content

Instantly share code, notes, and snippets.

@alistairjcbrown
Created December 31, 2018 01:13
Show Gist options
  • Save alistairjcbrown/9f6280e30332b164be3b57c870c2691e to your computer and use it in GitHub Desktop.
Save alistairjcbrown/9f6280e30332b164be3b57c870c2691e to your computer and use it in GitHub Desktop.
Percentage distribution of my comic collection
const groupings = [
'Star Wars',
'Martha Washington',
'Rick and Morty',
'James Bond',
'Star Trek',
];
const _ = require("lodash");
const lofcg = require("leagueofcomicgeeks");
const myUserId = 26853; // alistairjcbrown
lofcg.collection.get(
myUserId,
{ type: lofcg.types.SERIES },
(err, collection) => {
if (err) {
console.log("An error has occurred getting collection:", err);
return;
}
const distribution = _.reduce(collection, function(buckets, series) {
const matchingGrouping = _.find(groupings, (grouping) => series.name.toLowerCase().includes(grouping.toLowerCase()));
const key = matchingGrouping || 'other';
buckets[key] = buckets[key] || [];
buckets[key].push(series);
return buckets;
}, {})
const stats = _.reduce(distribution, function(result, matchedSeries, key) {
result[key] = result[key] || 0;
result[key] += _.sumBy(matchedSeries, 'count');
return result;
}, {});
const totalComics = _.sum(_.values(stats));
console.log('totalComics', totalComics);
_.each(stats, function(count, series) {
console.log(`Group ${_.padEnd(series+':', 20)}${_.padStart(count, 4)} [${Math.round(count/totalComics*100)}%]`)
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment