# All files / utils newTable.ts

87.04% Statements 47/54
78.13% Branches 25/32
100% Functions 10/10
86.79% Lines 46/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
    13x

    13x
    450x

    450x

    450x

    13x
    11x

    13x
    5010004x

    5010004x
    4x

    5010000x

    5010000x

    5010000x

    147x

    3x

    12x

    5009838x

    13x
    27x

    27x

    27x

    13x
    174x

    174x
    24x

    150x

    13x
    29x

    29x

    29x

    13x

    374x

    374x

    374x

    374x

    374x

    13x

    76x

    13x
    4x

    4x
    2x

    2x
    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]

        if (!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]

        Iif (!column) {
          return null
        }

        return column.name
      }

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

        if (!column) {
          return null
        }

        return column.type
      }

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

        Iif (!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 => {
      Iif (!config) {
        return newTable(0)
      }
      if (config.table) {
        return config.table
      }
      if (config.fluxResponse) {
        return fromFlux(config.fluxResponse).table
      }
      return newTable(0)
    }