Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions lib/hnapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down