2022-10-27 10:43:43 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
|
|
|
|
const printStats = function(path, timeToRead) {
|
|
|
|
fs.stat(path, (err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log("Size: " + stats.size)
|
|
|
|
console.log("Last change: " + stats.mtime)
|
|
|
|
console.log("Number of lines: " + (data.length - 1))
|
|
|
|
console.log("Time to read: " + timeToRead)
|
|
|
|
fs.time
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const csvToJson = function(data) {
|
|
|
|
data = data.split("\n")
|
|
|
|
attributes = data.shift()
|
|
|
|
attributes = attributes.split(",")
|
|
|
|
data = data.map(element => {
|
|
|
|
element = element.split(",")
|
|
|
|
const obj= {}
|
|
|
|
for (let i = 0; i < attributes.length; i++) {
|
|
|
|
obj[attributes[i]] = element[i]
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
})
|
2022-10-27 10:47:06 +02:00
|
|
|
return JSON.stringify(data)
|
2022-10-27 10:43:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const startTime = Date.now()
|
|
|
|
let data = fs.readFileSync('./csv/population.csv', 'utf8')
|
|
|
|
const endTime = Date.now()
|
|
|
|
printStats('./csv/population.csv', endTime - startTime)
|
2022-10-27 10:47:06 +02:00
|
|
|
fs.writeFileSync('./json/population.json', csvToJson(data))
|
2022-10-27 10:43:43 +02:00
|
|
|
|