-
Notifications
You must be signed in to change notification settings - Fork 0
/
smaps_parser.ts
46 lines (36 loc) · 889 Bytes
/
smaps_parser.ts
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
type SmapsInfo = {
value: any;
unit: string;
};
type Retval = { [key: string]: SmapsInfo };
export default function PidSmapsParse(raw: string): Retval {
const lines = raw.split("\n");
const t: Retval = {};
for (let i = 0; i < lines.length; i++) {
let parts = lines[i].split(" ");
parts[0] = parts[0].replace(":", "").trim();
parts = parts.filter((p) => p !== "");
if (typeof parts[0] === "undefined") {
continue;
}
if (parts.length > 3) {
continue;
}
let value;
if (typeof parts[1] === "string" && isNaN(parts[1] as any)) {
value = parts[1];
} else {
value = parseInt(parts[1]);
}
const unit = typeof parts[2] === "undefined" ? "" : parts[2];
if (t[parts[0]]) {
t[parts[0]].value += value;
} else {
t[parts[0]] = {
value,
unit,
};
}
}
return t;
}