Skip to content

Instantly share code, notes, and snippets.

@rlingineni
Created October 29, 2018 00:43
Show Gist options
  • Save rlingineni/f562ee96c8b05ce07c3bd93144018a84 to your computer and use it in GitHub Desktop.
Save rlingineni/f562ee96c8b05ce07c3bd93144018a84 to your computer and use it in GitHub Desktop.
Sourcepath to Nested Object Map
//pass in a csv file
//record looks like this: { item: 'another/test/blank.cpp', org: 'tester', churn: '1' }
//convert the source path to a tree like this:
/*
{
"another": {
"test":{
"blank.cpp":{}
}
}
}
*/
const Papa = require("papaparse");
const fs = require("fs");
let FileMap = {};
SourcePathConverter("paths.csv");
//takes in a file path
function SourcePathConverter(CSVPath) {
let csv = fs.readFileSync(CSVPath, "utf8");
Papa.parse(csv, {
header: true,
step: function(row) {
console.log(row.data);
AddPathToTree(row.data[0]);
},
complete: function() {
console.log("All done!");
}
});
console.log(JSON.stringify(FileMap));
}
function AddPathToTree(record) {
//add the line to the map as
let pathElements = record.item.split("/");
CheckPathRecursively(pathElements, record, FileMap, 0);
}
function CheckPathRecursively(pathElements, fileDetails, CurrentMap, currentIndex) {
if (currentIndex >= pathElements.length) {
return;
}
let key = pathElements[currentIndex];
if (key in CurrentMap) {
CheckPathRecursively(pathElements, fileDetails, CurrentMap[key], currentIndex + 1);
} else {
CurrentMap[key] = {
org: fileDetails.org,
churn: fileDetails.churn
};
CheckPathRecursively(pathElements, fileDetails, CurrentMap[key], currentIndex + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment