Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@extra-array/bsearch-any

nodef92MIT2.9.12TypeScript support: included

Binary searches value in sorted array.

extra, array, bsearch, any, bsearchAny

readme

Binary searches value in sorted array. :running: :vhs: :package: :moon: :ledger:

Alternatives: bsearch, bsearchRight, bsearchAny, bsearchClosest.
This is part of package extra-array.

array.bsearchAny(x, v, [fc], [fm]);
// x:  an array (sorted)
// v:  search value
// fc: compare function (a, b)
// fm: map function (v, i, x)
// --> index of value | ~(index of closest value)
const array = require('extra-array');

var x = [1, 3, 3, 3, 5];
array.bsearchAny(x, 3);
// 2           ^ found

array.bsearchAny(x, 4);
// -5 (~4)           ^ not found, closest

var x = [1, -3, -3, -3, 5];
array.bsearchAny(x, 3, (a, b) => Math.abs(a) - Math.abs(b));
// 2             ^

array.bsearchAny(x, 3, null, v => Math.abs(v));
// 2             ^

references