11import { DEFAULT_CONFIGURATION } from './constants' ;
2- import type { CollectionEntry } from 'astro:content' ;
32
43export const formatDate = ( date : Date ) => {
54 const formatter = new Intl . DateTimeFormat ( 'en-US' , {
@@ -23,18 +22,41 @@ export const includeDraft = (draft: boolean) => {
2322 return draft !== true ;
2423} ;
2524
26- export const sortJobsByDate = ( jobs : CollectionEntry < 'jobs' > [ ] ) => {
27- // Convert "Now" to current year, otherwise returns the year as is
28- const getEndYear = ( job : CollectionEntry < 'jobs' > ) =>
29- job . data . to === 'Now' ? new Date ( ) . getFullYear ( ) : job . data . to ;
30-
31- return jobs . sort ( ( current , next ) => {
32- // Compare end years first, then fall back to start years if end years are equal
33- const [ currentEnd , nextEnd ] = [ getEndYear ( current ) , getEndYear ( next ) ] ;
25+ /*
26+ * Generic function for sorting items with start and optional end years
27+ * - Items without an end year are considered ongoing and are sorted accordingly
28+ * - Items are sorted by end year descending, then by start year descending
29+ * - If end year is not present, current year is used for comparison
30+ */
31+ export const sortByDateRange = <
32+ T extends { data : { from : number ; to ?: number } } ,
33+ > (
34+ items : T [ ] ,
35+ ) => {
36+ const getCurrentYear = ( ) => new Date ( ) . getFullYear ( ) ;
37+
38+ return items . sort ( ( current , next ) => {
39+ // Prioritize ongoing jobs (no 'to' field) first
40+ const currentIsOngoing = current . data . to === undefined ;
41+ const nextIsOngoing = next . data . to === undefined ;
42+
43+ // If one is ongoing and the other isn't, ongoing comes first
44+ if ( currentIsOngoing && ! nextIsOngoing ) return - 1 ;
45+ if ( ! currentIsOngoing && nextIsOngoing ) return 1 ;
46+
47+ // If both are ongoing or both have end dates, sort by end year then start year
48+ const currentEnd = current . data . to ?? getCurrentYear ( ) ;
49+ const nextEnd = next . data . to ?? getCurrentYear ( ) ;
3450 return nextEnd - currentEnd || next . data . from - current . data . from ;
3551 } ) ;
3652} ;
3753
38- export const sortTalksByYear = ( talks : CollectionEntry < 'talks' > [ ] ) => {
39- return talks . sort ( ( a , b ) => b . data . year - a . data . year ) ;
54+ /*
55+ * Generic function for sorting items by year in descending order
56+ * - Items are sorted by year descending
57+ */
58+ export const sortByYear = < T extends { data : { year : number } } > (
59+ items : T [ ] ,
60+ ) => {
61+ return items . sort ( ( a , b ) => b . data . year - a . data . year ) ;
4062} ;
0 commit comments