# All files / utils extrema.ts

32.35% Statements 11/34
16.67% Branches 1/6
60% Functions 3/5
34.62% Lines 9/26

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

    1x

    1x

    1x

    1x
    24x

    8x

    16x

    8x

     import {extent} from 'd3-array'

    import {flatMap} from './flatMap'

    export const minBy = <T>(f: (x: T) => number, xs: T[]): T => {
      let minX = null
      let minDistance = Infinity

      for (const x of xs) {
        const distance = f(x)

        if (distance < minDistance) {
          minX = x
          minDistance = distance
        }
      }

      return minX
    }

    export const maxBy = <T>(f: (x: T) => number, xs: T[]): T => {
      let maxX = null
      let maxDistance = -Infinity

      for (const x of xs) {
        const distance = f(x)

        if (distance > maxDistance) {
          maxX = x
          maxDistance = distance
        }
      }

      return maxX
    }

    /*
      Given a list of lists, return the minimum and maximum value across all items
      in all lists as a tuple.

      Returns `null` if unsuccessful (e.g. if passed empty lists).
    */
    export const extentOfExtents = (
      ...data: number[][]
    ): [number, number] | null => {
      const result = extent(flatMap(data, d => extent(d)))

      Iif (result.some(x => x === undefined)) {
        return null
      }

      return result
    }