All files / app/src/shared/helpers normalize.helpers.ts

61.03% Statements 47/77
88.23% Branches 15/17
33.33% Functions 3/9
61.03% Lines 47/77

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 991x       1x   1x 17x 17x 17x   1x                     1x 153x 153x           153x 153x 153x 153x 153x 153x 153x 153x 153x             153x 153x 153x                         153x 153x 153x   1x 140x 140x 140x           140x 140x 140x 140x 491x 485x 140x 140x 489x 489x 489x 489x 489x 140x 140x   1x         1x     1x  
import { get } from 'lodash'
import { SelectOption, TFunction } from '../@types'
import { MouseEvent } from 'react'
 
export const getSelectAllOption = (t: TFunction) => [{ id: '', label: t('all') }]
 
export const getBinaryOptions = (truthyLabel: string, falsyLabel: string, t: TFunction) => [
  { label: t(truthyLabel), id: true },
  { label: t(falsyLabel), id: false },
]
 
export const sortArraOfObjectsByAlphabetically = <T extends Record<string, unknown>>(array: T[], sortField: keyof T) =>
  array.sort((a, b) => {
    if (a[sortField] < b[sortField]) {
      return -1
    }
    if (a[sortField] > b[sortField]) {
      return 1
    }
    return 0
  })
 
export const normalizeSelectOptions = <T extends {}[]>(
  data?: T,
  options?: {
    labelKey?: string | ((entity: T[number]) => string)
    valueKey?: string
    defaultValue?: SelectOption[]
    validate?: (item: T[number]) => boolean
  },
  additionalFields?: string[]
): SelectOption[] => {
  const { defaultValue, labelKey, valueKey, validate } = {
    defaultValue: [],
    labelKey: 'name',
    valueKey: '@id',
    ...options,
  }
  const getCustomFiled = (el: {}) => {
    return additionalFields?.reduce((acc, field) => {
      acc[field] = get(el, field)
      return acc
    }, {} as Record<string, unknown>)
  }
 
  return [
    ...defaultValue,
    ...(data
      ? data.reduce<SelectOption[]>((acc, el) => {
          if (!validate || validate(el)) {
            acc.push({
              label: typeof labelKey === 'function' ? labelKey(el) : (get(el, labelKey, '') as string),
              id: get(el, valueKey) as string,
              ...(additionalFields?.length && {
                ...getCustomFiled(el),
              }),
            })
          }
          return acc
        }, [])
      : []),
  ]
}
 
export const normalizeSelectOptionsFromEnum = <T extends Record<string, string>>(
  data: T,
  t: TFunction,
  config?: {
    translatePrefix?: string
    postfix?: string | ((value: string) => string)
    disabledOptions?: string[]
    permittedOptions?: string[]
  }
): SelectOption[] => {
  const translatePrefix = config?.translatePrefix || ''
  return Object.values(data)
    .filter(entity => {
      if (config?.permittedOptions !== undefined) return !config.permittedOptions.includes(entity)
      return entity
    })
    .map(entity => ({
      id: entity,
      label:
        t(translatePrefix + entity) +
        ((typeof config?.postfix === 'function' ? config?.postfix?.(entity) : config?.postfix) || ''),
      disabled: config?.disabledOptions?.includes(entity),
    }))
}
 
export const normalizeSelectOptionsFromConstantsKeysArray = (
  constants: string[],
  t: TFunction
): SelectOption<number>[] => constants.map((constant, index) => ({ id: index, label: t(constant) }))
 
export const normalizeArrayToSeparatedString = (data: unknown[], separator: string): string =>
  data.filter(entity => entity).join(separator)
 
export const stopDefaultEvent = (e: MouseEvent) => [e.stopPropagation(), e.preventDefault()]