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
3 changed files with 29 additions and 39 deletions
Showing only changes of commit f6ba006ccc - Show all commits

30
dist/bundle.es.js vendored
View file

@ -1711,24 +1711,23 @@ const selectCurrentHeight = reselect.createSelector(selectState$2, state => stat
const selectTransactionListFilter = reselect.createSelector(selectState$2, state => state.transactionListFilter || '');
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';
function formatBigNumberCredits(amount, precision) {
const actualAmount = parseFloat(amount);
if (Number.isNaN(actualAmount) || actualAmount < 1000) {
return formatCredits(amount, precision);
if (shortFormat) {
if (actualAmount >= 1000000) {
actualAmount = actualAmount / 1000000;
suffix = 'M';
}
if (actualAmount >= 1000) {
actualAmount = actualAmount / 1000;
suffix = 'K';
}
}
if (actualAmount > 1000000) {
return formatCredits(actualAmount / 1000000, precision) + 'M';
}
if (actualAmount > 1000) {
return formatCredits(actualAmount / 1000, precision) + 'K';
}
return actualAmount.toFixed(precision || 1).replace(/\.?0+$/, '') + suffix;
}
function formatFullPrice(amount, precision = 1) {
@ -4850,7 +4849,6 @@ exports.doWalletStatus = doWalletStatus;
exports.doWalletUnlock = doWalletUnlock;
exports.fileInfoReducer = fileInfoReducer;
exports.fileReducer = fileReducer;
exports.formatBigNumberCredits = formatBigNumberCredits;
exports.formatCredits = formatCredits;
exports.formatFullPrice = formatFullPrice;
exports.isClaimNsfw = isClaimNsfw;

View file

@ -116,12 +116,7 @@ export { doToggleBlockChannel } from 'redux/actions/blocked';
// utils
export { batchActions } from 'util/batch-actions';
export { parseQueryParams, toQueryString } from 'util/query-params';
export {
formatCredits,
formatBigNumberCredits,
formatFullPrice,
creditsToString,
} from 'util/format-credits';
export { formatCredits, formatFullPrice, creditsToString } from 'util/format-credits';
export { isClaimNsfw, createNormalizedClaimSearchKey } from 'util/claim';
// reducers

View file

@ -1,23 +1,20 @@
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.
export function formatBigNumberCredits(amount, precision) {
const actualAmount = parseFloat(amount);
if (Number.isNaN(actualAmount) || actualAmount < 1000) {
return formatCredits(amount, precision);
if (shortFormat) {
if (actualAmount >= 1000000) {
actualAmount = actualAmount / 1000000;
suffix = 'M';
}
if (actualAmount >= 1000) {
actualAmount = actualAmount / 1000;
suffix = 'K';
}
}
if (actualAmount > 1000000) {
return formatCredits(actualAmount / 1000000, precision) + 'M';
}
if (actualAmount > 1000) {
return formatCredits(actualAmount / 1000, precision) + 'K';
}
return actualAmount.toFixed(precision || 1).replace(/\.?0+$/, '') + suffix;
}
export function formatFullPrice(amount, precision = 1) {