forked from vscode-kubernetes-tools/vscode-kubernetes-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputUtils.ts
22 lines (22 loc) · 941 Bytes
/
outputUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Parse column based output which is seperated by whitespace(s) from kubectl or similar sources
* for example, kubectl get po
* @param lineOutput raw output with headers from kubectl or similar sources
* @param columnSeparator a regex for the column separators
* @return array of objects with key as column header and value
*/
export function parseLineOutput(lineOutput: string[], columnSeparator: RegExp): { [key: string]: string }[] {
const headers = lineOutput.shift();
if (!headers) {
return [];
}
const parsedHeaders = headers.toLowerCase().replace(columnSeparator, '|').split('|');
return lineOutput.map((line) => {
const lineInfoObject = {};
const bits = line.replace(columnSeparator, '|').split('|');
bits.forEach((columnValue, index) => {
lineInfoObject[parsedHeaders[index].trim()] = columnValue.trim();
});
return lineInfoObject;
});
}