Skip to content

Commit

Permalink
style: improved types
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuireu committed Jun 16, 2024
1 parent 4c664bf commit 44ed89f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
72 changes: 37 additions & 35 deletions src/application/dto/tag/tagDTO.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import type { BaseDTO } from '@shared/application/dto/baseDTO.ts';
import { groupBy } from './utils/groupBy';
import type { ArticleDTO } from '@application/dto/article/articleDTO.ts';
import { slugify } from '@shared/ui/utils/slugify';

export enum TagType {
TAG = 'tag',
AUTHOR = 'author'
}

export interface TagDTOItem {
name: string;
type: TagType;
count: number
}

export type TagDTO = Record<string, TagDTOItem[]>;

export const tagDTO: BaseDTO<ArticleDTO[], TagDTO> = {
render: (raw: ArticleDTO[]): TagDTO => {
const tags: TagDTOItem[] = raw.flatMap(article => ([
...article.data.tags.map((tag: string) => ({ name: tag, type: 'tag' })),
{ name: slugify(article.data.author.data.name), type: 'author' }
]));

const uniqueTags: TagDTOItem[] = [...new Set(tags.map(tag => tag.name))].map(name => {
const type = tags.find(tag => tag.name === name)?.type ?? TagType.TAG;
const count = tags.filter(tag => tag.name === name).length;

return { name, type, count };
}).sort((a, b) => a.name.localeCompare(b.name));

return groupBy(uniqueTags, item => item.name.charAt(0).toUpperCase());
},
};
import type { BaseDTO } from "@shared/application/dto/baseDTO.ts";
import { groupBy } from "./utils/groupBy";
import type { ArticleDTO } from "@application/dto/article/articleDTO.ts";
import { slugify } from "@shared/ui/utils/slugify";

export enum TagType {
TAG = "tag",
AUTHOR = "author",
}

export interface TagDTOItem {
name: string;
type: TagType;
count: number;
}

export type TagDTO = Record<string, TagDTOItem[]>;

export const tagDTO: BaseDTO<ArticleDTO[], TagDTO> = {
render: (raw: ArticleDTO[]): TagDTO => {
const tags: TagDTOItem[] = raw.flatMap((article) => [
...article.data.tags.map((tag: string) => ({ name: tag, type: "tag" })),
{ name: slugify(article.data.author.data.name), type: "author" },
]);

const uniqueTags: TagDTOItem[] = [...new Set(tags.map((tag) => tag.name))]
.map((name) => {
const type = tags.find((tag) => tag.name === name)?.type ?? TagType.TAG;
const count = tags.filter((tag) => tag.name === name).length;

return { name, type, count };
})
.sort((a, b) => a.name.localeCompare(b.name));

return groupBy(uniqueTags, (item) => item.name.charAt(0).toUpperCase());
},
};
23 changes: 13 additions & 10 deletions src/application/dto/tag/utils/groupBy/groupBy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export function groupBy<T, K extends string>(array: T[], keyFn: (item: T) => K): Record<K, T[]> {
return array.reduce((acc, currentItem) => {
const key = keyFn(currentItem);

if (!acc[key]) acc[key] = [];
acc[key].push(currentItem);

return acc;
}, {} as Record<K, T[]>);
}
export function groupBy<T, K extends string>(array: T[], keyFn: (item: T) => K): Record<K, T[]> {
return array.reduce(
(acc, currentItem) => {
const key = keyFn(currentItem);

if (!acc[key]) acc[key] = [];
acc[key].push(currentItem);

return acc;
},
{} as Record<K, T[]>,
);
}
2 changes: 1 addition & 1 deletion src/application/dto/tag/utils/groupBy/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './groupBy'
export * from "./groupBy";
4 changes: 2 additions & 2 deletions src/const/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export interface SeoMetadata {
title: string;
description: string;
robots?: {
index?: boolean;
follow?: boolean;
index: boolean;
follow: boolean;
};
image: string;
}
2 changes: 1 addition & 1 deletion src/pages/rss.xml.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
import { DEFAULT_SEO_PARAMS } from '@const/index.js';
import { DEFAULT_SEO_PARAMS } from "@const/index.js";

export async function GET(context) {
const posts = await getCollection("articles");
Expand Down

0 comments on commit 44ed89f

Please sign in to comment.