# All files / utils/legend sort.ts

6.15% Statements 4/65
0% Branches 0/27
0% Functions 0/14
6.9% Lines 4/58

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
    1x

    1x

    1x

    1x

     import {BandLineMap, ColumnData, LatestIndexMap} from '../../types'
    import {isNumber} from '../isNumber'

    export const sortIndicesByValueColumn = (
      valuesColumn: ColumnData,
      rowIndices: Array<number>
    ): Array<number> => {
      if (!valuesColumn || !rowIndices?.length) {
        return []
      }
      const numberMap = {}
      const sortable = []
      rowIndices.forEach(rowIndex => {
        const key = isNumber(valuesColumn[`${rowIndex}`])
          ? valuesColumn[`${rowIndex}`]
          : 0
        if (!numberMap[key]) {
          numberMap[key] = []
        }
        numberMap[key].push(rowIndex)
        sortable.push(key)
      })

      sortable.sort((first, second) => second - first)
      return sortable.map(numericalValue => numberMap[numericalValue].shift())
    }

    export const isSortable = (values): boolean => {
      if (!values || !values.length || values.length == 1) {
        return false
      }
      const initialValue = values[0]
      return values.every((value, index) => {
        if (index === 0) {
          return true
        }
        return value !== initialValue
      })
    }

    export const sortBandLines = (
      bandValues: ColumnData,
      bandLineMap: BandLineMap,
      selectedLinesIndexMap: LatestIndexMap
    ): BandLineMap => {
      const sortedBandLineMap: BandLineMap = {
        upperLines: [],
        rowLines: [],
        lowerLines: [],
      }

      const {upperLines, rowLines, lowerLines} = bandLineMap

      const indexToPreviousPositionMap: LatestIndexMap = {}
      const rowIndices = []
      const upperIndices = []
      const lowerIndices = []
      rowLines.forEach((line, position) => {
        const index = selectedLinesIndexMap[line]
        indexToPreviousPositionMap[index] = position
        rowIndices.push(index)
      })
      upperLines.forEach((line, position) => {
        const index = selectedLinesIndexMap[line]
        indexToPreviousPositionMap[index] = position
        upperIndices.push(index)
      })
      lowerLines.forEach((line, position) => {
        const index = selectedLinesIndexMap[line]
        indexToPreviousPositionMap[index] = position
        lowerIndices.push(index)
      })
      const rowValues = rowIndices.map(index => bandValues[index])
      const upperValues = upperIndices.map(index => bandValues[index])
      const lowerValues = lowerIndices.map(index => bandValues[index])

      let sortOrder
      if (isSortable(rowValues)) {
        sortOrder = sortIndicesByValueColumn(bandValues, rowIndices)
      } else if (isSortable(upperValues)) {
        sortOrder = sortIndicesByValueColumn(bandValues, upperIndices)
      } else if (isSortable(lowerValues)) {
        sortOrder = sortIndicesByValueColumn(bandValues, lowerIndices)
      }

      if (Array.isArray(sortOrder)) {
        sortOrder.forEach(index => {
          const previousPosition = indexToPreviousPositionMap[index]
          sortedBandLineMap.rowLines.push(rowLines[previousPosition])
          sortedBandLineMap.upperLines.push(upperLines[previousPosition])
          sortedBandLineMap.lowerLines.push(lowerLines[previousPosition])
        })
        return sortedBandLineMap
      }
      return bandLineMap
    }