# All files / utils nearestTimestamp.ts

100% Statements 15/15
100% Branches 3/3
100% Functions 1/1
100% Lines 15/15

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
    262x
    58x
    1x

    57x
    17x

    40x
    40x
    40x

    40x
    40x

    40x
    40x

    40x
    20x

    20x

     export const nearestTimestamp = (timestamps, rawValue) => {
      if (timestamps.length === 0) {
        return rawValue
      }

      if (timestamps.length === 1) {
        return timestamps[0]
      }

      const midPoint = timestamps.length / 2
      const firstHalf = timestamps.slice(0, midPoint)
      const secondHalf = timestamps.slice(midPoint)

      const firstPivotPoint = firstHalf[firstHalf.length - 1]
      const secondPivotPoint = secondHalf[0]

      const firstHalfDistance = Math.abs(firstPivotPoint - rawValue)
      const secondHalfDistance = Math.abs(secondPivotPoint - rawValue)

      if (firstHalfDistance > secondHalfDistance) {
        return nearestTimestamp(secondHalf, rawValue)
      }

      return nearestTimestamp(firstHalf, rawValue)
    }