forked from 21centurymotorcompany/bitd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.js
34 lines (31 loc) · 935 Bytes
/
encoding.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
const traverse = require('traverse')
const iconv = require('iconv-lite');
var encoding = function(subtree, encoding_schema) {
let copy = subtree;
traverse(copy).forEach(function(token) {
if (this.isLeaf) {
let encoding = "utf8";
let newVal = token;
// if the key is a special directive like '$in', traverse up the tree
// until we find a normal key that starts with b or s
let node = this;
if (/^([0-9]+|\$).*/.test(node.key)) {
while(!node.isRoot) {
node = node.parent;
if (/^b[0-9]+/.test(node.key)) {
break;
}
}
}
if (encoding_schema && encoding_schema[node.key]) {
encoding = encoding_schema[node.key];
}
if (/^b[0-9]+/.test(node.key)) {
newVal = iconv.encode(token, encoding).toString("base64");
}
this.update(newVal)
}
})
return copy;
}
module.exports = encoding;