From 80b069e9b6f3a448c01c83ac61fbd07be84f66eb Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 9 May 2018 08:54:56 -0700 Subject: [PATCH] Move phone number format/parse into ts/types/PhoneNumber --- js/modules/types/contact.js | 2 +- ts/types/Contact.ts | 2 +- .../PhoneNumber.ts} | 20 +++++++++++++++++-- ts/util/parsePhoneNumber.ts | 17 ---------------- 4 files changed, 20 insertions(+), 21 deletions(-) rename ts/{util/formatPhoneNumber.ts => types/PhoneNumber.ts} (53%) delete mode 100644 ts/util/parsePhoneNumber.ts diff --git a/js/modules/types/contact.js b/js/modules/types/contact.js index a9c8e235f..2f0724bc2 100644 --- a/js/modules/types/contact.js +++ b/js/modules/types/contact.js @@ -2,7 +2,7 @@ const { omit, compact, map } = require('lodash'); const { toLogFormat } = require('./errors'); const { SignalService } = require('../../../ts/protobuf'); -const { parsePhoneNumber } = require('../../../ts/util/parsePhoneNumber'); +const { parse: parsePhoneNumber } = require('../../../ts/types/PhoneNumber'); const DEFAULT_PHONE_TYPE = SignalService.DataMessage.Contact.Phone.Type.HOME; const DEFAULT_EMAIL_TYPE = SignalService.DataMessage.Contact.Email.Type.HOME; diff --git a/ts/types/Contact.ts b/ts/types/Contact.ts index ea7998a57..5767c7b44 100644 --- a/ts/types/Contact.ts +++ b/ts/types/Contact.ts @@ -1,6 +1,6 @@ // @ts-ignore import Attachments from '../../app/attachments'; -import { formatPhoneNumber } from '../util/formatPhoneNumber'; +import { format as formatPhoneNumber } from '../types/PhoneNumber'; export interface Contact { name: Name; diff --git a/ts/util/formatPhoneNumber.ts b/ts/types/PhoneNumber.ts similarity index 53% rename from ts/util/formatPhoneNumber.ts rename to ts/types/PhoneNumber.ts index 21c8e153f..5ca2bb86c 100644 --- a/ts/util/formatPhoneNumber.ts +++ b/ts/types/PhoneNumber.ts @@ -1,6 +1,6 @@ -import { instance, PhoneNumberFormat } from './libphonenumberInstance'; +import { instance, PhoneNumberFormat } from '../util/libphonenumberInstance'; -export function formatPhoneNumber( +export function format( phoneNumber: string, options: { ourRegionCode: string; @@ -20,3 +20,19 @@ export function formatPhoneNumber( return phoneNumber; } } + +export function parse( + phoneNumber: string, + options: { + regionCode: string; + } +): string { + const { regionCode } = options; + const parsedNumber = instance.parse(phoneNumber, regionCode); + + if (instance.isValidNumber(parsedNumber)) { + return instance.format(parsedNumber, PhoneNumberFormat.E164); + } + + return phoneNumber; +} diff --git a/ts/util/parsePhoneNumber.ts b/ts/util/parsePhoneNumber.ts deleted file mode 100644 index ef80d28b2..000000000 --- a/ts/util/parsePhoneNumber.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { instance, PhoneNumberFormat } from './libphonenumberInstance'; - -export function parsePhoneNumber( - phoneNumber: string, - options: { - regionCode: string; - } -): string { - const { regionCode } = options; - const parsedNumber = instance.parse(phoneNumber, regionCode); - - if (instance.isValidNumber(parsedNumber)) { - return instance.format(parsedNumber, PhoneNumberFormat.E164); - } - - return phoneNumber; -}