aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/helpers')
-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;
+};