All files / app/src/entities/accounting-object-import/lib merge-paths.ts

100% Statements 35/35
100% Branches 12/12
100% Functions 3/3
100% Lines 35/35

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  1x 4x   4x 5x 5x   5x 4x 4x   4x 44x 44x 44x   44x 39x 44x 5x 5x 5x 5x 44x   4x 4x   5x 5x   4x 4x   1x 5x 145x 145x 5x 5x   1x 9x 9x  
import { ErrorItem, GroupedError } from './types'
export const mergeConsecutivePaths = (groupedError: GroupedError): GroupedError => {
  const result: ErrorItem[] = []
 
  for (const error of groupedError.errors) {
    const mergedPaths: string[] = []
    const sortedPaths = customSort(error.paths)
 
    if (sortedPaths.length > 0) {
      let startPath = sortedPaths[0]
      let endPath = startPath
 
      for (let i = 1; i < sortedPaths.length; i++) {
        const currentPath = sortedPaths[i]
        const currentNumber = parseInt(currentPath.substring(1), 10)
        const endNumber = parseInt(endPath.substring(1), 10)
 
        if (currentNumber === endNumber + 1) {
          endPath = currentPath
        } else {
          mergedPaths.push(formatPathInterval(startPath, endPath))
          startPath = currentPath
          endPath = currentPath
        }
      }
 
      mergedPaths.push(formatPathInterval(startPath, endPath))
    }
 
    result.push({ message: error.message, paths: mergedPaths })
  }
 
  return { xlsxErrorGroup: groupedError.xlsxErrorGroup, errors: result }
}
 
export const customSort = (data: string[]): string[] => {
  return data.sort((a, b) => {
    const categoryComparison = a.charAt(0).localeCompare(b.charAt(0))
    return categoryComparison || parseInt(a.substring(1), 10) - parseInt(b.substring(1), 10)
  })
}
 
export const formatPathInterval = (startPath: string, endPath: string): string => {
  return startPath === endPath ? startPath : `${startPath}:${endPath}`
}