import { StandaloneColumn } from "." // Get the corresponding volume data for a particular point in time using simple binary searvh export function getCorrespondingVolumeData( date: Date, { items, getDate }: StandaloneColumn, ): T | undefined { let low = 0 let high = items.length - 1 while (low <= high) { const mid = Math.floor((low + high) / 2) const midBucket = items[mid] if (getDate(midBucket) === date) { return midBucket } else if (getDate(midBucket) < date) { if (mid + 1 < items.length && getDate(items[mid + 1]) > date) { return midBucket // The current bucket is the closest without going over } low = mid + 1 } else { high = mid - 1 } } if (high < 0) { return undefined // No bucket found before the point } // Return the closest bucket without going over the point's time return items[high] }