Skip to content

Instantly share code, notes, and snippets.

@claudiohilario
Created April 28, 2022 23:30
Show Gist options
  • Save claudiohilario/4dcebbca3c0f0440625e08d2392a9146 to your computer and use it in GitHub Desktop.
Save claudiohilario/4dcebbca3c0f0440625e08d2392a9146 to your computer and use it in GitHub Desktop.
Convert DATA Tanita BC-601 to JSON (NodeJs)
/**
* Tanita BC-601 (Source of Interpretation: https://github.com/cab938/tanita)
*
* Usage example: import-BC601.js ~/inputFile.csv ~/outputFile.json
*/
const fs = require("fs");
const [inputFile, outputFile] = process.argv.slice(2);
const dictionary = {
Wk: "Body mass",
MI: "Body mass index (BMI)",
MO: "Model",
DT: "Measurement Date",
Ti: "Measurement Time",
GE: "Gender",
AG: "Age",
Hm: "Height",
AL: "Activity Level",
FW: "Global fat %",
Fr: "Arm fat (right) %",
Fl: "Arm fat (left) %",
FR: "Leg fat (right) %",
FL: "Leg fat (left) %",
FT: "Torso fat %",
mW: "Global muscle %",
mr: "Arm muscle (right) %",
ml: "Arm muscle (left) %",
mR: "Leg muscle (right)%",
mL: "Leg muscle (left) %",
mT: "Torso muscle %",
bw: "Estimated bone mass",
IF: "Visceral fat rating",
rA: "Estimated metabolic age",
rD: "Daily calorie intake (DCI)",
ww: "Global body water %",
};
const rowsData = [];
const allFileContents = fs.readFileSync(inputFile, "utf-8");
allFileContents.split(/\r?\n/).forEach((line) => {
const arrData = line.split(",");
const rowData = {};
arrData.forEach((elem, index) => {
const isEvenNumber = (index + 1) % 2 == 0;
if (!isEvenNumber) {
const field = elem;
const value = arrData[index + 1];
const isDictionaryField = Object.prototype.hasOwnProperty.call(
dictionary,
field
);
if (isDictionaryField) {
rowData[dictionary[field]] = value.replace(/"/g, "");
}
}
});
rowsData.push(rowData);
});
fs.writeFileSync(outputFile, JSON.stringify(rowsData, null, 2), "utf-8");
console.log(`Output file: ${outputFile}`);
const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(`The script uses approximately ${Math.round(used * 100) / 100} MB`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment