const fs = require('fs');
const jsonfile = require('jsonfile');
const readline = require('readline');
const removeDoubleQuotes = (value) => value.replaceAll('"', '');
const txtToJson = (filename, columnNameMapping = {}, jsonFilePath) => {
const txtFilePath = `./${filename}.txt`;
jsonFilePath = jsonFilePath || `./${filename}.json`;
let entries = [],
i = 0,
lineValues;
const mappedColumnIndexes = Object.keys(columnNameMapping).map((index) =>
parseInt(index)
);
readline
.createInterface({
input: fs.createReadStream(txtFilePath),
output: process.stdout,
terminal: false,
})
.on('line', function (line) {
lineValues = line.split('\t');
if (i !== 0) {
entries.push(
lineValues.reduce((entry, value, valueIndex) => {
if (mappedColumnIndexes.includes(valueIndex)) {
entry[columnNameMapping[valueIndex]] = removeDoubleQuotes(value);
}
return entry;
}, {})
);
}
i++;
})
.on('close', function () {
console.log(`Writing ${i} entries to ${jsonFilePath}`);
jsonfile.writeFile(jsonFilePath, entries, { spaces: 2 }, function (err) {
if (err) {
console.error(err);
}
});
});
};
txtToJson(
'cities1000',
{
8: 'country',
1: 'name',
4: 'lat',
5: 'lng',
10: 'admin1',
11: 'admin2',
},
'./cities.json'
);
txtToJson(
'admin1CodesASCII',
{
0: 'code',
1: 'name',
},
'./admin1.json'
);
txtToJson(
'admin2Codes',
{
0: 'code',
1: 'name',
},
'./admin2.json'
);