// original from @airporty
// gist https://gist.github.com/airportyh/203f7c2d8a8b29af44218b79596af32f
// tweet https://twitter.com/airportyh/status/931540087602581504

function main(authors) {
  authors.forEach(function(author) {
    let name, articles;
    if (author) {
      if (author.name) {
        name = author.name;
      } else if (author.firstName) {
        if (author.lastName) {
          name = author.firstName + ' ' + author.lastName;
        } else {
          name = author.firstName;
        }
      }
    }
    if (!name) {
      name = '<Unknown author>';
    }
    if (author) {
      articles = author.articles;
    }
    if (!articles) {
      articles = [];
    }
    let articleCount = articles.length;
    let maybeS = articleCount > 1 || articleCount === 0  ? 's' : '';
    console.log(`${name} has published ${articles.length} article${maybeS}.`);
    articles.forEach(function(article) {
      let title, wordCount;
      if (article) {
        title = article.title;
      }
      if (!title) {
        title = '<Untitled>';
      }
      if (article) {
        if (article.contents) {
          let words = article.contents
            .split(/[^A-Za-z']+/)
            .filter(word => !!word);
          wordCount = words.length;
        }
      }
      if (!wordCount) {
        wordCount = 0;
      }
      console.log(`  Article ${title} has ${wordCount} words.`);
    });
  });
}