-
Notifications
You must be signed in to change notification settings - Fork 3
/
keyzipper.js
68 lines (61 loc) · 1.28 KB
/
keyzipper.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
const KEY_DECOMPRESSION_MAP = {
a: 'altitude',
ac: 'activatorCallsign',
ao: 'activationCount',
c: 'comments',
d: 'code',
e: 'speed',
f: 'frequency',
hc: 'homeCallsign',
i: 'isActivator',
ic: 'isoCode',
l: 'callsign',
m: 'mode',
n: 'name',
o: 'continent',
p: 'points',
s: 'summit',
t: 'spotter',
ts: 'timeStamp',
ui: 'userID',
y: 'type'
}
let KEY_COMPRESSION_MAP = null
function compressKeys (obj) {
// Lazy init
if (KEY_COMPRESSION_MAP === null) {
KEY_COMPRESSION_MAP = {}
Object.keys(KEY_DECOMPRESSION_MAP).forEach(key => {
KEY_COMPRESSION_MAP[KEY_DECOMPRESSION_MAP[key]] = key
})
}
return mapKeys(obj, KEY_COMPRESSION_MAP)
}
function decompressKeys (obj) {
return mapKeys(obj, KEY_DECOMPRESSION_MAP)
}
function mapKeys (obj, map) {
if (obj === null) {
return null
} else if (Array.isArray(obj)) {
return obj.map(el => {
return mapKeys(el, map)
})
} else if (typeof obj === 'object' && !(obj instanceof Date)) {
let ret = {}
Object.keys(obj).forEach(key => {
let val = mapKeys(obj[key], map)
if (map[key]) {
ret[map[key]] = val
} else {
ret[key] = val
}
})
return ret
} else {
return obj
}
}
module.exports = {
compressKeys, decompressKeys
}