# All files nearestTimestamp.ts

100% Statements 13/13
100% Branches 6/6
100% Functions 1/1
100% Lines 13/13

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
    44x
    1x

    43x
    14x

    29x

    29x
    29x

    29x
    29x

    29x
    12x

    17x

     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 = timestamps[midPoint - 1]
      const secondPivotPoint = timestamps[midPoint]

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

      if (firstHalfDistance > secondHalfDistance) {
        return nearestTimestamp(timestamps.slice(midPoint), rawValue)
      }

      return nearestTimestamp(timestamps.slice(0, midPoint), rawValue)
    }