PageSourceSearch

https://cycle.tools/TVCharts/plugin-examples/helpers/closest-index.js

js cycle.tools collected 2026-09-27 10:38:48 UTC 1,194 bytes, 38 lines download raw bytes

1export class ClosestTimeIndexFinder {
2    constructor(sortedNumbers) {
3        this.numbers = sortedNumbers;
4        this.cache = new Map();
5    }
6    findClosestIndex(target, direction) {
7        const cacheKey = `${target}:${direction}`;
8        if (this.cache.has(cacheKey)) {
9            return this.cache.get(cacheKey);
10        }
11        const closestIndex = this._performSearch(target, direction);
12        this.cache.set(cacheKey, closestIndex);
13        return closestIndex;
14    }
15    _performSearch(target, direction) {
16        let low = 0;
17        let high = this.numbers.length - 1;
18        if (target <= this.numbers[0].time)
19            return 0;
20        if (target >= this.numbers[high].time)
21            return high;
22        while (low <= high) {
23            const mid = Math.floor((low + high) / 2);
24            const num = this.numbers[mid].time;
25            if (num === target) {
26                return mid;
27            }
28            else if (num > target) {
29                high = mid - 1;
30            }
31            else {
32                low = mid + 1;
33            }
34        }
35        return direction === 'left' ? low : high;
36    }
37}
38//# sourceMappingURL=closest-index.js.map

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.