# All files / src/_utils CalendarHelpers.ts

93.87% Statements 92/98
75% Branches 9/12
68.42% Functions 13/19
92.4% Lines 73/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    25812x

    12x

    12x

    12x

    12x

    12x

    12x

    49x

    12x

    12x

    12x

    12x

    12x

    12x

    12x

    12x

    12x

    12x

    35x

    35x
    35x

    35x
    35x

    35x

    12x

    12x

    12x

    12x

    12x

    12x

    1596x

    12x

    12x

    12x

    12x
    1260x
    1092x

    168x

    12x
    3696x

    12x

    12x

    8280x
    8280x
    8280x
    8280x

    4140x
    4140x
    4140x
    4140x
    4140x
    4140x

    4140x

    393x
    393x
    393x

    393x
    393x

    395x
    393x

    12x
    360x

    12x
    30x

    630x

    113x
    113x

    12x

    12x

    12x

    12x

    12x

    12x
     import {
      DateTimeFormatter,
      DateTimeOptions,
      getStartDayOfTheWeek,
    } from '@procore/globalization-toolkit'
    import {
      addDays as addDaysFn,
      addMonths as addMonthsFn,
      eachDayOfInterval as eachDayFn,
      format as formatFn,
      getDate as getDateFn,
      getDay as getDayFn,
      getMonth as getMonthFn,
      getYear as getYearFn,
      isAfter as isAfterFn,
      isBefore as isBeforeFn,
      isSameDay as isSameDayFn,
      isSameMonth as isSameMonthFn,
      isToday as isTodayFn,
      isWithinInterval as isWithinRangeFn,
      startOfMonth as startOfMonthFn,
      startOfWeek,
      subDays as subDaysFn,
      subMonths as subMonthsFn,
      subYears as subYearsFn,
    } from 'date-fns'
    import {
      de,
      enUS as en,
      es,
      fr,
      is,
      ko,
      pt,
      th,
      zhCN,
    } from 'date-fns/esm/locale'
    import { range, splitEvery } from 'ramda'
    import { Locale } from '../_hooks/I18n'

    type DateFnsLocales = { [key in Locale]: any }

    const dateFnsLocales: DateFnsLocales = {
      en: en,
      'en-US': en,
      'en-CA': en,
      'fr-CA': fr,
      'fr-FR': fr,
      es: es,
      'es-ES': es,
      'en-AE': en,
      'en-AU': en,
      'en-GB': en,
      'en-SG': en,
      ko: ko,
      'th-TH': th,
      pt: pt,
      'is-IS': is,
      'de-DE': de,
      'zh-SG': zhCN,
      pseudo: en,
    }

    export const maxMonth = 12

    export const minYear: number = 1000

    // need to be function to make mocking of new Date works in tests.
    export const getMaxYear = () => new Date().getFullYear() + 100

    // rows in calendar
    const calendarWeeks: number = 6

    const daysInWeek: number = 7

    export const daysInCalendar: number = calendarWeeks * daysInWeek

    export const addDays: (date: Date, num: number) => Date = addDaysFn

    export const addMonths: (date: Date, num: number) => Date = addMonthsFn

    export const eachDay: (date1: Date, date2: Date) => Date[] = (date1, date2) =>
      eachDayFn({ start: date1, end: date2 })

    export const format: (date: Date, formatString: string) => string = formatFn

    // An entire date object has to be formed, format(month, 'MMM') breaks for month >= 10 (it just outputs 'Dec')
    export const formatMonth: (month: number) => string = (month) =>
      formatFn(normalizeNewDate(2019, month, 2), 'MMM')

    export const getDate: (date: Date) => number = getDateFn

    export const getDates: (date: Date, locale: Locale) => Date[] = (
      date,
      locale
    ) => {
      const startDayOfWeek: number = getStartDayOfTheWeek(locale)

      // Shift first day appearing on the calendar based on day of the week of the first day of the month and based on the start day of the week
      const dayShift = 7 - startDayOfWeek
      const subDays = (getDayFn(startOfMonthFn(date)) + dayShift) % 7

      const start = subDaysFn(startOfMonthFn(date), subDays)
      const end = addDaysFn(start, daysInCalendar - 1)

      return eachDayFn({ start, end })
    }

    export const getMonth: (date: Date) => number = getMonthFn

    export const getRows: (dates: Date[]) => Date[][] = splitEvery(7)

    export const getYear: (date: Date) => number = getYearFn

    export const isAfter: (date1: Date, date2: Date) => boolean = (date1, date2) =>
      isAfterFn(normalizeExistingDate(date1), normalizeExistingDate(date2))

    export const isBefore: (date1: Date, date2: Date) => boolean = (date1, date2) =>
      isBeforeFn(normalizeExistingDate(date1), normalizeExistingDate(date2))

    export const isSameDay: (date1: Date, date2: Date) => boolean = (
      date1,
      date2
    ) => isSameDayFn(normalizeExistingDate(date1), normalizeExistingDate(date2))

    export const isSameMonth: (date1: Date, date2: Date) => boolean = isSameMonthFn

    export const isToday: (date: Date) => boolean = isTodayFn

    export const isWithinRange: (
      date: Date,
      start?: Date,
      end?: Date
    ) => boolean = (date, start, end) => {
      if (!start || !end || start > end) {
        return false
      }

      return isWithinRangeFn(normalizeExistingDate(date), {
        start: normalizeExistingDate(start),
        end: normalizeExistingDate(end),
      })
    }

    export const normalizeExistingDate: (day: Date) => Date = (day) =>
      normalizeNewDate(getYearFn(day), getMonthFn(day), getDateFn(day))

    export const normalizeNewDate: (
      year: number,
      month: number,
      day: number,
      hour?: number,
      minute?: number,
      second?: number,
      millisecond?: number
    ) => Date = (
      year,
      month,
      day,
      hour = 0,
      minute = 0,
      second = 0,
      millisecond = 0
    ) => {
      const normalizedDate = new Date()
      normalizedDate.setFullYear(year, month, day)
      normalizedDate.setHours(hour)
      normalizedDate.setMinutes(minute)
      normalizedDate.setSeconds(second)
      normalizedDate.setMilliseconds(millisecond)

      return normalizedDate
    }

    interface SelectOption {
      label: string
      value: number
    }

    export const getMonthLabel = (month: number, Ilocale: Locale = 'en') => {
      const date: Date = normalizeNewDate(1970, month, 1)
      const options: DateTimeOptions = {
        locale: locale || 'en',
        year: 'numeric',
        month: 'short',
        day: 'numeric',
      }
      const formatter = new DateTimeFormatter(options)
      const monthPart = formatter
        .formatDateTimeToParts(date)
        .find((part) => part.type === 'month')!.value
      return monthPart
    }

    export const getMonthOptions = (locale: Locale): SelectOption[] => {
      return range(0, 12).map((value) => ({
        value,
        label: getMonthLabel(value, locale),
      }))
    }

    export const getYearOptions = (date: Date): SelectOption[] => {
      const start = Math.max(minYear, getYear(subYearsFn(date, 10)))

      return range(start, start + 21).map((value) => ({
        value,
        label: String(value).padStart(4, '0'),
      }))
    }

    export const getDayOfWeekLabel = (day: number, Ilocale: Locale = 'en') => {
      return formatFn(addDaysFn(startOfWeek(new Date()), day), 'iiiiii', {
        locale: dateFnsLocales[locale],
      })
    }

    export const updateMonth = (date: Date, month: number) => {
      return normalizeNewDate(getYear(date), month, getDate(date))
    }

    export const updateYear = (date: Date, year: number) => {
      return normalizeNewDate(year, getMonth(date), getDate(date))
    }

    export const subDays: (date: Date, num: number) => Date = subDaysFn

    export const subMonths: (date: Date, num: number) => Date = subMonthsFn

    const CalendarHelpers = {
      addDays,
      addMonths,
      eachDay,
      format,
      formatMonth,
      getDate,
      getDates,
      getMonth,
      getMonthLabel,
      getMonthOptions,
      getRows,
      getYear,
      getYearOptions,
      isAfter,
      isBefore,
      isSameDay,
      isSameMonth,
      isToday,
      isWithinRange,
      normalizeExistingDate,
      normalizeNewDate,
      subDays,
      subMonths,
      updateMonth,
      updateYear,
    }

    export default CalendarHelpers