const fs = require('fs'); const input = fs.readFileSync('./Chris S. Coaching Tracker - Weekly Tracker.csv', 'utf8'); const rows = []; const week = { 'Chest': null, 'Right thigh': null, 'Left thigh': null, 'Waist': null, 'Left biceps': null, 'Right biceps': null, } let year = 2023; input.split("\n").forEach((line) => { if (line.includes(' 1 Jan')) { year++; } const map = { 'R Arm': 'Right biceps', 'L Arm': 'Left biceps', 'Navel': 'Waist', 'Chest': 'Chest', 'R Leg': 'Right thigh', 'L Leg': 'Left thigh', } for (let key in map) { const val = map[key]; const match = line.match(new RegExp(`${key},([^,]+)`)); if (match) { week[val] = (1*match[1]).toPrecision(3); } } if (line.startsWith('"Sun')) { const match = line.match(/^"(Sun[^"]+)\s*"/)[1]; const date = new Date(`${match} UTC`); date.setFullYear(year); for (let key in week) { const val = week[key]; rows.push(`BODYMEASURE,${date.toJSON().split('T')[0]},${key},${val},cm`); } } }); ['Weight', 'Weight(1)', 'Weight(2)', 'Weight(3)'].forEach((name) => { const weightInput = fs.readFileSync(`./${name}.csv`, 'utf8').split("\n"); weightInput.shift(); for (let i = 0; i < weightInput.length;) { if (!weightInput[i] || !weightInput[i + 1]) { i++; continue; } const date = new Date(`${weightInput[i].substring(2, weightInput[i].length - 2)} UTC`); const [time, weight, change, bmi, fat, muscle, bone, water] = weightInput[i + 1].split(',') if (weight && weight !== '--') { const [kg, units] = weight.split(' '); rows.push(`BODYMEASURE,${date.toJSON().split('T')[0]},Weight,${kg},${units}`); } if (fat && fat !== '--') { const [perc, units] = fat.split(' '); rows.push(`BODYMEASURE,${date.toJSON().split('T')[0]},Fat,${perc},${units}`); } if (muscle && muscle !== '--' && weight && weight !== '--') { const [kg, units] = muscle.split(' '); const weightKg = weight.split(' ')[0]; const perc = (100 * kg / weightKg).toPrecision(3); rows.push(`BODYMEASURE,${date.toJSON().split('T')[0]},Muscles,${perc},%`); } if (water && water !== '--') { const [perc, units] = water.split(' '); rows.push(`BODYMEASURE,${date.toJSON().split('T')[0]},Water,${perc},${units}`); } i++; while (weightInput[i] && weightInput[i][0] !== '"') { i++; } } }); rows.sort(); rows.unshift('TABLE,DATE,BODYPART_NAME,MEASURE,UNIT'); fs.writeFileSync('./fnfImport.csv', rows.join("\n"));