Rob Colbert
2 years ago
1 changed files with 61 additions and 0 deletions
@ -0,0 +1,61 @@ |
|||
// phone.js
|
|||
// Copyright (C) 2022 DTP Technologies, LLC
|
|||
// License: Apache-2.0
|
|||
|
|||
'use strict'; |
|||
|
|||
const { |
|||
libphonenumber, |
|||
striptags, |
|||
SiteService, |
|||
SiteError, |
|||
} = require('../../lib/site-lib'); |
|||
|
|||
class PhoneService extends SiteService { |
|||
|
|||
constructor (dtp) { |
|||
super(dtp, module.exports); |
|||
} |
|||
|
|||
async processPhoneNumberInput (phoneNumberInput, country = 'US') { |
|||
const { parsePhoneNumber } = libphonenumber; |
|||
const phoneCheck = await parsePhoneNumber(striptags(phoneNumberInput.trim()), country); |
|||
|
|||
if (!phoneCheck.isValid()) { |
|||
throw new SiteError(400, 'The phone number entered is not valid'); |
|||
} |
|||
|
|||
// store everything this library provides about the new phone number
|
|||
const phoneNumber = { |
|||
type: phoneCheck.getType(), |
|||
number: phoneCheck.number, |
|||
countryCallingCode: phoneCheck.countryCallingCode, |
|||
nationalNumber: phoneCheck.nationalNumber, |
|||
country: phoneCheck.country, |
|||
ext: phoneCheck.ext, |
|||
carrierCode: phoneCheck.carrierCode, |
|||
}; |
|||
|
|||
if (phoneCheck.carrierCode) { |
|||
phoneNumber.carrierCode = phoneCheck.carrierCode; |
|||
} |
|||
if (phoneCheck.ext) { |
|||
phoneNumber.ext = phoneCheck.ext; |
|||
} |
|||
|
|||
phoneNumber.display = { |
|||
national: phoneCheck.formatNational(), |
|||
international: phoneCheck.formatInternational(), |
|||
uri: phoneCheck.getURI(), |
|||
}; |
|||
|
|||
return phoneNumber; |
|||
} |
|||
|
|||
} |
|||
|
|||
module.exports = { |
|||
slug: 'phone', |
|||
name: 'phone', |
|||
create: (dtp) => { return new PhoneService(dtp); }, |
|||
}; |
Loading…
Reference in new issue