1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-02-23 02:52:23 +01:00

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-08-29 18:57:23 +02:00
import videos from '../content/videos.json';
import formatDate from 'date-fns/format';
2021-09-03 12:59:44 +02:00
import { AuthorType, findAuthorByUsername } from './author';
2021-08-29 18:57:23 +02:00
export type VideoType = {
2021-09-03 12:59:44 +02:00
id: string;
2021-08-29 18:57:23 +02:00
title: string;
description: string;
2021-09-03 16:54:35 +02:00
youtubeLink?: string;
isNew: boolean;
2021-08-29 18:57:23 +02:00
duration: string;
createdAt: string;
updatedAt: string;
2021-09-03 12:59:44 +02:00
formattedCreatedAt?: string;
formattedUpdatedAt?: string;
authorUsername: string;
author?: AuthorType;
2021-08-29 18:57:23 +02:00
};
export function getAllVideos(limit: number = 0): VideoType[] {
return (videos as VideoType[])
.sort((a, b) => (new Date(b.updatedAt) as any) - (new Date(a.updatedAt) as any))
.map(video => ({
...video,
formattedCreatedAt: formatDate(new Date(video.createdAt), 'MMMM d, yyyy'),
2021-09-03 12:59:44 +02:00
formattedUpdatedAt: formatDate(new Date(video.updatedAt), 'MMMM d, yyyy'),
author: findAuthorByUsername(video.authorUsername)
2021-08-29 18:57:23 +02:00
}))
.slice(0, limit ? limit : videos.length);
}
2021-09-03 12:59:44 +02:00
export function getVideoById(id: string): VideoType | undefined {
2021-08-29 18:57:23 +02:00
const allVideos = getAllVideos();
2021-09-03 12:59:44 +02:00
return allVideos.find(guide => guide.id === id);
2021-08-29 18:57:23 +02:00
}