improve short format for balance credits >1K and >1M #184

Merged
akinwale merged 4 commits from format-big-credits into master 2019-08-16 16:54:39 +02:00
2 changed files with 34 additions and 10 deletions

21
dist/bundle.es.js vendored
View file

@ -1724,7 +1724,7 @@ const selectFetchingClaimSearch = reselect.createSelector(selectFetchingClaimSea
const selectClaimSearchByQuery = reselect.createSelector(selectState$2, state => state.claimSearchByQuery || {});
const selectClaimSearchByQueryLastPageReached = reselect.createSelector(selectState$1, state => state.claimSearchByQueryLastPageReached || {});
const selectClaimSearchByQueryLastPageReached = reselect.createSelector(selectState$2, state => state.claimSearchByQueryLastPageReached || {});
const makeSelectShortUrlForUri = uri => reselect.createSelector(makeSelectClaimForUri(uri), claim => claim && claim.short_url);
@ -1744,9 +1744,22 @@ const makeSelectSupportsForUri = uri => reselect.createSelector(selectSupportsBy
return total;
});
function formatCredits(amount, precision) {
if (Number.isNaN(parseFloat(amount))) return '0';
return parseFloat(amount).toFixed(precision || 1).replace(/\.?0+$/, '');
function formatCredits(amount, precision, shortFormat = false) {
let actualAmount = parseFloat(amount),
suffix = '';
if (Number.isNaN(actualAmount)) return '0';
if (shortFormat) {
if (actualAmount >= 1000000) {
actualAmount = actualAmount / 1000000;
suffix = 'M';
} else if (actualAmount >= 1000) {
actualAmount = actualAmount / 1000;
suffix = 'K';
}
}
return actualAmount.toFixed(precision || 1).replace(/\.?0+$/, '') + suffix;
}
function formatFullPrice(amount, precision = 1) {

View file

@ -1,8 +1,19 @@
export function formatCredits(amount, precision) {
if (Number.isNaN(parseFloat(amount))) return '0';
return parseFloat(amount)
.toFixed(precision || 1)
.replace(/\.?0+$/, '');
export function formatCredits(amount, precision, shortFormat = false) {
let actualAmount = parseFloat(amount),
suffix = '';
if (Number.isNaN(actualAmount)) return '0';
kauffj commented 2019-08-15 18:00:22 +02:00 (Migrated from github.com)
Review

Would it make more sense for this functionality to be a parameter to formatCredits rather than a standalone function?

Would it make more sense for this functionality to be a parameter to formatCredits rather than a standalone function?
akinwale commented 2019-08-15 20:27:41 +02:00 (Migrated from github.com)
Review

I was trying to avoid having to update all the references to the method, as there are quite a few.

I was trying to avoid having to update all the references to the method, as there are quite a few.
kauffj commented 2019-08-15 21:55:02 +02:00 (Migrated from github.com)
Review

Why would you have to update the references if you add an optional 3rd parameter?

Why would you have to update the references if you add an optional 3rd parameter?
akinwale commented 2019-08-16 09:33:24 +02:00 (Migrated from github.com)
Review

I suppose I wouldn't have to, now that I think about it. I should be able to roll the functionality into the formatCredits method.

I suppose I wouldn't have to, now that I think about it. I should be able to roll the functionality into the `formatCredits` method.
if (shortFormat) {
if (actualAmount >= 1000000) {
actualAmount = actualAmount / 1000000;
suffix = 'M';
} else if (actualAmount >= 1000) {
actualAmount = actualAmount / 1000;
suffix = 'K';
}
}
return actualAmount.toFixed(precision || 1).replace(/\.?0+$/, '') + suffix;
}
export function formatFullPrice(amount, precision = 1) {
@ -13,7 +24,7 @@ export function formatFullPrice(amount, precision = 1) {
if (fraction) {
const decimals = fraction.split('');
const first = decimals.filter((number) => number !== '0')[0];
const first = decimals.filter(number => number !== '0')[0];
const index = decimals.indexOf(first);
// Set format fraction