This gist implements a generic binary search. It accepts an simple array, a matrix or an array of objects. It demands a presorted array. It works with ascending or descending order, in exact or approximate way.
binarySearch(key, arr [, order, exact, func ])
It searchs key in array arr and returns the index of first element >= key, if it is an ascending ordered array. If it does not exist, returns -1.
if order is omitted or >=0, assumes a ascending array, as described above.
if order <0, assumes a descending array. In this case, it looks for the first element <= key
if exact is omitted, 0 or false, it assumes approximate binary search, as above. In this case, generally, the goal is inserting this searched element in array.
if exact is 1 or true, it assumes an exact binary search, i.e, returns the index only with precise key in array.
If func is omitted, it considers arr as a simple array and the searched element is just an array element.
if not, func should be a function (anonymous or named) with 2 parameters: the array itself and the current index.
Generally, using function means that the array contains objects and this function will refer to some object field.