mauss
Version:
practical functions and reusable configurations
19 lines (18 loc) • 580 B
JavaScript
/**
* Binary search algorithm on a sorted array
* @returns the first item that passes the check
*/
export function binary(sorted, check) {
let start = 0, final = sorted.length - 1; // prettier-ignore
while (start <= final) {
const midpoint = (start + final) >> 1;
const current = sorted[midpoint];
const passes = check.item(current);
if (passes)
return passes(current);
const flag = check.pointer(current);
start = flag ? start : midpoint + 1;
final = flag ? midpoint - 1 : final;
}
return;
}