-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathutils.js
75 lines (60 loc) · 1.63 KB
/
utils.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
'use strict';
var d64 = require('d64');
var nonBufferPrefix = 'n:'
var bufferPrefix = 'b:'
function keyEq(k1, k2) {
return (k1 || '').toString('hex') === (k2 || '').toString('hex');
}
function keyNeq(k1, k2) {
return (k1 || '').toString('hex') !== (k2 || '').toString('hex');
}
function keyGt(k1, k2) {
return (k1 || '').toString('hex') > (k2 || '').toString('hex');
}
function keyGte(k1, k2) {
return (k1 || '').toString('hex') >= (k2 || '').toString('hex');
}
function keyLt(k1, k2) {
return (k1 || '').toString('hex') < (k2 || '').toString('hex');
}
function keyLte(k1, k2) {
return (k1 || '').toString('hex') <= (k2 || '').toString('hex');
}
exports.keyEq = keyEq;
exports.keyNeq = keyNeq;
exports.keyGt = keyGt;
exports.keyGte = keyGte;
exports.keyLt = keyLt;
exports.keyLte = keyLte;
// taken from rvagg/memdown commit 2078b40
exports.sortedIndexOf = function(arr, item) {
var low = 0;
var high = arr.length;
var mid;
while (low < high) {
mid = (low + high) >>> 1;
if (keyLt(arr[mid], item)) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
};
exports.encode = function encode (vals) {
return Array.isArray(vals) ? vals.map(encodeOne) : encodeOne(vals)
}
exports.decode = function decode (strings) {
return Array.isArray(strings) ? strings.map(decodeOne) : decodeOne(strings)
}
function encodeOne (val) {
return Buffer.isBuffer(val)
? bufferPrefix + d64.encode(val)
: nonBufferPrefix + val
}
function decodeOne (str) {
if (str.slice(0, bufferPrefix.length) === bufferPrefix) {
return d64.decode(str.slice(bufferPrefix.length))
}
return str.slice(nonBufferPrefix.length)
}