# All files / utils nearestTimestamp.ts

86.67% Statements 13/15
66.67% Branches 4/6
100% Functions 1/1
86.67% Lines 13/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
    261x
    6x

    6x
    3x

    3x
    3x
    3x

    3x
    3x

    3x
    3x

    3x

    3x

     export const nearestTimestamp = (timestamps, rawValue) => {
      Iif (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)

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

      return nearestTimestamp(firstHalf, rawValue)
    }