-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (75 loc) · 2.03 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var NumBase = require('numbase')
var defined = require('defined')
function BisectingNumberSystem (chars) {
if (!(this instanceof BisectingNumberSystem)) { return new BisectingNumberSystem(chars) }
var chars = defined(chars, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
var numbase = new NumBase(chars)
this.zero = function () {
return chars.charAt(0)
}
// a==b => 0, a<b => -1, a>b => 1
this.compare = function (a, b) {
return compare(numbase, a, b)
}
this.inc = function (v) {
// Increment the suffix only
if (v.indexOf('.') !== -1) {
return prefix(v) + this.inc(suffix(v))
}
return numbase.encode(parseInt(numbase.decode(v), 10) + 1)
}
this.dec = function (v) {
// Decrement the suffix only
if (v.indexOf('.') !== -1) {
return prefix(v) + this.dec(suffix(v))
}
return numbase.encode(parseInt(numbase.decode(v), 10) - 1)
}
var prefix = function (v) {
if (v.indexOf('.') === -1) {
return ''
} else {
return v.substring(0, v.lastIndexOf('.') + 1)
}
}
var suffix = function (v) {
if (v.indexOf('.') === -1) {
return v
} else {
return v.substring(v.lastIndexOf('.') + 1)
}
}
this.bisect = function (v) {
return v + '.' + chars.charAt(0)
}
}
// return a list of the .-delimited segments of a number
function segments (v) {
return v.split('.')
}
function compare (numbase, a, b) {
if (a && !b) return 1
if (!a && b) return -1
if (a.indexOf('.') !== -1 || b.indexOf('.') !== -1) {
var as = segments(a)
var bs = segments(b)
for (var i = 0; i < Math.min(as.length, bs.length); i++) {
var result = compare(numbase, as[i], bs[i])
if (result !== 0) {
return result
}
}
return as.length - bs.length
} else {
var av = parseInt(numbase.decode(a), 10)
var bv = parseInt(numbase.decode(b), 10)
if (av < bv) {
return -1
} else if (av > bv) {
return 1
} else {
return 0
}
}
}
module.exports = BisectingNumberSystem