Skip to content

Commit

Permalink
Rewrite LoginUtils to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
kowczarz committed Sep 12, 2023
1 parent 5f668c6 commit eb144ac
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions src/libs/LoginUtils.js → src/libs/LoginUtils.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,59 @@
import _ from 'underscore';
import Str from 'expensify-common/lib/str';
import Onyx from 'react-native-onyx';
import {PUBLIC_DOMAINS} from 'expensify-common/lib/CONST';
import {parsePhoneNumber} from 'awesome-phonenumber';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';

let countryCodeByIP;
let countryCodeByIP: number;
Onyx.connect({
key: ONYXKEYS.COUNTRY_CODE,
callback: (val) => (countryCodeByIP = val || 1),
callback: (val) => (countryCodeByIP = val ?? 1),
});

/**
* Remove the special chars from the phone number
*
* @param {String} phone
* @return {String}
*/
function getPhoneNumberWithoutSpecialChars(phone) {
function getPhoneNumberWithoutSpecialChars(phone: string): string {
return phone.replace(CONST.REGEX.SPECIAL_CHARS_WITHOUT_NEWLINE, '');
}

/**
* Append user country code to the phone number
*
* @param {String} phone
* @return {String}
*/
function appendCountryCode(phone) {
function appendCountryCode(phone: string): string {
return phone.startsWith('+') ? phone : `+${countryCodeByIP}${phone}`;
}

/**
* Check email is public domain or not
*
* @param {String} email
* @return {Boolean}
*/
function isEmailPublicDomain(email) {
const emailDomain = Str.extractEmailDomain(email);
return _.includes(PUBLIC_DOMAINS, emailDomain.toLowerCase(), false);
function isEmailPublicDomain(email: string): boolean {
const emailDomain = Str.extractEmailDomain(email).toLowerCase();
// That's the place where it's being checked if a general string is included in the union
return (PUBLIC_DOMAINS as readonly string[]).includes(emailDomain);
}

/**
* Check if number is valid
* @param {String} values
* @returns {String} - Returns valid phone number formatted
* @returns a valid phone number formatted
*/
function validateNumber(values) {
function validateNumber(values: string): string {
const parsedPhoneNumber = parsePhoneNumber(values);

if (parsedPhoneNumber.possible && Str.isValidPhone(values.slice(0))) {
return parsedPhoneNumber.number.e164 + CONST.SMS.DOMAIN;
return parsedPhoneNumber.number?.e164 + CONST.SMS.DOMAIN;
}

return '';
}

/**
* Check number is valid and attach country code
* @param {String} partnerUserID
* @returns {String} - Returns valid phone number with country code
* @returns a valid phone number with country code
*/
function getPhoneLogin(partnerUserID) {
if (_.isEmpty(partnerUserID)) {
function getPhoneLogin(partnerUserID: string): string {
if (partnerUserID.length === 0) {
return '';
}

Expand Down

0 comments on commit eb144ac

Please sign in to comment.