Skip to content

Commit

Permalink
Use forEach, not map, to mutate collection items
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskrycho authored Dec 5, 2019
1 parent 46c5cd1 commit 183f115
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ module.exports = function(config) {
console.error(err);
});

collection.map(post => {
collection.forEach(post => {
post.url = stripDomain(post.url);
post.primary_author.url = stripDomain(post.primary_author.url);
post.tags.map(tag => (tag.url = stripDomain(tag.url)));

// Convert publish date into a Date object
post.published_at = new Date(post.published_at);
return post;
});

// Bring featured post to the top of the list
Expand Down Expand Up @@ -133,16 +132,14 @@ module.exports = function(config) {
});

// Attach posts to their respective authors
collection.map(async author => {
collection.forEach(async author => {
const authorsPosts = posts.filter(post => {
post.url = stripDomain(post.url);
return post.primary_author.id === author.id;
});
if (authorsPosts.length) author.posts = authorsPosts;

author.url = stripDomain(author.url);

return author;
});

return collection;
Expand Down Expand Up @@ -170,16 +167,14 @@ module.exports = function(config) {
});

// Attach posts to their respective tags
collection.map(async tag => {
collection.forEach(async tag => {
const taggedPosts = posts.filter(post => {
post.url = stripDomain(post.url);
return post.primary_tag && post.primary_tag.slug === tag.slug;
});
if (taggedPosts.length) tag.posts = taggedPosts;

tag.url = stripDomain(tag.url);

return tag;
});

return collection;
Expand Down

0 comments on commit 183f115

Please sign in to comment.