From 35dad91bf902d9937df80cabe6b8a04be42fbdcc Mon Sep 17 00:00:00 2001 From: Thaddee Tyl Date: Sun, 14 Feb 2016 18:37:50 +0100 Subject: [PATCH] Show 0 downloads for npm packages with no downloads Fixes #635. --- server.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/server.js b/server.js index f3c9794..b4ffea5 100644 --- a/server.js +++ b/server.js @@ -1352,25 +1352,24 @@ cache(function(data, match, sendBadge, request) { return; } try { - var monthly = JSON.parse(buffer).downloads; + var monthly = JSON.parse(buffer).downloads || 0; + badgeData.text[1] = metric(monthly) + '/month'; + if (monthly === 0) { + badgeData.colorscheme = 'red'; + } else if (monthly < 10) { + badgeData.colorscheme = 'yellow'; + } else if (monthly < 100) { + badgeData.colorscheme = 'yellowgreen'; + } else if (monthly < 1000) { + badgeData.colorscheme = 'green'; + } else { + badgeData.colorscheme = 'brightgreen'; + } + sendBadge(format, badgeData); } catch(e) { badgeData.text[1] = 'invalid'; sendBadge(format, badgeData); - return; } - badgeData.text[1] = metric(monthly) + '/month'; - if (monthly === 0) { - badgeData.colorscheme = 'red'; - } else if (monthly < 10) { - badgeData.colorscheme = 'yellow'; - } else if (monthly < 100) { - badgeData.colorscheme = 'yellowgreen'; - } else if (monthly < 1000) { - badgeData.colorscheme = 'green'; - } else { - badgeData.colorscheme = 'brightgreen'; - } - sendBadge(format, badgeData); }); })); @@ -1391,7 +1390,7 @@ cache(function (data, match, sendBadge, request) { try { var totalDownloads = 0; - var downloads = JSON.parse(buffer).downloads; + var downloads = JSON.parse(buffer).downloads || 0; for (var index = 0; index < downloads.length; index++) { totalDownloads = totalDownloads + downloads[index].downloads; }