summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/sort.ts
blob: ade82d0f3c4b1c4ec0f67dbb229cd610901f0a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.date.publication).getFullYear().toString();
      yearCollection[postYear] = [...(yearCollection[postYear] || []), post];
    });
  });

  return yearCollection;
};