Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Support of short representation of currency values #350 #477

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const defaults = {
pattern: '!#',
negativePattern: '-!#',
format,
fromCents: false
fromCents: false,
short: false
};

const round = v => Math.round(v);
Expand Down Expand Up @@ -54,7 +55,7 @@ function currency(value, opts) {

function parse(value, opts, useRounding = true) {
let v = 0
, { decimal, errorOnInvalid, precision: decimals, fromCents } = opts
, { decimal, errorOnInvalid, precision: decimals, fromCents, short } = opts
, precision = pow(decimals)
, isNumber = typeof value === 'number'
, isCurrency = value instanceof currency;
Expand Down Expand Up @@ -88,20 +89,42 @@ function parse(value, opts, useRounding = true) {
return useRounding ? round(v) : v;
}

function shortFormat(number) {
if (number >= 1e12) {
return (number / 1e12).toFixed(1).replace(/\.0$/, '') + 'T';
}
if (number >= 1e9) {
return (number / 1e9).toFixed(1).replace(/\.0$/, '') + 'B';
}
if (number >= 1e6) {
return (number / 1e6).toFixed(1).replace(/\.0$/, '') + 'M';
}
if (number >= 1e3) {
return (number / 1e3).toFixed(1).replace(/\.0$/, '') + 'K';
}
return number;
}

/**
* Formats a currency object
* @param currency
* @param {object} [opts]
*/
function format(currency, settings) {
let { pattern, negativePattern, symbol, separator, decimal, groups } = settings
let { pattern, negativePattern, symbol, separator, decimal, groups, short } = settings
, split = ('' + currency).replace(/^-/, '').split('.')
, dollars = split[0]
, cents = split[1];

return (currency.value >= 0 ? pattern : negativePattern)
if (short) {
return symbol + shortFormat(dollars);
}

const result = (currency.value >= 0 ? pattern : negativePattern)
.replace('!', symbol)
.replace('#', dollars.replace(groups, '$1' + separator) + (cents ? decimal + cents : ''));

return result;
}

currency.prototype = {
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,18 @@ test('should handle fractional cents', t => {
var values = currency(1234.56, { fromCents: true });
t.is(values.intValue, 1235);
t.is(values.value, 12.35);
});

test('should to short representation of currency values', t => {
var oneThousand = currency(1500, { short: true });
var tenThousand = currency(10500, { short: true });
var oneMillion = currency(1000000, { short: true });
var oneBillion = currency(1000000000, { short: true });
var oneTrillion = currency(1000000000000, { short: true });

t.is(oneThousand.format(), '$1.5K');
t.is(tenThousand.format(), '$10.5K');
t.is(oneMillion.format(), '$1M');
t.is(oneBillion.format(), '$1B');
t.is(oneTrillion.format(), '$1T');
});