Skip to content

Commit

Permalink
fix: apply requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
koko57 committed Aug 4, 2023
1 parent 81d595f commit 014b326
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
29 changes: 16 additions & 13 deletions src/libs/MoneyRequestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,58 @@ 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
*
* @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
*
* @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
*
* @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
*
* @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
Expand All @@ -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);
Expand All @@ -78,5 +80,6 @@ const replaceAllDigits = (text, convertFn) =>
})
.join('')
.value();
}

export {stripCommaFromAmount, stripSpacesFromAmount, addLeadingZero, validateAmount, replaceAllDigits};
9 changes: 3 additions & 6 deletions src/pages/iou/steps/NewRequestAmountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`;
Expand Down

0 comments on commit 014b326

Please sign in to comment.