-
Notifications
You must be signed in to change notification settings - Fork 25
/
lookup.js
64 lines (59 loc) · 1.92 KB
/
lookup.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
// Node.js API.
const util = require('util')
// Format source code maintaining indentation.
const $ = require('programmatic')
module.exports = function (definition) {
const lookup = []
function seek (field) {
switch (field.type) {
case 'bigint':
case 'integer':
if (field.lookup != null) {
if (lookup.length == field.lookup.index) {
if (Array.isArray(field.lookup.values)) {
lookup[field.lookup.index] = field.lookup.values
} else {
const reverse = {}
for (const key in field.lookup.values) {
reverse[field.lookup.values[key]] = key
}
lookup[field.lookup.index] = {
forward: field.lookup.values,
reverse: reverse
}
}
}
}
if (field.fields != null) {
field.fields.forEach(seek)
}
break
case 'switch':
field.cases.forEach(when => when.fields.forEach(seek))
break
case 'conditional':
field.serialize.conditions.forEach(when => when.fields.forEach(seek))
if (!field.split) {
field.parse.conditions.forEach(when => when.fields.forEach(seek))
}
break
case 'absent':
case 'buffer':
break
case 'accumulator':
case 'literal':
case 'inline':
case 'fixed':
case 'lengthEncoded':
case 'terminated':
case 'structure':
field.fields.forEach(seek)
break
default: throw new Error(field.type)
}
}
for (const name in definition) {
seek(definition[name])
}
return util.inspect(lookup, { depth: null })
}