]> git.r.bdr.sh - rbdr/dotfiles/blob
8eb45675149256ea7f2acc8e2f638225f7a660a8
[rbdr/dotfiles] /
1 'use strict';
2
3 var toPosInt = require('../../number/to-pos-integer')
4 , callable = require('../../object/valid-callable')
5 , value = require('../../object/valid-value')
6
7 , floor = Math.floor;
8
9 module.exports = function (compareFn) {
10 var length, low, high, middle;
11
12 value(this);
13 callable(compareFn);
14
15 length = toPosInt(this.length);
16 low = 0;
17 high = length - 1;
18
19 while (low <= high) {
20 middle = floor((low + high) / 2);
21 if (compareFn(this[middle]) < 0) high = middle - 1;
22 else low = middle + 1;
23 }
24
25 if (high < 0) return 0;
26 if (high >= length) return length - 1;
27 return high;
28 };