forked from adibenmati/node-helm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helperMethods.js
102 lines (90 loc) · 2.96 KB
/
helperMethods.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const YAML = require('yaml')
const flattenValuesToString = function (values) {
var valuesString = '';
for (var valueSet in values) {
if (values.hasOwnProperty(valueSet)) {
if (Array.isArray(values[valueSet])) {
valuesString += `${valueSet}=${flattenArrayToString(values[valueSet])},`;
} else if (typeof values[valueSet] == "object") {
valuesString += flattenObjectToString(values[valueSet], `${valueSet}`)
} else {
valuesString += `${valueSet}=${values[valueSet]},`;
}
}
}
return valuesString;
};
const flattenObjectToString = function (obj, index = null) {
var objString = '';
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (Array.isArray(obj[i])) {
objString += `${index ? index + '.' : ''}${i}=${flattenArrayToString(obj[i])},`;
} else if (typeof obj[i] == "object") {
objString += `${flattenObjectToString(obj[i], `${index ? index + '.' : ''}${i}`)}`
} else {
objString += `${index ? index + '.' : ''}${i}=${obj[i]},`;
}
}
return objString;
}
const flattenArrayToString = function (arr) {
if(arr.length === 0) return ''
var arrString = '{';
for (var i in arr) {
if (!arr.hasOwnProperty(i)) continue;
arrString += `${arr[i]}`;
if (parseInt(i) !== arr.length - 1) arrString += ',';
}
arrString += '}'
return arrString;
}
const flattenObject = function (obj) {
var toReturn = {};
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == "object") {
var flatObject = flattenObject(obj[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
toReturn[i + "." + x] = flatObject[x];
}
} else {
toReturn[i] = obj[i];
}
}
return toReturn;
};
function parseResponseToJson(rawData) {
try {
var parsedDocuments = YAML.parseAllDocuments(rawData);
var jsonData = [];
if (typeof parsedDocuments == "object") {
for (var i=0; i<parsedDocuments.length; i++) {
jsonData.push(parsedDocuments[i].toJSON());
}
}
return jsonData;
}
catch(e) {
console.log("could not parse helm response with error: " + e.message);
//ignore
return rawData;
}
}
function parseJson(rawData) {
try {
var jsonData = JSON.parse(rawData);
return jsonData;
}
catch(e) {
console.log("could not parse helm response with error: " + e.message);
return rawData;
}
}
module.exports = {
flattenValuesToString : flattenValuesToString,
flattenObject : flattenObject,
parseResponseToJson : parseResponseToJson,
parseJson : parseJson
};