From 7be26fcd668532437cf808e65d9b76698199812f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gabriel=20Quaresma=20de=20Almeida?= <joaoGabriel55> Date: Mon, 24 Jun 2024 13:44:59 -0300 Subject: [PATCH 1/2] feat: implement short currency format support --- src/currency.js | 28 ++++++++++++++++++++++++---- test/test.js | 12 ++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/currency.js b/src/currency.js index 1edc89fe..ca70e1e3 100644 --- a/src/currency.js +++ b/src/currency.js @@ -7,7 +7,8 @@ const defaults = { pattern: '!#', negativePattern: '-!#', format, - fromCents: false + fromCents: false, + short: false }; const round = v => Math.round(v); @@ -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; @@ -88,20 +89,39 @@ function parse(value, opts, useRounding = true) { return useRounding ? round(v) : v; } +function shortFormat(num) { + if (num >= 1e9) { + return (num / 1e9).toFixed(1).replace(/\.0$/, '') + 'B'; + } + if (num >= 1e6) { + return (num / 1e6).toFixed(1).replace(/\.0$/, '') + 'M'; + } + if (num >= 1e3) { + return (num / 1e3).toFixed(1).replace(/\.0$/, '') + 'K'; + } + return num; +} + /** * 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 = { diff --git a/test/test.js b/test/test.js index ce1d38ca..96895adc 100644 --- a/test/test.js +++ b/test/test.js @@ -555,4 +555,16 @@ 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(1000, { short: true }); + var tenThousand = currency(10000, { short: true }); + var oneMillion = currency(1000000, { short: true }); + var oneBillion = currency(1000000000, { short: true }); + + t.is(oneThousand.format(), '$1K'); + t.is(tenThousand.format(), '$10K'); + t.is(oneMillion.format(), '$1M'); + t.is(oneBillion.format(), '$1B'); }); \ No newline at end of file From a10843448c4e97e23a7ba5376a9ec3b04b06d329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gabriel=20Quaresma=20de=20Almeida?= <joaoGabriel55> Date: Mon, 24 Jun 2024 13:49:20 -0300 Subject: [PATCH 2/2] feat: add trillion number shorter support --- src/currency.js | 19 +++++++++++-------- test/test.js | 10 ++++++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/currency.js b/src/currency.js index ca70e1e3..fbc76b5a 100644 --- a/src/currency.js +++ b/src/currency.js @@ -89,17 +89,20 @@ function parse(value, opts, useRounding = true) { return useRounding ? round(v) : v; } -function shortFormat(num) { - if (num >= 1e9) { - return (num / 1e9).toFixed(1).replace(/\.0$/, '') + 'B'; +function shortFormat(number) { + if (number >= 1e12) { + return (number / 1e12).toFixed(1).replace(/\.0$/, '') + 'T'; } - if (num >= 1e6) { - return (num / 1e6).toFixed(1).replace(/\.0$/, '') + 'M'; + if (number >= 1e9) { + return (number / 1e9).toFixed(1).replace(/\.0$/, '') + 'B'; } - if (num >= 1e3) { - return (num / 1e3).toFixed(1).replace(/\.0$/, '') + 'K'; + if (number >= 1e6) { + return (number / 1e6).toFixed(1).replace(/\.0$/, '') + 'M'; } - return num; + if (number >= 1e3) { + return (number / 1e3).toFixed(1).replace(/\.0$/, '') + 'K'; + } + return number; } /** diff --git a/test/test.js b/test/test.js index 96895adc..fbffdf3f 100644 --- a/test/test.js +++ b/test/test.js @@ -558,13 +558,15 @@ test('should handle fractional cents', t => { }); test('should to short representation of currency values', t => { - var oneThousand = currency(1000, { short: true }); - var tenThousand = currency(10000, { short: true }); + 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(), '$1K'); - t.is(tenThousand.format(), '$10K'); + 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'); }); \ No newline at end of file