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

27.9% Statements 24/86
100% Branches 9/9
23.07% Functions 3/13
27.9% Lines 24/86

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 1071x           1x     1x         1x   1x 249x 1182x 368x 368x 814x 249x 249x   1x                 1x                                     1x 2x   1x               1x           1x                         1x                     1x 10x 10x   1x                
import dayjs, { Dayjs } from 'dayjs'
import { BaseFilter, Nullable, SelectOption } from '@/shared/@types'
import { getObjectWithoutEmptyProperty } from './object.helpers'
import { OperationFilter } from '@/entities/accounting-object'
import { convertSnakeCaseToCamelCase } from '@tmk/ui-kit'
 
export const getSelectOptionsFromListOfConstants = (list: string[]): SelectOption[] =>
  list.map(el => ({ id: el, label: el }))
 
export const getDateRangeFilters = (data: Record<string, Nullable<Dayjs>>) => {
  const object = getObjectWithoutEmptyProperty(data)
  return Object.keys(object).reduce((arr, key) => ({ ...arr, [key]: dayjs(object[key]).format() }), {})
}
 
export const getBetweenFilterValue = (value: Nullable<number[]>) => value && value.join('..')
 
export const removeEmptyFilters = (filters: Record<string, unknown>) => {
  return Object.keys(filters).reduce((acc, key) => {
    if (filters[key] !== null && filters[key] !== undefined && filters[key] !== '') {
      return { ...acc, [key]: filters[key] }
    }
    return acc
  }, {} as Record<string, unknown>)
}
 
export const removeEmptyValues = (obj: Record<string, any>) => {
  return Object.keys(obj).reduce((acc, key) => {
    if (obj[key] !== null && obj[key] !== undefined) {
      return { ...acc, [key]: obj[key] }
    }
    return acc
  }, {} as Record<string, any>)
}
 
export const reorderObjectFields = (obj: Record<string, any>, fieldOrder: string[]) => {
  const orderedObj: Record<string, any> = {}
  fieldOrder.forEach(field => {
    //call добавлен, чтобы не срабатывало правило ESLint no-prototype-builtins,
    //которое запрещает использовать методы Object.prototype напрямую через объекты.
    if (Object.prototype.hasOwnProperty.call(obj, field)) {
      orderedObj[field] = obj[field]
    } else if (field === 'cp_') {
      // костыль для сортировки кастомных полей
      Object.keys(obj).forEach(key => {
        if (key.startsWith('cp_')) {
          orderedObj[key] = obj[key]
        }
      })
    }
  })
  return removeEmptyValues(orderedObj)
}
 
export const filterStringArrayByUniqueValues = (array: string[]) =>
  array.filter((item, index) => array.indexOf(item) === index)
 
export const removeParamByName = (urlString: string, paramName: string, value?: string) => {
  const urlParams = new URLSearchParams(urlString)
  if (urlParams.has(paramName, value)) {
    urlParams.delete(paramName)
  }
  return urlParams.toString()
}
 
export const getFilterByOperationKey = (operationFilter: OperationFilter) => {
  const operationNameSingularKey = convertSnakeCaseToCamelCase(operationFilter.label)
  const operationNamePluralKey = operationNameSingularKey + 's'
  return { [`${operationNamePluralKey}`]: operationFilter.id }
}
 
export const createQueryString = (params: Record<string, string | string[]>) => {
  const queryString = Object.keys(params)
    .map(key => {
      const value = params[key]
      if (Array.isArray(value)) {
        return value.map(item => `${encodeURIComponent(key)}=${encodeURIComponent(item)}`).join('&')
      }
      return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
    })
    .join('&')
  return queryString
}
 
export const getFiltersString = (filters: Record<string, any>) => {
  return Object.entries(filters)
    .map(([key, value]) => {
      if (Array.isArray(value)) {
        return value.map(v => `${key}[]=${v}`).join('&')
      }
      return `${key}=${value}`
    })
    .join('&')
}
 
export const getArrayFilterIds = (filtersArray: Nullable<BaseFilter[]>) => {
  return (filtersArray || [])?.map(item => item?.id)
}
 
export const getFiltersQueryLink = (link: string, query: Record<string, any>) => {
  if (Object.keys(query).length) {
    return `${link}?${Object.keys(query)
      .map(key => `${key}=${query[key]}`)
      .join('&')}`
  }
  return link
}