-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (60 loc) · 1.42 KB
/
index.js
File metadata and controls
76 lines (60 loc) · 1.42 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const Long = require('long');
const dictionary = require(process.env.DIAMETER_DICTIONARY || 'diameter-dictionary');
const toCamelMap = {};
const fromCamelMap = {};
dictionary.avps.forEach(avp => {
const key = avp.name;
const camelized = key
.split('-')
.map((token, idx) => {
const lc = token.toLowerCase();
return idx
? lc[0].toUpperCase() + lc.slice(1)
: lc;
})
.join('');
toCamelMap[key] = camelized;
fromCamelMap[camelized] = key;
});
function toObject(avpList) {
const obj = {};
avpList.forEach(avp => {
const key = toCamelMap[avp[0]];
let value = avp[1];
if (value instanceof Array) {
value = toObject(value);
}
if (obj.hasOwnProperty(key)) {
obj[key] = [].concat(obj[key], value);
} else {
obj[key] = value;
}
});
return obj;
}
function fromObject(obj) {
const avpList = [];
Object.keys(obj).forEach(key => {
let value = obj[key];
const decamelizedKey = fromCamelMap[key];
function pushValue(value) {
avpList.push([
decamelizedKey,
value instanceof Object && !(value instanceof Long) && !(value instanceof Buffer)
? fromObject(value)
: value
]);
}
if (value instanceof Array) {
value.forEach(pushValue);
} else {
pushValue(value);
}
});
return avpList;
}
module.exports = Object.freeze({
toObject,
fromObject
});