import moment from 'moment-timezone' ; import sortBy from 'lodash/sortBy' ; import type { DayOfWeekEnumType } from '../types' ; import { daysOfTheWeek } from './constants' ; export function summarizeDays ( days ) { if ( days . length === 1 ) { return moment ( days [ 0 ] , 'dd' ) . format ( 'dddd' ) ; } let sortedDays = sortBy ( days , ( d ) => daysOfTheWeek . indexOf ( d ) ) ; let startDay = sortedDays [ 0 ] ; let endDay = sortedDays [ sortedDays . length - 1 ] ; let startIndex = daysOfTheWeek . indexOf ( startDay ) ; let endIndex = daysOfTheWeek . indexOf ( endDay ) ; if ( endIndex - startIndex !== sortedDays . length - 1 ) { return sortedDays . join ( ', ' ) ; } if ( startDay === 'Mo' && endDay === 'Fr' ) { return 'Weekdays' ; } else if ( startDay === 'Sa' && endDay === 'Su' ) { return 'Weekend' ; } else if ( startDay === 'Mo' && endDay === 'Su' ) { return 'Every day' ; } let start = moment ( startDay , 'dd' ) . format ( 'ddd' ) ; let end = moment ( endDay , 'dd' ) . format ( 'ddd' ) ; return `${ start } — ${ end }` ; }