Skip to content

Commit

Permalink
feat(lib): create getTagsAndNumberOfPosts function
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Jun 13, 2023
1 parent 1d2319e commit e37eee6
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/shared/lib/tags.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { allPosts } from 'contentlayer/generated'
import { slug } from '@/shared/lib/slug'
import { removeRepeatedValuesFromArray } from '@/shared/lib/remove-repeated-values-from-array'
import { getFrequencyOfValue } from '@/shared/lib/get-frequency-of-value'

const getRawTagListFromPosts = (): string[] => {
const listOfTagList = allPosts
Expand All @@ -21,6 +22,26 @@ const getRawTagListFromPosts = (): string[] => {
export const getUniqueTagListFromPosts = () =>
removeRepeatedValuesFromArray(getRawTagListFromPosts())

export interface TagsAndNumberOfPosts {
tag: string
numberOfPosts: number
}
export function getTagsAndNumberOfPosts(): TagsAndNumberOfPosts[] {
const rawTagList = getRawTagListFromPosts()
const uniqueTagList = getUniqueTagListFromPosts()

const tagsAndNumberOfPosts = uniqueTagList.map(tag => {
const numberOfPosts = getFrequencyOfValue(rawTagList, tag)

return {
tag,
numberOfPosts
}
})

return tagsAndNumberOfPosts
}

export function getPostListBasedOnTag(tag: string) {
const filteredPostList = allPosts.filter(post => {
const listOfTags = post.tags.split(',').map(rawTag => slug(rawTag.trim()))
Expand Down

0 comments on commit e37eee6

Please sign in to comment.