Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get size on disk by highest order of magnitude using recursion.
// From https://ponyfoo.com/articles/var-let-const
function toLargestUnit (value, unit = `MB`) {
const units = [`MB`, `GB`, `TB`]
const i = units.indexOf(unit)
const nextUnit = units[i + 1]
if (value >= 1024 && nextUnit) {
return toLargestUnit(value / 1024, nextUnit)
}
return { value, unit }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.