forked from kamranahmedse/developer-roadmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguide.ts
37 lines (33 loc) · 1.12 KB
/
guide.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import guides from '../content/guides.json';
import formatDate from 'date-fns/format';
import { AuthorType, findAuthorByUsername } from './author';
export type GuideType = {
id: string;
title: string;
description: string;
isNew: boolean;
isDraft: boolean;
createdAt: string;
updatedAt: string;
formattedCreatedAt?: string;
type?: 'visual' | 'textual';
formattedUpdatedAt?: string;
authorUsername: string;
author?: AuthorType;
};
export function getGuideById(id: string): GuideType | undefined {
const allGuides = getAllGuides();
return allGuides.find(guide => guide.id === id);
}
export function getAllGuides(limit: number = 0): GuideType[] {
return (guides as GuideType[])
.filter(guide => !guide.isDraft)
.sort((a, b) => (new Date(b.updatedAt) as any) - (new Date(a.updatedAt) as any))
.map(guide => ({
...guide,
formattedCreatedAt: formatDate(new Date(guide.createdAt), 'MMMM d, yyyy'),
formattedUpdatedAt: formatDate(new Date(guide.updatedAt), 'MMMM d, yyyy'),
author: findAuthorByUsername(guide.authorUsername)
}))
.slice(0, limit ? limit : guides.length);
}