forked from NikhilM98/nodejs-helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperMethods.js
53 lines (46 loc) · 1.4 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
const YAML = require("yaml");
const flattenValuesToString = function(values) {
const traverse = (current, path = "") => {
if (Array.isArray(current)) {
return current.flatMap((item, index) => traverse(item, `${path}[${index}]`));
}
if (typeof current === "object" && current !== null) {
return Object.entries(current).flatMap(([key, value]) => {
const newPath = path ? `${path}.${key}` : key;
return traverse(value, newPath);
});
}
return [`${path}=${current}`];
};
return traverse(values).join(",");
};
function parseResponseToJson(rawData) {
try {
const parsedDocuments = YAML.parseAllDocuments(rawData);
const 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 {
const 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,
parseResponseToJson : parseResponseToJson,
parseJson : parseJson,
};