All files / src/common/utils general.utils.js

64.06% Statements 41/64
46.15% Branches 12/26
50% Functions 16/32
65.45% Lines 36/55

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    6x   6x   16x                 6x 4x       6x 4x     4x   4x     10x           6x   2x         6x 1x                                   6x                           6x   2x               6x 3x   6x 1x   6x     6x 2x 2x 2x     6x 7x 7x     6x 2x 2x 2x     6x   6x 3x                                          
import * as generalConstants from './general.constants'
 
export const emptyFunc = (param) => param
 
export const unix = () => Math.round(+new Date() / 1000)
 
export const onChangeValue = ({ onChange, isValid, valueForced = 'NO', isTouched = false }) => (
  event,
) =>
  onChange({
    value: valueForced !== 'NO' ? valueForced : event.target.value,
    isValid,
    isTouched,
  })
 
export const getParamURL = (param) =>
  window.location.search.split(`${param}=`)[1]
    ? window.location.search.split(`${param}=`)[1]
    : false
 
export const detectBrowserLanguage = () => {
  const forcedLang = getParamURL('lang')
 
  const browserLang =
    (navigator.languages && navigator.languages[0]) || navigator.language || navigator.userLanguage
 
  return forcedLang || browserLang
}
 
export const setFieldInitialState = (isValid = true, value = '') => ({
  value,
  isValid,
  isTouched: false,
})
 
const validate = (translate) => ({
  EMAIL: {
    test: (email) => generalConstants.EMAIL_VALIDATION.test(String(email).toLowerCase()),
    message: () => translate('Your email looks incorrect'),
  },
})
 
export const getSeatsAndTime = (availableTimesAndSeats) =>
  availableTimesAndSeats
    .reduce(
      (seatsAndTime, { time, seats }) => {
        seats.forEach((seat, index) => {
          seatsAndTime.push({
            id: `${time}-${seat}-${index}`,
            seat,
            time,
          })
        })
 
        return seatsAndTime
      },
 
      [],
    )
    .sort((a, b) => timeToNum(formattedRoundHour(a.time)) - timeToNum(formattedRoundHour(b.time)))
 
export const getSeatsByTime = (availableTimesAndSeats, translate) =>
  availableTimesAndSeats.reduce(
    (seatsDict, { time, seats }) => {
      seatsDict[String(time)] = seats.map((seat) => ({
        value: seat,
        text: seat === 1 ? translate('One seat') : translate(`{num} seats`, { num: seat }),
      }))
 
      return seatsDict
    },
 
    {},
  )
 
export const validateEmail = (translate, acceptedDomains) => ({
  test: (emailAddress) =>
    validate(translate).EMAIL.test(emailAddress) &&
    acceptedDomains.some((domain) => emailAddress.split('@')[1] === domain),
  message: (emailAddress) =>
    !validate(translate).EMAIL.test(emailAddress)
      ? validate(translate).EMAIL.message(emailAddress)
      : translate('This email is not acceptable, please make sure to use your corporate email'),
})
 
export const getSeatsList = ({ preferredTime, availableTimesAndSeats, translate }) =>
  preferredTime.value ? getSeatsByTime(availableTimesAndSeats, translate)[preferredTime.value] : []
 
export const filterByPage = ({ items, itemsPerPage, page }) =>
  items.slice((page - 1) * itemsPerPage, page * itemsPerPage)
 
export const removeEmptyObjValues = (obj) =>
  Object.fromEntries(Object.entries(obj).filter(([_, value]) => Boolean(value)))
 
export const formattedDate = (unixTime) => {
  const date = new Date(unixTime * 1000)
  const options = { weekday: 'short', day: 'numeric', month: 'short' }
  return date.toLocaleDateString(detectBrowserLanguage(), options)
}
 
export const formattedRoundHour = (unixTime) => {
  const date = new Date(unixTime * 1000)
  return `${date.getHours()}:00`
}
 
export const accessibleFormattedDate = (unixTime) => {
  const date = new Date(unixTime * 1000)
  const options = { weekday: 'long', day: 'numeric', month: 'long' }
  return date.toLocaleDateString(detectBrowserLanguage(), options)
}
 
const timeToNum = (time) => time.split(':').join('')
 
export const getPreferredTimeData = (availableTimesAndSeats) =>
  (availableTimesAndSeats || [])
    .reduce(
      (times, { time }) => {
        const now = unix()
        if (now > time) {
          return times
        }
 
        const hour = formattedRoundHour(time)
 
        times.push({
          value: time,
          text: hour,
        })
 
        return times
      },
 
      [],
    )
    .sort((a, b) => timeToNum(a.text) - timeToNum(b.text))