UNPKG

finesse-toolkit

Version:

useful tools for finesse phone system

78 lines (60 loc) 2.13 kB
import { PhoneNumberUtil, PhoneNumberFormat } from 'google-libphonenumber'; const UK_LOCALE = 'en-GB'; const UK_PREFIX = '901144'; export type CoveredLocales = 'en-GB' | 'en-US'; export default class FinesseUtil { /* Because of the Revolutionary War, the United States is no longer part of Great Britain (a.k.a. The Red Coats). Because of this, when we try to call King George III to tell him about his tea, we need to tell our phone system that we're attempting to call another country. Therefore, we must sanitize the numbers. Their numbers begin with a '0' which totally makes sense. */ static sanitizePhoneNumber (number: string, locale: CoveredLocales): string { switch (locale) { case UK_LOCALE: number = `${UK_PREFIX}${number.slice(1)}`; break; } return number; } /* Phone Number formatting */ static unformatPhone (phoneNumber: string): string { // remove 011, +011, +1, 1 from the beginning of the number return phoneNumber.replace(/^(\+*011+|\+1+|1)/, ''); } static _formatPhone (phoneNumber: string): string { try { if (!phoneNumber) { return ''; } if (phoneNumber.length < 10) { return phoneNumber; } let instance = PhoneNumberUtil.getInstance(); // `011` maps to `+`, but should be removed, because we use the `+` for local locales too let locale: string = !!phoneNumber.match(/(^\+*011)/) ? 'UK' : 'US'; phoneNumber = phoneNumber.replace(/(^\+*011)/, ''); let number = instance.parse(`+${phoneNumber}`, locale); return instance.format(number, PhoneNumberFormat.NATIONAL); } catch (error) { console.error('ERROR formatting Phone Number: ', phoneNumber, '\nerror: ', error); return phoneNumber; } } static _sanitizeState (state: string): string { switch (state) { case 'WORK': case 'WORK_READY': state = 'WRAP_UP'; break; case null: state = 'DROPPED'; break; } return state; } static _promiseErrorHandler (error: Error): void { console.error(error); throw error; } };