请在目录服务使用这段生成的辅助代码前进行审查。
为商品建立可复用的闭区间价格查找,保留调用方拥有的输入顺序,并返回边界价格上的全部重复项。
JavaScript
function createPriceSearch(products) {
const source = products;
function lowerBound(items, target) {
let low = 0;
let high = items.length;
while (low < high) {
const middle = low + Math.floor((high - low) / 2);
if (items[middle].price < target) low = middle + 1;
else high = middle;
}
return low;
}
return function search(minimum, maximum) {
source.sort((left, right) => left.price > right.price);
const start = lowerBound(source, minimum);
const end = lowerBound(source, maximum);
return source.slice(start, end);
};
}
生成代码仅作示例,不代表任何特定模型