You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.5 KiB
61 lines
1.5 KiB
// 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); },
|
|
};
|