"Given a list of file paths, print them out in a hierarchal way" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "Given a list of file paths, print them out in a hierarchal way" answered properly. Developers are finding an appropriate answer about Given a list of file paths, print them out in a hierarchal way related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like Given a list of file paths, print them out in a hierarchal way. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on Given a list of file paths, print them out in a hierarchal way.
Given a list of file paths, print them out in a hierarchal way
var data = 'three-dxf-master/' +
'\nfoobar/fumm' +
'\nthree-dxf-master/.DS_Store' +
'\nthree-dxf-master/.gitignore' +
'\nthree-dxf-master/LICENSE' +
'\nthree-dxf-master/README.md' +
'\nthree-dxf-master/bower.json' +
'\nthree-dxf-master/bower_components/' +
'\nthree-dxf-master/bower_components/.DS_Store' +
'\nthree-dxf-master/bower_components/dxf-parser/';
function parseData(data) {
var records = data.split(/\n/);
var result = records.reduce(function(acc, record) {
var fields = record.match(/[^\/]+\/?/g) || [];
var currentDir = acc;
fields.forEach(function (field, idx) {
// If field is a directory...
if (/\/$/.test(field)) {
// If first one and not an existing directory, add it
if (idx == 0) {
if (!(field in currentDir)) {
currentDir[field] = [];
}
// Move into subdirectory
currentDir = currentDir[field];
// If not first, see if it's a subdirectory of currentDir
} else {
// Look for field as a subdirectory of currentDir
var subDir = currentDir.filter(function(element){
return typeof element == 'object' && element[field];
})[0];
// If didn't find subDir, add it and set as currentDir
if (!subDir) {
var t = Object.create(null);
t[field] = [];
currentDir.push(t);
currentDir = t[field];
// If found, set as currentDir
} else {
currentDir = subDir[field];
}
}
// Otherwise it's a file. Make sure currentDir is a directory and not the root
} else {
if (Array.isArray(currentDir)) {
currentDir.push(field);
// Otherwise, must be at root where files aren't allowed
} else {
throw new Error('Files not allowed in root: ' + field);
}
}
});
return acc;
}, Object.create(null));
return result;
}
//console.log(JSON.stringify(parseData(data)));
console.log(parseData(data));
Source: stackoverflow.com
All those coders who are working on the Whatever based application and are stuck on Given a list of file paths, print them out in a hierarchal way can get a collection of related answers to their query. Programmers need to enter their query on Given a list of file paths, print them out in a hierarchal way related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Given a list of file paths, print them out in a hierarchal way for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of Given a list of file paths, print them out in a hierarchal way while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "Given a list of file paths, print them out in a hierarchal way". Visit this developer's friendly online web community, CodeProZone, and get your queries like Given a list of file paths, print them out in a hierarchal way resolved professionally and stay updated to the latest Whatever updates.