-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing-4.1.js
42 lines (33 loc) · 1014 Bytes
/
listing-4.1.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
"use strict";
const file = require('./toolkit/file.js');
function parseCustomData (textFileData) {
const regex = /(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)\|(.*)$/gm;
var rows = [];
var m;
while ((m = regex.exec(textFileData)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
m.shift();
rows.push(m);
}
var header = rows.shift();
var data = rows.map(row => {
var hash = {};
for (var i = 0; i < header.length; ++i) {
hash[header[i]] = row[i];
}
return hash;
});
return data;
};
file.read("./data/earthquakes.txt")
.then(textFileData => parseCustomData(textFileData))
.then(data => {
console.log(data);
})
.catch(err => {
console.error("An error occurred.");
console.error(err.stack);
});