summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2021-12-16 19:30:32 +0100
committerArmand Philippot <git@armandphilippot.com>2021-12-16 19:30:32 +0100
commita8e8fc73498e85c0cc1692b7330aeb3567f4a1e6 (patch)
tree02e92ea4d892c1128930327bc56c5123009bb365 /src/utils
parent9a81ce60575403d5d3fc489f9b51d85e7108344b (diff)
chore: display year between blog posts
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/helpers/sort.ts19
1 files changed, 19 insertions, 0 deletions
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;
+};