-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (48 loc) · 1.34 KB
/
index.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
var re = {
connected: /^(\S+) connected (?:(\d+)x(\d+))?/,
disconnected: /^(\S+) disconnected/,
mode: /^\s+(\d+)x(\d+)\s+((?:\d+\.)?\d+)([* ]?)([+ ]?)/
};
module.exports = function (src) {
var lines = src.split('\n');
var query = {};
var last = null;
var index = 0;
lines.forEach(function (line) {
var m;
if (m = re.connected.exec(line)) {
query[m[1]] = {
connected: true,
modes: [],
index: index++
};
if (m[2] && m[3]) {
query[m[1]].width = parseInt(m[2]);
query[m[1]].height = parseInt(m[3]);
}
last = m[1];
}
else if (m = re.disconnected.exec(line)) {
query[m[1]] = {
connected: false,
modes: [],
index: index++
};
last = m[1];
}
else if (last && (m = re.mode.exec(line))) {
var r = {
width: m[1],
height: m[2],
rate: parseFloat(m[3])
};
query[last].modes.push(r);
if (m[4] === '*') query[last]['native'] = r;
if (m[5] === '+') query[last].current = r;
}
else {
last = null;
}
});
return query;
};