All files / src/shared utilities.ts

78.94% Statements 30/38
40% Branches 4/10
100% Functions 8/8
78.37% Lines 29/37

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 687x 7x   24x 24x       24x     28x   28x 28x 28x         24x       24x 24x 24x 22x                 2x       24x 70x   24x 67x       8x 8x 8x 7x 7x   1x       2x 2x 2x   1x          
import parsePhoneNumber from 'libphonenumber-js';
import { useI18nContext } from '@procore/core-react';
 
export function arrayTextTransform(array: string[]): string {
  Iif (!array.length) {
    return '';
  }
 
  return createTranslatedTypeArray(array).join(', ');
}
 
export function getStylizedPhoneNumber(phoneNumber: string) {
  // TODO: This is hardcoded to be US for now, but if PCN ever supports international customers, we'll need to update this
  const parsedPhoneNumber = parsePhoneNumber(phoneNumber, 'US');
  if (parsedPhoneNumber) {
    return parsedPhoneNumber.formatNational();
  }
  return phoneNumber;
}
 
export function formatPhoneNumberInput(
  phoneNumber: string,
  countryCode: string | undefined
) {
  if (countryCode === 'US' || countryCode === 'CA') {
    const parsedPhoneNumber = parsePhoneNumber(phoneNumber, countryCode as any);
    if (parsedPhoneNumber) {
      return parsedPhoneNumber.formatNational();
    }
  } else IEif (countryCode) {
    const parsedPhoneNumber = parsePhoneNumber(phoneNumber, countryCode as any);
    Iif (parsedPhoneNumber) {
      return parsedPhoneNumber.formatInternational();
    }
  }
 
  return phoneNumber;
}
 
export function createTranslatedTypeArray(array: string[]) {
  const i18n = useI18nContext();
  const trimmedArray = array.filter((item: string) => item.trim() !== '');
 
  return trimmedArray.map((type: string) => {
    return i18n.t('views.generic.directory.add_company_package.' + type);
  });
}
 
export function isLocalStorageAvailable(): boolean {
  try {
    localStorage.setItem('test', 'test');
    localStorage.removeItem('test');
    return true;
  } catch {
    return false;
  }
}
 
export function saveToLocalStorage(key: string, value: string) {
  try {
    localStorage.setItem(key, value);
  } catch (e: any) {
    Iif (e.name === 'QuotaExceededError') {
      console.error('Error trying to setItem', e);
    }
  }
}