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

26.96% Statements 24/89
60% Branches 3/5
11.11% Functions 2/18
26.96% Lines 24/89

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        1x   1x   1x     1x     1x         1x                                 1x 1x 1x 1x     1x         1x 1x 1x   1x 1x   1x         1x     1x           1x                         1x         1x               1x                         1x  
import { ErrorWithDetail, Nullable } from '@/shared/@types'
import { NextParsedUrlQuery } from 'next/dist/server/request-meta'
import { NextRouter } from 'next/router'
 
export const getNumberFromString = (str: string) => +str.replace(/\D+/g, '')
 
export const getOnlyDigitsString = (str = '') => str.replace(/\D+/g, '')
 
export const getNormalizedSortString = (field: string, sort: Nullable<string>): string =>
  `{"order[${field}]":"${sort}"}`
 
export const getNormalizedStringSort = (sort: string) => {
  return sort.replace(/"|{|}/g, '').replace(':', '=')
}
export const getDefinitionFromArray = (number: number, titles: string[]) => {
  const cases = [2, 0, 1, 1, 1, 2]
  return titles[number % 100 > 4 && number % 100 < 20 ? 2 : cases[number % 10 < 5 ? number % 10 : 5]]
}
 
export const getLinkWithFilters = (link: string, query: NextParsedUrlQuery, externalQuery?: Record<string, any>) => {
  const queryCopy = JSON.parse(JSON.stringify(query))
  if (queryCopy.from) {
    delete queryCopy.from
  }
  if (externalQuery) {
    Object.keys(externalQuery).forEach(key => {
      queryCopy[key] = externalQuery[key]
    })
  }
  if (Object.keys(queryCopy).length) {
    return `${link}?${Object.keys(queryCopy)
      .map(key => `${key}=${queryCopy[key]}`)
      .join('&')}`
  }
  return link
}
export const getBackPath = (router: NextRouter, fromUrl?: string) => {
  const from = (router?.query?.from || fromUrl) as string
  const query = JSON.parse(JSON.stringify(router.query))
  if (query.from) {
    delete query.from
  }
  if (Object.keys(query).length) {
    return `${from}?${Object.keys(query)
      .map(key => `${key}=${query[key]}`)
      .join('&')}`
  }
  return from
}
export const getIdFromIRI = (iri?: Nullable<string>) => iri?.split('/').pop()
 
export const getValueAfterFirstUnderline = (value?: Nullable<string>) => value?.split('_')[1]
export const getValueAfterOperationWord = (value?: Nullable<string>) => value?.split('operation_')[1]
 
export const toUpperCaseFirstLetter = (value?: Nullable<string>) => {
  if (!value) return
  return value.charAt(0).toUpperCase() + value.slice(1).toLowerCase()
}
 
export const convertSnakeCaseToCamelCase = (str?: Nullable<string>) =>
  str?.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase()) || ''
 
export const removeLastDot = (str: string) => {
  if (str.endsWith('.')) {
    return str.slice(0, -1)
  }
  return str
}
export const trimToRussianError = (error?: ErrorWithDetail) => {
  const text = error?.['hydra:description'] || error?.['detail']
  if (!text) return null
  const russianRegex = /[а-яА-Я]/
  const russianIndex = text?.search(russianRegex)
 
  if (russianIndex !== -1) {
    return text?.substring(russianIndex)
  } else {
    return null
  }
}
 
export const isNumberCodeKey = (key: string) => {
  const keyValue = parseInt(key, 10)
  return !isNaN(keyValue) && keyValue >= 0 && keyValue <= 9
}
 
export const getLink = (link: string) => {
  if (link?.includes('https://') || link?.includes('http://')) {
    return link
  } else {
    return `https://${link}`
  }
}
 
export const countDecimals = (numStr?: string): number => {
  if (!numStr) {
    return 0
  }
  const decimalIndex = numStr.indexOf('.')
 
  if (decimalIndex === -1) {
    return 0
  }
 
  return numStr.length - decimalIndex - 1
}
 
export const removeHtmlTags = (str: string) => str.replace(/<[^>]*>?/gm, '')