# All files / utils newTable.ts

87.03% Statements 47/54
68.18% Branches 15/22
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
    16x

    16x
    812x

    812x

    812x

    16x
    13x

    16x
    5013697x

    5013697x
    7x

    5013690x

    5013690x

    5013690x

    186x

    6x

    20x

    5013478x

    16x
    35x

    35x

    35x

    16x
    280x

    280x
    48x

    232x

    16x
    31x

    31x

    31x

    16x

    679x

    679x

    679x

    679x

    679x

    16x

    133x

    16x
    4x

    4x
    2x

    2x
    1x

    1x

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

    // Don't export me!
    class EmpiricalTable 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 EmpiricalTable(this.length)

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

        return table
      }
    }

    export const newTable = (length: number): Table => new EmpiricalTable(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)
    }