Skip to content

Instantly share code, notes, and snippets.

@gianlucacandiotti
Last active June 22, 2016 21:11
Show Gist options
  • Save gianlucacandiotti/ea14d82ed0b1d8b635f2441dc2af7d65 to your computer and use it in GitHub Desktop.
Save gianlucacandiotti/ea14d82ed0b1d8b635f2441dc2af7d65 to your computer and use it in GitHub Desktop.
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