From a8e8fc73498e85c0cc1692b7330aeb3567f4a1e6 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 16 Dec 2021 19:30:32 +0100 Subject: chore: display year between blog posts --- src/utils/helpers/sort.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/utils/helpers/sort.ts (limited to 'src/utils/helpers') diff --git a/src/utils/helpers/sort.ts b/src/utils/helpers/sort.ts new file mode 100644 index 0000000..ade82d0 --- /dev/null +++ b/src/utils/helpers/sort.ts @@ -0,0 +1,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; +}; -- cgit v1.2.3