All files / src/shared/HFInputs/HFGiveTelephone phoneNumber.utils.tsx

71.42% Statements 15/21
60.86% Branches 14/23
66.66% Functions 2/3
80% Lines 12/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33      546x 2761x 2224x 2224x 2224x 2224x 1382x       546x 262x 100x   100x                         100x    
import { country_dial_codes } from "@utils/country_dial_codes";
import parsePhoneNumberFromString from "libphonenumber-js";
 
export const normalizePhoneInputValue = (value?: string) => {
  if (!value) return value;
  Iif (value.substring(0, 3) === "+00") return `+${value.substring(3)}`;
  Iif (value.substring(0, 2) === "00") return `+${value.substring(2)}`;
  Iif (value.startsWith("++")) return value.substring(1);
  if (!value.startsWith("+") && !value.startsWith("(+")) return `+${value}`;
  else return value;
};
//TODO: include all phone input related logic in this file to easily find them, like phone schemas, formatters, etc.
 
export const getDefaultCountryCode = (defaultPhone?: string) => {
  if (!defaultPhone) return "US";
  const phoneData = parsePhoneNumberFromString(defaultPhone);
  //if the default number provided is not a valid number look for the first match in dial codes list to populate the input field correctly
  Iif (
    !phoneData?.country &&
    !!phoneData?.countryCallingCode &&
    !!phoneData?.nationalNumber
  ) {
    const defaultCountry =
      phoneData.countryCallingCode === "1"
        ? "US"
        : country_dial_codes.find(
            (country) =>
              country.dial_code === `+${phoneData?.countryCallingCode}`,
          )?.code;
    return defaultCountry || "US";
  } else return "US";
};