Created
March 18, 2016 02:07
-
-
Save qrg/c3b2ec3a950161250d2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| 'use strict'; | |
| const npmCount = require('npm-count'); | |
| const today = new Date().toISOString().replace(/T.+$/g, ''); | |
| const RANK = 50; | |
| const padLeft = (str, width, z) => { | |
| z = z || 0; | |
| str = str + ''; | |
| if (str.length >= width) { | |
| return str; | |
| } | |
| return new Array(width - str.length + 1).join(z) + str; | |
| } | |
| const padRight = (str, width, z) => { | |
| z = z || 0; | |
| str = str + ''; | |
| if (str.length >= width) { | |
| return str; | |
| } | |
| return str + new Array(width - str.length + 1).join(z); | |
| } | |
| npmCount.fetchTrending(today).then((trending) => { | |
| // sort by downloads desc, name asc | |
| const rank = trending | |
| .sort((a, b) => { | |
| if (a.downloads > b.downloads) { | |
| return -1; | |
| } | |
| if (a.downloads < b.downloads) { | |
| return 1; | |
| } | |
| if (a.name.toLowerCase() > b.name.toLowerCase()) { | |
| return -1; | |
| } | |
| if (a.name.toLowerCase() < b.name.toLowerCase()) { | |
| return 1; | |
| } | |
| return 0; | |
| }) | |
| .slice(0, RANK) | |
| .map((pkg) => ({ | |
| name: pkg.name, | |
| downloads: pkg.downloads | |
| })); | |
| const data = [].concat(rank); | |
| const longestName = data.sort((a, b) => { | |
| if (a.name.length > b.name.length) { return -1; } | |
| if (a.name.length < b.name.length) { return 1; } | |
| })[0].name; | |
| const maxDownloads = data.sort((a, b) => { | |
| if ((a.downloads + '').length > (b.downloads + '').length) { return -1; } | |
| if ((a.downloads + '').length < (b.downloads + '').length) { return 1; } | |
| })[0].downloads; | |
| rank.forEach((item, i) => { | |
| const order = padLeft(i + 1, (RANK + '').length); | |
| const name = padRight(item.name, longestName.length, '.'); | |
| const downloads = padLeft(item.downloads, (maxDownloads + '').length, ' '); | |
| console.log(order, name, downloads); | |
| }); | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment