From 0d60b5b2d3dff0b97b9876bdeb91b6940cc0ac2c Mon Sep 17 00:00:00 2001 From: Dharmesh Patel Date: Mon, 29 Jun 2015 21:43:27 -0700 Subject: [PATCH] Handling item = null --- lib/hnapi.js | 62 +++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/hnapi.js b/lib/hnapi.js index d2af69d..d56f22d 100644 --- a/lib/hnapi.js +++ b/lib/hnapi.js @@ -48,37 +48,39 @@ var api = { // Throw them all into an array Promise.all(itemFetches).then(function(res){ var apiRes = res.map(function(item){ - var commentsCount = item.descendants || 0; - - var output = { - id: item.id, - title: item.title, - points: item.score, - user: item.by, - time: item.time, // Unix timestamp - time_ago: moment(item.time*1000).fromNow(), - comments_count: commentsCount, - type: typeMapping[item.type] || item.type - }; - - if (item.url){ - output.url = item.url; - output.domain = url.parse(item.url).hostname.replace(/^www\./i, ''); - } else { - output.url = 'item?id=' + item.id; // Simulate "local" links - } - - // If it's a job, username and points are useless - if (item.type == 'job'){ - output.user = output.points = null; - } - - // Identify type=ask - if (item.type == 'story' && output.url.match(/^item/i) && item.title.match(/^ask/i)){ - output.type = 'ask'; + if (item) { + var commentsCount = item.descendants || 0; + + var output = { + id: item.id, + title: item.title, + points: item.score, + user: item.by, + time: item.time, // Unix timestamp + time_ago: moment(item.time*1000).fromNow(), + comments_count: commentsCount, + type: typeMapping[item.type] || item.type + }; + + if (item.url){ + output.url = item.url; + output.domain = url.parse(item.url).hostname.replace(/^www\./i, ''); + } else { + output.url = 'item?id=' + item.id; // Simulate "local" links + } + + // If it's a job, username and points are useless + if (item.type == 'job'){ + output.user = output.points = null; + } + + // Identify type=ask + if (item.type == 'story' && output.url.match(/^item/i) && item.title.match(/^ask/i)){ + output.type = 'ask'; + } + + return output; } - - return output; }); fn(null, apiRes);