@buttercup/googledrive-client
Version:
Basic client for Google Drive
31 lines (30 loc) • 1.3 kB
JavaScript
export function formulateTree(files) {
// 1st pass: Collect all IDs
const ids = files.map(file => file.id);
// 2nd pass: Collect root IDs
const rootIDs = [];
files.forEach(file => {
file.parents.forEach(parentID => {
if (ids.indexOf(parentID) === -1 && rootIDs.indexOf(parentID) === -1) {
// ID is not available in the results, so assume it's root
rootIDs.push(parentID);
}
});
});
// Level creation
const getLevel = (item) => ({
id: item ? item.id : null,
filename: item ? item.filename : null,
files: !item
? files.filter(file => file.type === "file" &&
(file.parents.length === 0 ||
file.parents.some(parentID => rootIDs.indexOf(parentID) >= 0)))
: files.filter(file => file.type === "file" && file.parents.indexOf(item.id) >= 0),
children: (!item
? files.filter(file => file.type === "directory" &&
(file.parents.length === 0 ||
file.parents.some(parentID => rootIDs.indexOf(parentID) >= 0)))
: files.filter(file => file.type === "directory" && file.parents.indexOf(item.id) >= 0)).map(child => getLevel(child))
});
return getLevel();
}