Skip to content

Instantly share code, notes, and snippets.

@mendes5
Last active April 25, 2021 05:26
Show Gist options
  • Select an option

  • Save mendes5/8124ef0ded1b6572547119703cb71087 to your computer and use it in GitHub Desktop.

Select an option

Save mendes5/8124ef0ded1b6572547119703cb71087 to your computer and use it in GitHub Desktop.
Parses the data from NCDU dump file https://dev.yorhel.nl/ncdu/jsonfmt, the folder sizes are probably wrong tho....
var computeDirSize = (list) =>
list.reduce((prev, curr) => {
if (curr.type === "FOLDER") {
return computeDirSize(curr.children) + prev;
} else {
return curr.value + prev;
}
}, 0) || 0;
var readFile = file => ({
type: "FILE",
name: file.name,
value: file.asize || 1,
});
var readFolder = ([folderInfo, ...folderItems]) => {
const children = folderItems.map(recursiveRead).sort((a, b) => a.value - b.value);
return {
type: "FOLDER",
name: folderInfo.name,
value: computeDirSize(children) || 1,
children,
};
};
var recursiveRead = (items) => {
if (Array.isArray(items)) {
return readFolder(items);
} else {
return readFile(items);
}
};
l=recursiveRead(z)
fs.writeFileSync('./parsed.json', JSON.stringify(l))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment