blob: c1ee35d6abb2bc60fb1170449e213632e52613fb (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 | import { ArticlePreview } from '@ts/types/articles';
import { PostsList } from '@ts/types/blog';
type YearCollection = {
  [key: string]: ArticlePreview[];
};
export const sortPostsByYear = (data: PostsList[]) => {
  const yearCollection: YearCollection = {};
  data.forEach((page) => {
    page.posts.forEach((post) => {
      const postYear = new Date(post.dates.publication)
        .getFullYear()
        .toString();
      yearCollection[postYear] = [...(yearCollection[postYear] || []), post];
    });
  });
  return yearCollection;
};
 |