duplicate files
by Steven Senkus
JavaScript
const filePaths = [
"root/a 1.txt(abcd) 2.txt(efgh)",
"root/c 3.txt(abcd)",
"root/c/d 4.txt(efgh)",
"root 4.txt(efgh)"
];
const output = [
["root/a/2.txt","root/c/d/4.txt","root/4.txt"],
["root/a/1.txt","root/c/3.txt"]
];
class File {
constructor(path, fileName, content) {
this.content = content;
this.fullFilePath = `${path}/${fileName}`;
}
}
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
var findDuplicate = function(paths) {
console.log('PATHS', paths);
let files = paths.map(path => {
const data = path.split(' ');
return data.slice(1).map((file) => {
const d = file.split('(');
const path = d[0];
const content = d[1].replace(')', '')
return new File(path, fileName, content);
});
});
files = [].concat.apply([], files);
console.log(groupBy(files, 'content'));
console.log('result', Object.values(groupBy(files, 'content')));
// return result;
};
findDuplicate(filePaths);