react-infinite
Version:
A browser-ready efficient scrolling container based on UITableView
40 lines (32 loc) • 751 B
JavaScript
var opts = {
CLOSEST_LOWER: 1,
CLOSEST_HIGHER: 2
}
var binaryIndexSearch = function(array, item, opt) {
var index;
var high = array.length - 1,
low = 0,
middle,
middleItem;
while(low <= high) {
middle = low + Math.floor((high - low) / 2);
middleItem = array[middle];
if (middleItem === item) {
return middle;
} else if (middleItem < item) {
low = middle + 1;
} else if (middleItem > item) {
high = middle - 1;
}
}
if (opt === opts.CLOSEST_LOWER && low > 0) {
index = low - 1;
} else if (opt === opts.CLOSEST_HIGHER && high < array.length - 1) {
index = high + 1;
}
return index;
};
module.exports = {
binaryIndexSearch: binaryIndexSearch,
opts: opts
}