Skip to content

Commit

Permalink
Skip old news
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Apr 17, 2024
1 parent bcf8ed5 commit 866d9bc
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/scripts/news-service.ts
Expand Up @@ -14,15 +14,23 @@ export async function fetchNews(): Promise<NewsItem[]> {
const itemsRaw = Yaml.load(feedYaml) as NewsItemRaw[]

const currentDate = new Date().toISOString().slice(0, 10)
const oneYearAgo = modifyYear(new Date(), -1).toISOString().slice(0, 10)
const items: NewsItem[] = itemsRaw
// Hide expired items.
.filter((item) => !item.expires || formatDate(item.expires) >= currentDate)
// Stringify dates.
.map((item) => ({ ...item, created: moment(item.created).format("YYYY-MM-DD") }))
.map((item) => ({ ...item, created: formatDate(item.created) }))
// Hide old items.
.filter((item) => item.created >= oneYearAgo)

return items
}

function modifyYear(date: Date, years: number) {
date.setFullYear(date.getFullYear() + years)
return date
}

const formatDate = (date: Date) => moment(date).format("YYYY-MM-DD")

type NewsItemRaw = {
Expand Down

0 comments on commit 866d9bc

Please sign in to comment.