All files / src/features/MerchantPortal/Customer/Modals utils.ts

73.68% Statements 14/19
26.66% Branches 4/15
33.33% Functions 1/3
73.33% Lines 11/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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52            22x   22x 22x   22x   22x 22x 22x 22x   22x     54x                                                     54x      
import { Customer } from "types/customer.types";
import moment from "moment";
import { isoStringToLocalDate } from "@utils/date.helpers";
 
export function formatCustomerPhoneNumber(phone: string) {
  // Remove duplicate plus signs at the beginning
  phone = phone.replace(/^\+{2}/, "+");
 
  const match = phone.match(/^(\+\d)(\d{3})?(\d{3})?(\d{0,4})$/);
  Iif (!match) return phone;
 
  const [, countryCode, areaCode = "", prefix = "", lineNumber = ""] = match;
 
  let formatted = `${countryCode}`;
  Eif (areaCode) formatted += ` (${areaCode})`;
  Eif (prefix) formatted += ` ${prefix}`;
  Eif (lineNumber) formatted += `-${lineNumber}`;
 
  return formatted.trim();
}
 
export const mapToFormData = (data: Customer) => {
  const usedDateOfBirth = data?.dateOfBirth
    ? typeof data.dateOfBirth === "number"
      ? moment.unix(data.dateOfBirth).toDate()
      : isoStringToLocalDate(data.dateOfBirth as string)
    : null;
 
  const mapped = {
    avatarURL: data?.avatarURL,
    firstName: data?.firstName,
    lastName: data?.lastName,
    email: data?.email,
    dateOfBirth: usedDateOfBirth,
    phone: data?.phoneNumber,
    occupation: data?.occupation,
    employer: data?.employer,
    address: {
      line1: data?.address?.line1,
      city: data?.address?.city,
      state: data?.address?.state,
      zip: data?.address?.zip,
    },
    notes: data?.notes,
  };
  return mapped;
};
 
export const cleanPhoneNumber = (value: string) => {
  return value.replace("+", "").replaceAll(" ", "");
};