-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathencoding.js
More file actions
30 lines (25 loc) · 782 Bytes
/
encoding.js
File metadata and controls
30 lines (25 loc) · 782 Bytes
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
'use strict'
exports.create = function createEncoding (options) {
options || (options = {})
if (options.ttlEncoding) return options.ttlEncoding
const PATH_SEP = options.separator
const INITIAL_SEP = options.sub ? '' : PATH_SEP
function encodeElement (e) {
// transform dates to timestamp strings
return String(e instanceof Date ? +e : e)
}
return {
buffer: false,
encode: function (e) {
// TODO: reexamine this with respect to level-sublevel@6's native codecs
if (Array.isArray(e)) {
return Buffer.from(INITIAL_SEP + e.map(encodeElement).join(PATH_SEP))
}
return Buffer.from(encodeElement(e))
},
decode: function (e) {
// TODO: detect and parse ttl records
return e.toString('utf8')
}
}
}