Code coverage report for formatting.ts

# All files formatting.ts

27.65% Statements 26/94
100% Branches 4/4
50% Functions 1/2
27.65% Lines 26/94

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
    941x
    1x
    3x
    3x
    2x
    1x
    1x
    1x
    1x
    1x
    3x
    1x
    1x
    3x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

     import { filterNeedsEscapeRegexp, streamRegexp } from "./regexp"

    export function formatBitrate(bitrate: number | string): string {
      if (typeof bitrate === 'number') {
        if (bitrate < 1024) {
          // Assume the user means kbps
          return `${bitrate}k`
        } else {
          return `${bitrate}`
        }
      } else {
        return bitrate
      }
    }

    interface OutputFilterSpec {
      filter: string,
      options?: string | string[] | { [key: string]: string },
      input?: string,
      inputs?: string[],
      output?: string,
      outputs?: string[]
    }

    export type OutputFilter = string | OutputFilterSpec

    export function formatFilters(specs: OutputFilter[]): string[] {
      /* Filter syntax:

          filter := inputs? filterspec outputs?

          inputs := input inputs?
          input := '[' input-name ']'

          outputs := output outputs?
          output := '[' output-name ']'

          filterspec := filter-name ('=' filterargs)?
          filterargs := filterarg (':' filterargs)?
          filterarg := arg-value | (arg-name '=' arg-value)
       */

      return specs.map((spec) => {
        if (typeof spec === 'string') {
          return spec
        }

        if (spec.input) {
          spec.inputs = [spec.input]
        }

        let inputs = (spec.inputs || []).map((stream) => stream.replace(streamRegexp, '[$1]')).join('')

        let options = ''

        if (spec.options) {
          if (typeof spec.options === 'string') {
            options = `=${spec.options}`
          } else if (Array.isArray(spec.options)) {
            let optionStrings = spec.options
              .map((option) => {
                if (option.match(filterNeedsEscapeRegexp)) {
                  return `'${option}'`
                } else {
                  return option
                }
              })

            options = `=${optionStrings.join(':')}`
          } else {
            let optionStrings = Object.entries(spec.options)
              .map(([key, value]) => {
                if (value.match(filterNeedsEscapeRegexp)) {
                  value = `'${value}'`
                }

                return `${key}=${value}`
              })

            options = `=${optionStrings.join(':')}`
          }
        }

        let filter = `${spec.filter}${options}`

        if (spec.output) {
          spec.outputs = [spec.output]
        }

        let outputs = (spec.outputs || []).map((stream) => stream.replace(streamRegexp, '[$1]')).join('')

        return `${inputs}${filter}${outputs}`
      })
    }