@type-ddd/phone
Version:
Library that provides TypeScript type definitions for handling Phone Numbers in Domain-Driven Design contexts. It facilitates the validation and manipulation of Brazilian phone numbers.
120 lines (119 loc) • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HomePhone = void 0;
const rich_domain_1 = require("rich-domain");
const ddd_list_1 = require("./ddd.list");
const regexHash = /^\([1-9]{2}\)\s[2-5][0-9]{3}\-[0-9]{4}$|^[1-9]{2}[2-5]{1}[0-9]{7}$/;
const regexHashSpecialChars = /\(|\)|-|\s/g;
/**
* @description Brazilian Home Phone Number
* @default (XX) XXXX-XXXX
*/
class HomePhone extends rich_domain_1.ValueObject {
constructor(prop) {
super(prop);
}
toPattern() {
return HomePhone.addMask(this.props);
}
isMobile() {
return false;
}
;
isHome() {
return true;
}
;
static isValid(value) {
return this.isValidProps(value);
}
/**
*
* @param value Phone number (XX) XXXX-XXXX
* @returns true if pattern match and false if not.
*/
static isValidProps(value) {
const isValidDDD = ddd_list_1.AreaCodes.includes(this.code(value));
const matchPattern = this.validator.string(value).match(HomePhone.REGEX);
return isValidDDD && matchPattern;
}
/**
* @returns value XXXXXXXXXX as string
*/
value() {
return this.props;
}
/**
*
* @returns only numbers without special chars. Includes 0 and DDD.
* @example 01122502301
*/
toCall() {
const onlyNumbersAsString = this.props;
return `0${onlyNumbersAsString}`;
}
number() {
return this.props.slice(2);
}
/**
*
* @returns only area code (DDD) as number
* @example 11
*/
code() {
return parseInt(this.props.slice(0, 2));
}
/**
*
* @returns only area code (DDD) as number
* @example 11
*/
static code(phone) {
const value = this.util.string(phone).removeSpecialChars();
return parseInt(value.slice(0, 2));
}
uf() {
const ddd = this.code();
return ddd_list_1.UfForCode[ddd];
}
static removeSpecialChars(cell) {
const value = this.util.string(cell).removeSpecialChars();
return this.util.string(value).removeSpaces();
}
static addMask(cell) {
const phone = this.removeSpecialChars(cell);
const ddd = phone.slice(0, 2);
const partA = phone.slice(2, 6);
const partB = phone.slice(6, 10);
return `(${ddd}) ${partA}-${partB}`;
}
/**
*
* @param value value as string
* @returns instance of HomePhone or throw an error
*/
static init(value) {
const isValidValue = HomePhone.isValidProps(value);
if (!isValidValue)
throw new Error(HomePhone.MESSAGE);
const phone = this.removeSpecialChars(value);
return new HomePhone(phone);
}
/**
*
* @param value Brazilian home phone number
* @example (XX) XXXX-XXXX
* @returns Result of HomePhoneValueObject
*/
static create(value) {
if (!HomePhone.isValidProps(value)) {
return rich_domain_1.Result.fail(HomePhone.MESSAGE);
}
const phone = this.removeSpecialChars(value);
return rich_domain_1.Result.Ok(new HomePhone(phone));
}
}
exports.HomePhone = HomePhone;
HomePhone.REGEX = regexHash;
HomePhone.MESSAGE = 'Invalid Home Phone Number';
exports.default = HomePhone;