-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorterV1.js
65 lines (55 loc) · 1.34 KB
/
sorterV1.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
function isTag(s) {
if( s.startsWith("Tag"))
return true;
return false;
}
function getTag(s) {
var foo = s.split("=")[1].trim().split('"')
tagMarker = foo[0].trim();
tag = foo[1].trim();
return {"tag": tag, "marker": tagMarker}
}
function isTagged(s, dict) {
var mTag = s.slice(-2),
res = {};
res["result"] = false;
res["tag"] = "";
dict.forEach((tagInfo, idx) => {
if(tagInfo["marker"] == mTag) {
res["result"] = true;
res["tag"] = tagInfo["tag"];
}
});
return res
}
// Run
var lines = require('fs').readFileSync('input.txt', 'utf8').split('\n').filter(Boolean),
tags = [],
output = {}; //This is an empty object
lines.forEach((line, idx) => {
if( isTag(line) ) {
tags.push( getTag(line) );
}
});
tags.forEach((tag, idx) => {
output[tag["tag"]] = [];
});
lines.forEach((line, idx) => {
var check = isTagged(line, tags);
if( check["result"] === true ) {
output[check["tag"]].push(line.slice(0, -2));
}
});
var keys = Object.keys(output),
stream = require('fs').createWriteStream("output.txt");
keys.sort();
stream.once('open', function(fd) {
for( var i = 0; i < keys.length; i++) {
var key = keys[i],
vals = output[key];
stream.write(key.concat(':\n'));
vals.map(x => stream.write(x.concat('\n')));
stream.write('\n');
}
stream.end()
});