-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (50 loc) · 1.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function BisectingBetween (chars) {
if (!(this instanceof BisectingBetween)) { return new BisectingBetween(chars) }
var bnum = require('bisecting-numbers')(chars)
var between = function (lo, hi) {
// base cases
if (!lo && !hi) {
return bnum.inc(bnum.zero())
}
if (lo === between.lo && hi === between.hi) {
return bnum.inc(bnum.zero())
}
if (!lo) {
lo = between.lo
}
if (!hi) {
hi = between.hi
}
// right and left edges
if (hi === between.hi) {
return bnum.inc(lo)
}
if (lo === between.lo) {
return bnum.dec(hi)
}
// invariant
if (bnum.compare(lo, hi) >= 0) {
throw new Error(lo + ' is larger than ' + hi)
}
// there is space in between lo and hi
var loInc = bnum.inc(lo)
if (bnum.compare(loInc, hi) === -1 && loInc !== between.lo) {
return loInc
}
var lolen = lo.split('.').length
var hilen = hi.split('.').length
// no space between lo and hi ==> BRANCH!
if (lolen === hilen) {
return bnum.bisect(lo)
}
// hi is in a higher bisection space than lo, so decrement hi in its own bisection space
if (lolen < hilen) {
return bnum.dec(hi)
}
}
between.lo = 'SPECIAL LO VALUE'
between.hi = 'SPECIAL HI VALUE'
between.numbers = bnum
return between
}
module.exports = BisectingBetween