From 3ade495b60a209c818441e81162959f7d242e259 Mon Sep 17 00:00:00 2001 From: VitalyDevico Date: Tue, 16 Jul 2024 12:13:28 +0300 Subject: [PATCH 1/4] X2-10202 | added currency formatter --- src/helpers/currency.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/helpers/currency.js b/src/helpers/currency.js index 3f01210cc..a027a1974 100644 --- a/src/helpers/currency.js +++ b/src/helpers/currency.js @@ -1,5 +1,5 @@ import getUserLocale from "get-user-locale"; - +import { almostZero, numberFormat } from "../helpers/numbers"; const userLocale = getUserLocale(); const zeroDecimalCurrencies = new Set(["JPY", "CLP", "KRW", "LAK", "PYG", "VND", "VUV"]); @@ -18,3 +18,13 @@ export const getSymbol = (currency, locale = userLocale, amount = 0, isNarrowSym return string.replace(/\d/g, "").trim(); }; + +export const getValueWithCurrency = (amount, currency) => { + const userLocale = getUserLocale(); + + const amountVariable = almostZero(amount) ? 0 : amount; + const maxDigits = isZeroDecimal(currency) ? 0 : 2; + const formattedAmount = numberFormat(amountVariable, currency, userLocale, maxDigits, false).replace(".00", ""); + + return `${formattedAmount}`; +}; From 8e1619146e55d95ef184849c94b12f169e9a801b Mon Sep 17 00:00:00 2001 From: VitalyDevico Date: Wed, 17 Jul 2024 14:45:24 +0300 Subject: [PATCH 2/4] lint --- src/helpers/currency.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/helpers/currency.js b/src/helpers/currency.js index a027a1974..3a375b7a4 100644 --- a/src/helpers/currency.js +++ b/src/helpers/currency.js @@ -1,5 +1,6 @@ import getUserLocale from "get-user-locale"; import { almostZero, numberFormat } from "../helpers/numbers"; + const userLocale = getUserLocale(); const zeroDecimalCurrencies = new Set(["JPY", "CLP", "KRW", "LAK", "PYG", "VND", "VUV"]); From 26703f4502dae882770d3150b8287522794dfe6e Mon Sep 17 00:00:00 2001 From: VitalyDevico Date: Wed, 17 Jul 2024 14:47:30 +0300 Subject: [PATCH 3/4] add export --- index.d.ts | 1 + src/index.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 1ee09e5b4..0dd363805 100644 --- a/index.d.ts +++ b/index.d.ts @@ -136,6 +136,7 @@ export { getChildrenByType, getInitials, getSymbol, + getValueWithCurrency, GiftIcon, GlobeIcon, HandIcon, diff --git a/src/index.js b/src/index.js index ba2df7c9d..e52b24515 100644 --- a/src/index.js +++ b/src/index.js @@ -51,7 +51,7 @@ export { Number } from "./components/Utilities/Number"; // Helpers export { almostZero, numberFormat, roundNumber } from "./helpers/numbers"; -export { getSymbol, isZeroDecimal } from "./helpers/currency"; +export { getSymbol, getValueWithCurrency, isZeroDecimal } from "./helpers/currency"; export { formatDate, formatTime, dateFromObjectId } from "./helpers/date"; export { formatPhoneNumber } from "./helpers/phone"; export { isOSX, isIosBrowser } from "./helpers/browser"; From 5a9599ff07dfb6cacd3fc9b3cd8431cb2bb9966d Mon Sep 17 00:00:00 2001 From: VitalyDevico Date: Tue, 27 Aug 2024 12:34:27 +0300 Subject: [PATCH 4/4] added optional locale field --- src/helpers/currency.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/currency.js b/src/helpers/currency.js index 3a375b7a4..771414cde 100644 --- a/src/helpers/currency.js +++ b/src/helpers/currency.js @@ -20,8 +20,8 @@ export const getSymbol = (currency, locale = userLocale, amount = 0, isNarrowSym return string.replace(/\d/g, "").trim(); }; -export const getValueWithCurrency = (amount, currency) => { - const userLocale = getUserLocale(); +export const getValueWithCurrency = (amount, currency, locale) => { + const userLocale = locale ?? getUserLocale(); const amountVariable = almostZero(amount) ? 0 : amount; const maxDigits = isZeroDecimal(currency) ? 0 : 2;