Skip to content

Instantly share code, notes, and snippets.

@oslego
Created December 31, 2016 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oslego/4b82905cd9b3d4a59b9dd4139050e6cb to your computer and use it in GitHub Desktop.
Save oslego/4b82905cd9b3d4a59b9dd4139050e6cb to your computer and use it in GitHub Desktop.
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