From 0eba8a5c228014fbe85b01182a744e766a51fb97 Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Wed, 20 Sep 2023 15:42:28 +0200 Subject: [PATCH 1/3] [TS migration] Migrate 'CurrencyUtils.js' lib --- .../{CurrencyUtils.js => CurrencyUtils.ts} | 60 +++++++------------ 1 file changed, 22 insertions(+), 38 deletions(-) rename src/libs/{CurrencyUtils.js => CurrencyUtils.ts} (68%) diff --git a/src/libs/CurrencyUtils.js b/src/libs/CurrencyUtils.ts similarity index 68% rename from src/libs/CurrencyUtils.js rename to src/libs/CurrencyUtils.ts index 6cbb0db7661b..bc0c8cb77aca 100644 --- a/src/libs/CurrencyUtils.js +++ b/src/libs/CurrencyUtils.ts @@ -1,16 +1,18 @@ -import _ from 'underscore'; -import lodashGet from 'lodash/get'; import Onyx from 'react-native-onyx'; import ONYXKEYS from '../ONYXKEYS'; import CONST from '../CONST'; import BaseLocaleListener from './Localize/LocaleListener/BaseLocaleListener'; import * as NumberFormatUtils from './NumberFormatUtils'; +import {Currency} from '../types/onyx'; + +type CurrencyList = Record; + +let currencyList: CurrencyList = {}; -let currencyList = {}; Onyx.connect({ key: ONYXKEYS.CURRENCY_LIST, callback: (val) => { - if (_.isEmpty(val)) { + if (!val || Object.keys(val).length === 0) { return; } @@ -23,56 +25,45 @@ Onyx.connect({ * For currencies that have decimal places > 2, floor to 2 instead: * https://github.com/Expensify/App/issues/15878#issuecomment-1496291464 * - * @param {String} currency - IOU currency - * @returns {Number} + * @param currency - IOU currency */ -function getCurrencyDecimals(currency = CONST.CURRENCY.USD) { - const decimals = lodashGet(currencyList, [currency, 'decimals']); - return _.isUndefined(decimals) ? 2 : decimals; +function getCurrencyDecimals(currency: string = CONST.CURRENCY.USD): number { + const decimals = currencyList?.[currency]?.decimals; + return decimals ?? 2; } /** * Returns the currency's minor unit quantity * e.g. Cent in USD * - * @param {String} currency - IOU currency - * @returns {Number} + * @param currency - IOU currency */ -function getCurrencyUnit(currency = CONST.CURRENCY.USD) { +function getCurrencyUnit(currency: string = CONST.CURRENCY.USD): number { return 10 ** getCurrencyDecimals(currency); } /** * Get localized currency symbol for currency(ISO 4217) Code - * - * @param {String} currencyCode - * @returns {String} */ -function getLocalizedCurrencySymbol(currencyCode) { +function getLocalizedCurrencySymbol(currencyCode: string): string | undefined { const parts = NumberFormatUtils.formatToParts(BaseLocaleListener.getPreferredLocale(), 0, { style: 'currency', currency: currencyCode, }); - return _.find(parts, (part) => part.type === 'currency').value; + return parts.find((part) => part.type === 'currency')?.value; } /** * Get the currency symbol for a currency(ISO 4217) Code - * - * @param {String} currencyCode - * @returns {String|undefined} */ -function getCurrencySymbol(currencyCode) { - return lodashGet(currencyList, [currencyCode, 'symbol']); +function getCurrencySymbol(currencyCode: string): string | undefined { + return currencyList?.[currencyCode]?.symbol; } /** * Whether the currency symbol is left-to-right. - * - * @param {String} currencyCode - * @returns {Boolean} */ -function isCurrencySymbolLTR(currencyCode) { +function isCurrencySymbolLTR(currencyCode: string): boolean { const parts = NumberFormatUtils.formatToParts(BaseLocaleListener.getPreferredLocale(), 0, { style: 'currency', currency: currencyCode, @@ -88,11 +79,8 @@ function isCurrencySymbolLTR(currencyCode) { * when doing math operations. * * @note we do not currently support any currencies with more than two decimal places. Decimal past the second place will be rounded. Sorry Tunisia :( - * - * @param {Number} amountAsFloat - * @returns {Number} */ -function convertToBackendAmount(amountAsFloat) { +function convertToBackendAmount(amountAsFloat: number): number { return Math.round(amountAsFloat * 100); } @@ -100,11 +88,8 @@ function convertToBackendAmount(amountAsFloat) { * Takes an amount in "cents" as an integer and converts it to a floating point amount used in the frontend. * * @note we do not support any currencies with more than two decimal places. - * - * @param {Number} amountAsInt - * @returns {Number} */ -function convertToFrontendAmount(amountAsInt) { +function convertToFrontendAmount(amountAsInt: number): number { return Math.trunc(amountAsInt) / 100.0; } @@ -112,11 +97,10 @@ function convertToFrontendAmount(amountAsInt) { * Given an amount in the "cents", convert it to a string for display in the UI. * The backend always handle things in "cents" (subunit equal to 1/100) * - * @param {Number} amountInCents – should be an integer. Anything after a decimal place will be dropped. - * @param {String} currency - * @returns {String} + * @param amountInCents – should be an integer. Anything after a decimal place will be dropped. + * @param currency - IOU currency */ -function convertToDisplayString(amountInCents, currency = CONST.CURRENCY.USD) { +function convertToDisplayString(amountInCents: number, currency: string = CONST.CURRENCY.USD): string { const convertedAmount = convertToFrontendAmount(amountInCents); return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, { style: 'currency', From ee8c075337667b0ae419034053d381b66662d355 Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Wed, 20 Sep 2023 17:41:29 +0200 Subject: [PATCH 2/3] Remove extra type --- src/libs/CurrencyUtils.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libs/CurrencyUtils.ts b/src/libs/CurrencyUtils.ts index bc0c8cb77aca..0f6dc08b42b3 100644 --- a/src/libs/CurrencyUtils.ts +++ b/src/libs/CurrencyUtils.ts @@ -1,13 +1,10 @@ import Onyx from 'react-native-onyx'; -import ONYXKEYS from '../ONYXKEYS'; +import ONYXKEYS, {OnyxValues} from '../ONYXKEYS'; import CONST from '../CONST'; import BaseLocaleListener from './Localize/LocaleListener/BaseLocaleListener'; import * as NumberFormatUtils from './NumberFormatUtils'; -import {Currency} from '../types/onyx'; -type CurrencyList = Record; - -let currencyList: CurrencyList = {}; +let currencyList: OnyxValues[typeof ONYXKEYS.CURRENCY_LIST] = {}; Onyx.connect({ key: ONYXKEYS.CURRENCY_LIST, From 42d21e22fae4ce8ee957ad090ec9019837e01efa Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Mon, 25 Sep 2023 10:39:49 +0200 Subject: [PATCH 3/3] Resolve conflicts --- src/libs/CurrencyUtils.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libs/CurrencyUtils.ts b/src/libs/CurrencyUtils.ts index 1c7dd68dea72..21784d450a07 100644 --- a/src/libs/CurrencyUtils.ts +++ b/src/libs/CurrencyUtils.ts @@ -111,12 +111,9 @@ function convertToDisplayString(amountInCents: number, currency: string = CONST. /** * Checks if passed currency code is a valid currency based on currency list - * - * @param {String} currencyCode - * @returns {Boolean} */ -function isValidCurrencyCode(currencyCode) { - const currency = lodashGet(currencyList, currencyCode); +function isValidCurrencyCode(currencyCode: string): boolean { + const currency = currencyList?.[currencyCode]; return Boolean(currency); }