diff --git a/src/libs/MoneyRequestUtils.js b/src/libs/MoneyRequestUtils.js index 7ccfff2e7796..706c34ad912d 100644 --- a/src/libs/MoneyRequestUtils.js +++ b/src/libs/MoneyRequestUtils.js @@ -8,7 +8,9 @@ import CONST from '../CONST'; * @param {String} amount * @returns {String} */ -const stripCommaFromAmount = (amount) => amount.replace(/,/g, ''); +function stripCommaFromAmount(amount) { + return amount.replace(/,/g, ''); +} /** * Strip spaces from the amount @@ -16,7 +18,9 @@ const stripCommaFromAmount = (amount) => amount.replace(/,/g, ''); * @param {String} amount * @returns {String} */ -const stripSpacesFromAmount = (amount) => amount.replace(/\s+/g, ''); +function stripSpacesFromAmount(amount) { + return amount.replace(/\s+/g, ''); +} /** * Adds a leading zero to the amount if user entered just the decimal separator @@ -24,7 +28,9 @@ const stripSpacesFromAmount = (amount) => amount.replace(/\s+/g, ''); * @param {String} amount - Changed amount from user input * @returns {String} */ -const addLeadingZero = (amount) => (amount === '.' ? '0.' : amount); +function addLeadingZero(amount) { + return amount === '.' ? '0.' : amount; +} /** * Calculate the length of the amount with leading zeroes @@ -32,21 +38,17 @@ const addLeadingZero = (amount) => (amount === '.' ? '0.' : amount); * @param {String} amount * @returns {Number} */ -const calculateAmountLength = (amount) => { +function calculateAmountLength(amount) { const leadingZeroes = amount.match(/^0+/); const leadingZeroesLength = lodashGet(leadingZeroes, '[0].length', 0); const absAmount = parseFloat((stripCommaFromAmount(amount) * 100).toFixed(2)).toString(); - // The following logic will prevent users from pasting an amount that is excessively long in length, - // which would result in the 'absAmount' value being expressed in scientific notation or becoming infinity. if (/\D/.test(absAmount)) { return CONST.IOU.AMOUNT_MAX_LENGTH + 1; } - // Return the sum of leading zeroes length and absolute amount length (including fraction digits). - // When the absolute amount is 0, add 2 to the leading zeroes length to represent fraction digits. return leadingZeroesLength + (absAmount === '0' ? 2 : absAmount.length); -}; +} /** * Check if amount is a decimal up to 3 digits @@ -54,10 +56,10 @@ const calculateAmountLength = (amount) => { * @param {String} amount * @returns {Boolean} */ -const validateAmount = (amount) => { +function validateAmount(amount) { const decimalNumberRegex = new RegExp(/^\d+(,\d+)*(\.\d{0,2})?$/, 'i'); return amount === '' || (decimalNumberRegex.test(amount) && calculateAmountLength(amount) <= CONST.IOU.AMOUNT_MAX_LENGTH); -}; +} /** * Replaces each character by calling `convertFn`. If `convertFn` throws an error, then @@ -67,8 +69,8 @@ const validateAmount = (amount) => { * @param {Function} convertFn - `fromLocaleDigit` or `toLocaleDigit` * @returns {String} */ -const replaceAllDigits = (text, convertFn) => - _.chain([...text]) +function replaceAllDigits(text, convertFn) { + return _.chain([...text]) .map((char) => { try { return convertFn(char); @@ -78,5 +80,6 @@ const replaceAllDigits = (text, convertFn) => }) .join('') .value(); +} export {stripCommaFromAmount, stripSpacesFromAmount, addLeadingZero, validateAmount, replaceAllDigits}; diff --git a/src/pages/iou/steps/NewRequestAmountPage.js b/src/pages/iou/steps/NewRequestAmountPage.js index cafbd90dee67..90cc4c20b837 100644 --- a/src/pages/iou/steps/NewRequestAmountPage.js +++ b/src/pages/iou/steps/NewRequestAmountPage.js @@ -82,9 +82,6 @@ function NewRequestAmountPage({route, iou, report, errors}) { const currency = currentCurrency || iou.currency; - /** - * Focus text input - */ const focusTextInput = () => { // Component may not be initialized due to navigation transitions // Wait until interactions are complete before trying to focus @@ -113,14 +110,14 @@ function NewRequestAmountPage({route, iou, report, errors}) { }, [errors, report, reportID]); // Because we use Onyx to store iou info, when we try to make two different money requests from different tabs, it can result in many bugs. - // This logic is added to prevent such bugs. useEffect(() => { if (isEditing) { if (prevMoneyRequestID.current !== iou.id) { // The ID is cleared on completing a request. In that case, we will do nothing. - if (iou.id) { - Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType, reportID), true); + if (!iou.id) { + return; } + Navigation.goBack(ROUTES.getMoneyRequestRoute(iouType, reportID), true); return; } const moneyRequestID = `${iouType}${reportID}`;