Last active
June 22, 2016 21:11
-
-
Save gianlucacandiotti/ea14d82ed0b1d8b635f2441dc2af7d65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { round10 } from 'round10'; | |
export function formatMaxDataUnit(quantity, currentUnit = 'Bytes') { | |
if (quantity < 1024) { | |
return `${round10(quantity, -2)} ${currentUnit}`; | |
} | |
const nextUnit = { | |
Bytes: 'KB', | |
KB: 'MB', | |
MB: 'GB', | |
}; | |
if (!(currentUnit in nextUnit)) { | |
return `${round10(quantity, -2)} ${currentUnit}`; | |
} | |
const nextQuantity = quantity / 1024; | |
return formatMaxDataUnit(nextQuantity, nextUnit[currentUnit]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment