Skip to content

Commit

Permalink
fix(macro): add number format helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Feb 28, 2018
1 parent 3ebdd53 commit 541fed7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sources/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}

// ----------------------------------------------------------------------------
// Convert byte size into a well formatted string
// ----------------------------------------------------------------------------

export function formatBytesToProperUnit(size, precision = 2, chunkSize = 1000) {
const units = ['TB', 'GB', 'MB', 'KB'];
let value = Number(size);
let currentUnit = 'B';
while (value > chunkSize) {
value /= chunkSize;
currentUnit = units.pop();
}
return `${value.toFixed(precision)} ${currentUnit}`;
}
// ----------------------------------------------------------------------------
// Convert thousand number with proper seperator
// ----------------------------------------------------------------------------

export function formatNumbersWithThousandSeparator(n, separator = ' ') {
const sections = [];
let size = n;
while (size > 1000) {
sections.push(`000${size % 1000}`.slice(-3));
size = Math.floor(size / 1000);
}
if (size > 0) {
sections.push(size);
}
sections.reverse();
return sections.join(separator);
}

// ----------------------------------------------------------------------------
// Array helper
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -1313,4 +1345,6 @@ export default {
proxy,
proxyPropertyMapping,
proxyPropertyState,
formatBytesToProperUnit,
formatNumbersWithThousandSeparator,
};

0 comments on commit 541fed7

Please sign in to comment.