Сортировка постов по дате обновления
This commit is contained in:
parent
be6615ff48
commit
90f14bc652
1 changed files with 16 additions and 4 deletions
|
|
@ -2,15 +2,27 @@ import path from 'path';
|
||||||
|
|
||||||
export const THUMBNAIL_DEFAULT = "https://teasanctuary.ru/common/background-day.webp";
|
export const THUMBNAIL_DEFAULT = "https://teasanctuary.ru/common/background-day.webp";
|
||||||
|
|
||||||
export async function fetchPostsSorted() {
|
export type PostComparer = (a: App.BlogPost, b: App.BlogPost) => number;
|
||||||
|
|
||||||
|
export const sortPostsByPostDate: PostComparer = (a, b) => new Date(b.date!).valueOf() - new Date(a.date!).valueOf();
|
||||||
|
|
||||||
|
function laterDate(a: string, b?: string): number {
|
||||||
|
const dateA = new Date(a).valueOf();
|
||||||
|
if (!b) return dateA;
|
||||||
|
|
||||||
|
const dateB = new Date(b).valueOf();
|
||||||
|
return Math.max(dateA, dateB);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const sortPostsByPostAndUpdateDate: PostComparer = (a, b) => laterDate(b.date!, b.dateChanged) - laterDate(a.date!, a.dateChanged);
|
||||||
|
|
||||||
|
export async function fetchPostsSorted(postComparer?: PostComparer) {
|
||||||
const allPosts = await fetchPosts();
|
const allPosts = await fetchPosts();
|
||||||
|
|
||||||
const sortedPosts = allPosts
|
const sortedPosts = allPosts
|
||||||
// Для списка постов оставляем только те, у которых объявлена дата публикации
|
// Для списка постов оставляем только те, у которых объявлена дата публикации
|
||||||
.filter((a) => !!a.date)
|
.filter((a) => !!a.date)
|
||||||
.sort((a, b) => {
|
.sort(postComparer ?? sortPostsByPostDate);
|
||||||
return new Date(b.date!).valueOf() - new Date(a.date!).valueOf();
|
|
||||||
});
|
|
||||||
|
|
||||||
return sortedPosts;
|
return sortedPosts;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue