-
Notifications
You must be signed in to change notification settings - Fork 11
/
monotonic.js
27 lines (25 loc) · 960 Bytes
/
monotonic.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
const whittle = require('whittle')
const Monotonic = {
toBigInt: promise => promise.split('/').map(part => BigInt(part)),
compare: whittle(function (left, right) {
for (let i = 0, I = Math.min(left.length, right.length); i < I; i++) {
const compare = (left[i] > right[i]) - (left[i] < right[i])
if (compare != 0) {
return compare
}
}
return left.length - right.length
}, promise => Monotonic.toBigInt(promise)),
isGovernment: promise => promise.endsWith('/0'),
incrementr: (promise, index) => {
const split = Monotonic.toBigInt(promise).reverse()
split[index] += 1n
return split.map(part => part.toString(10)).join('/')
},
increment: (promise, index) => {
const split = Monotonic.toBigInt(promise)
split[index] += 1n
return split.map(part => part.toString(10)).join('/')
}
}
module.exports = Monotonic