All files parseIndicator.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 1/1
100% Lines 11/11

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 241x       1x     12x 12x   12x 11x 11x 11x     12x 1x       11x    
const defaultIndicator = '+32';
 
type IndicatorAndPhoneNumber = { indicator: string; rest: string };
 
export default function parseIndicator(
  phoneNumber: string
): IndicatorAndPhoneNumber {
  let indicator = defaultIndicator;
  let rest = phoneNumber;
 
  if (phoneNumber.startsWith('00') || phoneNumber.startsWith('+')) {
    const [indi, ...r] = phoneNumber.split(' ');
    indicator = indi!.replace('00', '+');
    rest = r.join(' ');
  }
 
  if (rest === '')
    throw new Error(
      'Phone number is invalid: regional indicator is probably mixed with the rest of the number'
    );
 
  return { indicator, rest };
}