-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.js
More file actions
48 lines (45 loc) · 1.45 KB
/
decoder.js
File metadata and controls
48 lines (45 loc) · 1.45 KB
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
exports.__decode = (config, value) => {
const eson_data = JSON.parse(value)
return decode_eson_data(config, eson_data)
}
const decode_eson_data = (config, data) => {
if (data && data.constructor === Object) {
const _data = {}
let encoded_key
let encoded_value
for (encoded_key in data) {
encoded_value = data[encoded_key]
let [key, value] = decode_value(config, encoded_key, encoded_value)
if (
value &&
(Array.isArray(value) || value.constructor == Object)
) {
value = decode_eson_data(config, value)
}
if (!key) return value
_data[key] = value
}
return _data
}
if (Array.isArray(data)) {
return data.map(value => {
if (
value &&
(Array.isArray(value) || value.constructor == Object)
) {
value = decode_eson_data(config, value)
}
return value
})
}
return data
}
const decode_value = (config, encoded_key, encoded_value) => {
const key_parts = encoded_key.split("~")
if (key_parts.length !== 2) return [encoded_key, encoded_value]
const [name, key] = key_parts
const extension = config[name]
if (extension) return [key, extension.decode(encoded_value)]
console.warn(`Missing ESON extension ${name}`)
return [encoded_key, encoded_value]
}