# All files / utils normalizeConfig.ts

87.87% Statements 29/33
84.61% Branches 33/39
100% Functions 3/3
86.66% Lines 26/30

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

    1x
    22x
    17x
    6x
    6x
    4x

    6x
    4x

    6x

    11x

    1x
    20x

    20x
    18x

    18x

    11x
    6x
    5x
    4x

    1x

    11x

    7x

    7x

    7x

    20x

     import {Config, LayerConfig} from '../types'
    import {newTable} from './newTable'

    // TODO: move all layer defaults here, see: https://github.com/influxdata/giraffe/issues/447
    export const normalizeLayers = (layers: LayerConfig[]): LayerConfig[] =>
      layers?.map(layerConfig => {
        if (layerConfig.type === 'band') {
          let {upperColumnName, lowerColumnName} = layerConfig
          if (!upperColumnName || upperColumnName === layerConfig.mainColumnName) {
            upperColumnName = ''
          }
          if (!lowerColumnName || lowerColumnName === layerConfig.mainColumnName) {
            lowerColumnName = ''
          }
          return {...layerConfig, upperColumnName, lowerColumnName}
        }
        return layerConfig
      }) || []

    export const normalizeConfig = (config: Config): Config => {
      const dataProperties: Partial<Config> = {}

      if (config) {
        const {fluxResponse, fromFluxResult, table} = config

        switch (config.layers?.[0]?.type) {
          case 'simple table':
            if (fromFluxResult) {
              dataProperties.fromFluxResult = fromFluxResult
            } else if (fluxResponse) {
              dataProperties.fluxResponse = fluxResponse
            } else {
              dataProperties.fromFluxResult = {
                table: newTable(0),
                fluxGroupKeyUnion: [],
                resultColumnNames: [],
              }
            }
            break

          case 'table':
            dataProperties.fluxResponse = fluxResponse ? fluxResponse : ''
            break

          default:
            Iif (table) {
              dataProperties.table = table
            } else Iif (fluxResponse) {
              dataProperties.fluxResponse = fluxResponse
            } else {
              dataProperties.table = newTable(0)
            }
        }
      }

      return {
        ...config,
        ...dataProperties,
        layers: normalizeLayers(config?.layers),
      }
    }