import { parseISO , format } from 'date-fns' ; import { sortBy , groupBy } from 'lodash' ; import { Conference } from 'types/conference' ; import { SortBy , SortDirection } from 'types/global' ; export function getConfsMonthsSorted ( conferences , sortDirection ) { return sortBy ( Object . keys ( conferences ) , ( conferenceDate ) => { const monthNumber = parseInt ( conferenceDate . split ( '-' ) [ 1 ] , 10 ) ; return sortDirection === 'asc' ? monthNumber : - monthNumber ; } ) ; } export function getMonthName ( month ) { return format ( parseISO ( `2017-${ month }-01` ) , 'MMMM' ) ; } export function groupAndSortConferences ( conferences , sortByKey , sortDirection ) { const confs = groupBy < Conference > ( conferences , ( conf ) => format ( parseISO ( conf [ sortByKey ] ) , 'yyyy' ) ) ; Object . keys ( confs ) . map ( ( year ) => { confs [ year ] = groupBy ( confs [ year ] , ( conf ) => format ( parseISO ( conf [ sortByKey ] ) , 'yyyy-MM' ) ) ; Object . keys ( confs [ year ] ) . map ( ( month ) => { confs [ year ] [ month ] = sortBy ( confs [ year ] [ month ] , ( conference ) => { const conferenceDate = new Date ( conference [ sortByKey ] ) . getTime ( ) ; return sortDirection === 'asc' ? conferenceDate : - conferenceDate ; } ) ; } ) ; } ) ; return confs ; }