Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA][TS migration] Migrate 'MoneyRequestUtils.js' lib to TypeScript #27276

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 14 additions & 36 deletions src/libs/MoneyRequestUtils.js → src/libs/MoneyRequestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
import lodashGet from 'lodash/get';
import _ from 'underscore';
import {ValueOf} from 'type-fest';
import CONST from '../CONST';

/**
* Strip comma from the amount
*
* @param {String} amount
* @returns {String}
*/
function stripCommaFromAmount(amount) {
function stripCommaFromAmount(amount: string): string {
return amount.replace(/,/g, '');
}

/**
* Strip spaces from the amount
*
* @param {String} amount
* @returns {String}
*/
function stripSpacesFromAmount(amount) {
function stripSpacesFromAmount(amount: string): string {
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}
* @param amount - Changed amount from user input
*/
function addLeadingZero(amount) {
function addLeadingZero(amount: string): string {
return amount === '.' ? '0.' : amount;
}

/**
* Calculate the length of the amount with leading zeroes
*
* @param {String} amount
* @returns {Number}
*/
function calculateAmountLength(amount) {
function calculateAmountLength(amount: string): number {
const leadingZeroes = amount.match(/^0+/);
const leadingZeroesLength = lodashGet(leadingZeroes, '[0].length', 0);
const absAmount = parseFloat((stripCommaFromAmount(amount) * 100).toFixed(2)).toString();
const leadingZeroesLength = leadingZeroes?.[0]?.length ?? 0;
const absAmount = parseFloat((Number(stripCommaFromAmount(amount)) * 100).toFixed(2)).toString();

if (/\D/.test(absAmount)) {
return CONST.IOU.AMOUNT_MAX_LENGTH + 1;
Expand All @@ -52,44 +41,33 @@ function calculateAmountLength(amount) {

/**
* Check if amount is a decimal up to 3 digits
*
* @param {String} amount
* @returns {Boolean}
*/
function validateAmount(amount) {
function validateAmount(amount: string): boolean {
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])
function replaceAllDigits(text: string, convertFn: (char: string) => string): string {
return text
.split('')
.map((char) => {
try {
return convertFn(char);
} catch {
return char;
}
})
.join('')
.value();
.join('');
}

/**
* Check if distance request or not
*
* @param {String} iouType - `send` | `split` | `request`
* @param {String} selectedTab - `manual` | `scan` | `distance`
* @returns {Boolean}
*/
function isDistanceRequest(iouType, selectedTab) {
function isDistanceRequest(iouType: ValueOf<typeof CONST.IOU.MONEY_REQUEST_TYPE>, selectedTab: ValueOf<typeof CONST.TAB>): boolean {
return iouType === CONST.IOU.MONEY_REQUEST_TYPE.REQUEST && selectedTab === CONST.TAB.DISTANCE;
}

Expand Down