angular-phone-utils-lib
Version:
Angular library for phone number formatting and validation based on libphonenumber-js
168 lines (160 loc) • 5.45 kB
JavaScript
import { ɵɵdefineInjectable, Injectable, Pipe, NgModule } from '@angular/core';
import { parsePhoneNumberFromString } from 'libphonenumber-js/max';
var PhoneUtilsService = /** @class */ (function () {
function PhoneUtilsService() {
}
/**
* Transform a given phone number to international format
*
* @param value Phone numer
* @param country Two-letters country code
*/
PhoneUtilsService.prototype.getInternational = function (value, country) {
var 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
*/
PhoneUtilsService.prototype.getNational = function (value, country) {
var 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
*/
PhoneUtilsService.prototype.getPlain = function (value, country) {
var 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
*/
PhoneUtilsService.prototype.isValid = function (value, country) {
var 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
*/
PhoneUtilsService.prototype.isValidFormControl = function (country) {
var _this = this;
return function (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",
},] }
];
return PhoneUtilsService;
}());
var InternationalFormatPipe = /** @class */ (function () {
function InternationalFormatPipe(phoneUtilsService) {
this.phoneUtilsService = phoneUtilsService;
}
InternationalFormatPipe.prototype.transform = function (value) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return this.phoneUtilsService.getInternational(value, args[0]);
};
InternationalFormatPipe.ctorParameters = function () { return [
{ type: PhoneUtilsService }
]; };
InternationalFormatPipe.decorators = [
{ type: Pipe, args: [{
name: 'internationalFormat'
},] }
];
InternationalFormatPipe.ctorParameters = function () { return [
{ type: PhoneUtilsService }
]; };
return InternationalFormatPipe;
}());
var NationalFormatPipe = /** @class */ (function () {
function NationalFormatPipe(phoneUtilsService) {
this.phoneUtilsService = phoneUtilsService;
}
NationalFormatPipe.prototype.transform = function (value) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return this.phoneUtilsService.getNational(value, args[0]);
};
NationalFormatPipe.ctorParameters = function () { return [
{ type: PhoneUtilsService }
]; };
NationalFormatPipe.decorators = [
{ type: Pipe, args: [{
name: 'nationalFormat'
},] }
];
NationalFormatPipe.ctorParameters = function () { return [
{ type: PhoneUtilsService }
]; };
return NationalFormatPipe;
}());
var AngularPhoneUtilsLibModule = /** @class */ (function () {
function AngularPhoneUtilsLibModule() {
}
AngularPhoneUtilsLibModule.decorators = [
{ type: NgModule, args: [{
declarations: [InternationalFormatPipe, NationalFormatPipe],
exports: [InternationalFormatPipe, NationalFormatPipe],
},] }
];
return AngularPhoneUtilsLibModule;
}());
/*
* 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