angular-phone-utils-lib
Version:
Angular library for phone number formatting and validation based on libphonenumber-js
151 lines (143 loc) • 4.29 kB
JavaScript
import { ɵɵdefineInjectable, Injectable, Pipe, NgModule } from '@angular/core';
import { parsePhoneNumberFromString } from 'libphonenumber-js/max';
class PhoneUtilsService {
/**
* Transform a given phone number to international format
*
* @param value Phone numer
* @param country Two-letters country code
*/
getInternational(value, country) {
const phoneNumber = parsePhoneNumberFromString(value, country);
if (phoneNumber) {
return phoneNumber.formatInternational();
}
return value;
}
/**
* Transform a given phone number to its national format
*
* @param value Phone numer
* @param country Two-letters country code
*/
getNational(value, country) {
const phoneNumber = parsePhoneNumberFromString(value, country);
if (phoneNumber) {
return phoneNumber.formatNational();
}
return value;
}
/**
* International format without inner spaces (PNF.E164)
*
* Instead of "+359 88 123" it will return "+35988123"
*
* @param value
* @param country
*/
getPlain(value, country) {
const phoneNumber = parsePhoneNumberFromString(value, country);
if (phoneNumber) {
return phoneNumber.number.toString();
}
return value ? value.replace(/ /g, "") : value;
}
/**
* Is valid phone number?
*
* The method will return true when arguments relates to a
* phone number from the selected country.
*
* @param value
* @param country
*/
isValid(value, country) {
const phoneNumber = parsePhoneNumberFromString(value, country);
if (!phoneNumber) {
return false;
}
return phoneNumber.isValid();
}
/**
* Reactive form validator
*
* Will set *phoneInvalid* property in related form control error's object.
*
* @param country
*/
isValidFormControl(country) {
return (control) => {
if (!control.value) {
return null;
}
try {
return !this.isValid(control.value, country)
? { phoneInvalid: true }
: null;
}
catch (e) {
return { phoneInvalid: true };
}
};
}
}
PhoneUtilsService.ɵprov = ɵɵdefineInjectable({ factory: function PhoneUtilsService_Factory() { return new PhoneUtilsService(); }, token: PhoneUtilsService, providedIn: "root" });
PhoneUtilsService.decorators = [
{ type: Injectable, args: [{
providedIn: "root",
},] }
];
class InternationalFormatPipe {
constructor(phoneUtilsService) {
this.phoneUtilsService = phoneUtilsService;
}
transform(value, ...args) {
return this.phoneUtilsService.getInternational(value, args[0]);
}
}
InternationalFormatPipe.ctorParameters = () => [
{ type: PhoneUtilsService }
];
InternationalFormatPipe.decorators = [
{ type: Pipe, args: [{
name: 'internationalFormat'
},] }
];
InternationalFormatPipe.ctorParameters = () => [
{ type: PhoneUtilsService }
];
class NationalFormatPipe {
constructor(phoneUtilsService) {
this.phoneUtilsService = phoneUtilsService;
}
transform(value, ...args) {
return this.phoneUtilsService.getNational(value, args[0]);
}
}
NationalFormatPipe.ctorParameters = () => [
{ type: PhoneUtilsService }
];
NationalFormatPipe.decorators = [
{ type: Pipe, args: [{
name: 'nationalFormat'
},] }
];
NationalFormatPipe.ctorParameters = () => [
{ type: PhoneUtilsService }
];
class AngularPhoneUtilsLibModule {
}
AngularPhoneUtilsLibModule.decorators = [
{ type: NgModule, args: [{
declarations: [InternationalFormatPipe, NationalFormatPipe],
exports: [InternationalFormatPipe, NationalFormatPipe],
},] }
];
/*
* Public API Surface of angular-phone-utils-lib
*/
/**
* Generated bundle index. Do not edit.
*/
export { AngularPhoneUtilsLibModule, InternationalFormatPipe, NationalFormatPipe, PhoneUtilsService };
//# sourceMappingURL=angular-phone-utils-lib.js.map