1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-02-24 11:33:09 +01:00

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-08-29 16:34:00 +02:00
import guides from '../content/guides.json';
import formatDate from 'date-fns/format';
2021-08-29 18:57:23 +02:00
import { AuthorType, findAuthorByUsername } from './author';
2021-08-29 16:34:00 +02:00
export type GuideType = {
2021-09-03 09:45:46 +02:00
id: string;
2021-08-29 16:34:00 +02:00
title: string;
description: string;
isPro: boolean;
isDraft: boolean;
createdAt: string;
updatedAt: string;
2021-09-03 12:59:44 +02:00
formattedCreatedAt?: string;
formattedUpdatedAt?: string;
2021-08-29 18:57:23 +02:00
authorUsername: string;
author?: AuthorType;
2021-08-29 16:34:00 +02:00
};
2021-09-03 09:45:46 +02:00
export function getGuideById(id: string): GuideType | undefined {
const allGuides = getAllGuides();
2021-09-03 12:59:44 +02:00
return allGuides.find(guide => guide.id === id);
2021-09-03 09:45:46 +02:00
}
2021-08-29 16:36:51 +02:00
export function getAllGuides(limit: number = 0): GuideType[] {
2021-08-29 16:34:00 +02:00
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'),
2021-09-03 12:59:44 +02:00
formattedUpdatedAt: formatDate(new Date(guide.updatedAt), 'MMMM d, yyyy'),
author: findAuthorByUsername(guide.authorUsername)
2021-08-29 16:34:00 +02:00
}))
.slice(0, limit ? limit : guides.length);
}