-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
54 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]>, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from './groupBy' | ||
export * from "./groupBy"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters