# All files / utils newTable.ts

53.7% Statements 29/54
40.63% Branches 13/32
60% Functions 6/10
52.83% Lines 28/53

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
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    1x

    1x
    6x

    6x

    6x

    1x

    1x
    33x

    33x

    33x

    33x

    33x

    33x

    1x

    1x
    384x

    384x

    384x

    1x

    1x

    5x

    5x

    5x

    5x

    5x

    1x

    1x

    1x

     import {Table, ColumnType, ColumnData, Config, FluxDataType} from '../types'
    import {fromFlux} from './fromFlux'

    // Don't export me!
    class SimpleTable implements Table {
      public readonly length: number = 0

      private columns: {
        [colKey: string]: {
          name: string
          fluxDataType: FluxDataType
          type: ColumnType
          data: ColumnData
        }
      } = {}

      constructor(length: number) {
        this.length = length
      }

      get columnKeys(): string[] {
        return Object.keys(this.columns)
      }

      getColumn(columnKey: string, columnType?: ColumnType): any[] | null {
        const column = this.columns[columnKey]

        Iif (!column) {
          return null
        }

        // Allow time columns to be retrieved as number columns
        const isWideningTimeType = columnType === 'number' && column.type === 'time'

        Iif (columnType && columnType !== column.type && !isWideningTimeType) {
          return null
        }

        switch (columnType) {
          case 'number':
            return column.data as number[]
          case 'time':
            return column.data as number[]
          case 'string':
            return column.data as string[]
          case 'boolean':
            return column.data as boolean[]
          default:
            return column.data as any[]
        }
      }

      getColumnName(columnKey: string): string {
        const column = this.columns[columnKey]

        if (!column) {
          return null
        }

        return column.name
      }

      getColumnType(columnKey: string): ColumnType {
        const column = this.columns[columnKey]

        Iif (!column) {
          return null
        }

        return column.type
      }

      getOriginalColumnType(columnKey: string): FluxDataType {
        const column = this.columns[columnKey]

        if (!column) {
          return null
        }

        return column.fluxDataType
      }

      addColumn(
        columnKey: string,
        fluxDataType: FluxDataType,
        type: ColumnType,
        data: ColumnData,
        name?: string
      ): Table {
        Iif (this.columns[columnKey]) {
          throw new Error('column already exists')
        }

        Iif (data.length !== this.length) {
          throw new Error(
            `expected column of length ${this.length}, got column of length ${data.length} instead`
          )
        }

        const table = new SimpleTable(this.length)

        table.columns = {
          ...this.columns,
          [columnKey]: {
            name: name || columnKey,
            fluxDataType,
            type,
            data,
          },
        }

        return table
      }
    }

    export const newTable = (length: number): Table => new SimpleTable(length)

    export const newTableFromConfig = (config: Config): Table => {
      if (!config) {
        return newTable(0)
      }
      if (config.table) {
        return config.table
      }
      if (config.fluxResponse) {
        return fromFlux(config.fluxResponse).table
      }
      return newTable(0)
    }