-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
47 lines (41 loc) · 1.24 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
'use strict'
const lexint = require('lexicographic-integer')
const Buffer = require('safe-buffer').Buffer
module.exports = function factory (enc, opts) {
const strict = opts && opts.strict
if (enc === 'hex') {
return {
type: 'lexicographic-integer/hex',
encode: maybeStrict(strict, function (n) {
return lexint.pack(n, 'hex')
}),
decode: lexint.unpack,
buffer: false
}
} else if (enc === 'buffer' || enc === 'binary') {
return {
type: 'lexicographic-integer/buffer',
encode: maybeStrict(strict, function (n) {
return Buffer.from(lexint.pack(n))
}),
decode: function (buf) {
// Hack because lexicographic-integer doesn't take a Buffer.
return lexint.unpack(Array.from(buf.values()))
},
buffer: true
}
} else {
throw new Error('First argument must be a valid encoding')
}
}
function maybeStrict (strict, encode) {
return strict ? function encodeStrict (n) {
if (typeof n !== 'number' || isNaN(n)) {
throw new TypeError('Argument must be a valid number')
}
if (n < 0 || n > Number.MAX_SAFE_INTEGER) {
throw new RangeError('Argument must be >= 0 < Number.MAX_SAFE_INTEGER')
}
return encode(n)
} : encode
}