-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23979 from koko57/refactor/23149-money-request-page
Refactor/23149 money request page
- Loading branch information
Showing
6 changed files
with
563 additions
and
514 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import lodashGet from 'lodash/get'; | ||
import _ from 'underscore'; | ||
import CONST from '../CONST'; | ||
|
||
/** | ||
* Strip comma from the amount | ||
* | ||
* @param {String} amount | ||
* @returns {String} | ||
*/ | ||
function stripCommaFromAmount(amount) { | ||
return amount.replace(/,/g, ''); | ||
} | ||
|
||
/** | ||
* Strip spaces from the amount | ||
* | ||
* @param {String} amount | ||
* @returns {String} | ||
*/ | ||
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} | ||
*/ | ||
function addLeadingZero(amount) { | ||
return amount === '.' ? '0.' : amount; | ||
} | ||
|
||
/** | ||
* Calculate the length of the amount with leading zeroes | ||
* | ||
* @param {String} amount | ||
* @returns {Number} | ||
*/ | ||
function calculateAmountLength(amount) { | ||
const leadingZeroes = amount.match(/^0+/); | ||
const leadingZeroesLength = lodashGet(leadingZeroes, '[0].length', 0); | ||
const absAmount = parseFloat((stripCommaFromAmount(amount) * 100).toFixed(2)).toString(); | ||
|
||
if (/\D/.test(absAmount)) { | ||
return CONST.IOU.AMOUNT_MAX_LENGTH + 1; | ||
} | ||
|
||
return leadingZeroesLength + (absAmount === '0' ? 2 : absAmount.length); | ||
} | ||
|
||
/** | ||
* Check if amount is a decimal up to 3 digits | ||
* | ||
* @param {String} amount | ||
* @returns {Boolean} | ||
*/ | ||
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 | ||
* the original character will be preserved. | ||
* | ||
* @param {String} text | ||
* @param {Function} convertFn - `fromLocaleDigit` or `toLocaleDigit` | ||
* @returns {String} | ||
*/ | ||
function replaceAllDigits(text, convertFn) { | ||
return _.chain([...text]) | ||
.map((char) => { | ||
try { | ||
return convertFn(char); | ||
} catch { | ||
return char; | ||
} | ||
}) | ||
.join('') | ||
.value(); | ||
} | ||
|
||
export {stripCommaFromAmount, stripSpacesFromAmount, addLeadingZero, validateAmount, replaceAllDigits}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.