breakhours-transform/transform.js
2024-10-03 13:55:18 +02:00

37 lines
1.1 KiB
JavaScript

import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
function formatTime(decimalTime) {
const [hours, minutes] = decimalTime.split('.');
return `${hours.padStart(2, '0')}:${(minutes || '0').padEnd(2, '0')}`;
}
function csvToJson(filePath) {
const data = fs.readFileSync(filePath, 'utf8');
const rows = data.trim().split('\n');
const result = rows.map(row => {
const parts = row.split(',');
const start_time = formatTime(parts[0]);
const end_time = formatTime(parts[1]);
const break_hours = parseFloat(parts[2]).toFixed(2);
return {
start_time,
end_time,
break_hours
};
});
return JSON.stringify(result, null, 4);
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const csvFilePath = path.join(__dirname, './data/colorcrew_data.csv');
const jsonOutput = csvToJson(csvFilePath);
const outputFilePath = path.join(__dirname, './data/colorcrew_data_transformed.json');
fs.writeFileSync(outputFilePath, jsonOutput, 'utf8');