-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin-script.js
executable file
·97 lines (81 loc) · 2.48 KB
/
join-script.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
const _ = require('lodash');
const fs = require('fs');
const csv = require('csvtojson');
const converter = require('json-2-csv');
function printUsage(exitCode) {
console.log('');
console.log(' Joins data in source CSV files on Address_Number and Street fields and writes output as CSV.');
console.log('');
console.log(' Usage: join-script <input-file-1.csv> <input-file-1.csv> <output-file.csv>');
process.exit(exitCode);
}
if (process.argv.length !== 5) {
console.error('Invalid command line.');
printUsage(1);
}
const inputFileNames = [process.argv[2], process.argv[3]];
const outputFileName = process.argv[4];
if (fs.existsSync(outputFileName)) {
console.error('Error: output file exists.');
printUsage(1);
}
console.log('Joining data in', inputFileNames);
const inputFileContents = [];
return readInputFileContents()
.then(function() {
let noMatchCount = 0;
_.each(inputFileContents[0], function(jsonObj) {
const match = _.find(inputFileContents[1], { Address_Number: jsonObj.Address_Number, Street: jsonObj.Street });
if (!match) {
noMatchCount++;
} else {
_.extend(jsonObj, match);
}
});
console.log(`No matches for ${noMatchCount} records.`);
converter.json2csv(inputFileContents[0], function(error, csv) {
if (error) {
console.log('Error converting output to CSV:', error);
printUsage(1);
}
console.log('Writing output to', outputFileName);
fs.writeFileSync(outputFileName, csv);
}, {
checkSchemaDifferences: false
});
})
.catch(function(error) {
console.log('Error:', error);
printUsage(1);
})
function readInputFileContents() {
const promises = [];
_.each(inputFileNames, function(fileName) {
promises.push(readCsvFile(fileName)
.then(function(fileJson) {
inputFileContents.push(fileJson);
}));
});
return Promise.all(promises);
}
function readCsvFile(fileName) {
return new Promise(function(resolve, reject) {
const fileJson = [];
const parsed = csv().fromFile(fileName)
.on('json', (jsonObj) => {
if (jsonObj.Street && !_.endsWith(jsonObj.Street, ' Street')) {
jsonObj.Street += ' Street';
}
fileJson.push(jsonObj);
})
.on('done', (error) => {
if (error) {
console.error('Error parsing CSV file', fileName, error);
return reject(error);
} else {
return resolve(fileJson);
}
});
})
}