UNPKG

@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.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MobilePhone = void 0; const rich_domain_1 = require("rich-domain"); const ddd_list_1 = require("./ddd.list"); const regexHash = /^\([1-9]{2}\)\s[9](?!\d(?:(\d)\1{2})-(\d)\1{3})[5-9][0-9]{3}\-[0-9]{4}$|^[1-9]{2}9[0-9]{8}$/; const regexHashSpecialChars = /\(|\)|-|\s/g; /** * @description Brazilian Mobile Phone Number * @default (XX) 9XXXX-XXXX */ class MobilePhone extends rich_domain_1.ValueObject { constructor(prop) { super(prop); } toPattern() { return MobilePhone.addMask(this.props); } /** * * @param value Phone number (XX) 9XXXX-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(MobilePhone.REGEX); return isValidDDD && matchPattern; } static isValid(value) { return MobilePhone.isValidProps(value); } isMobile() { return true; } ; isHome() { return false; } ; /** * @returns value XX9XXXXXXXX as string */ value() { return this.props; } /** * * @returns only numbers without special chars. Includes 0 and DDD. * @example 01199502301 */ 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, 7); const partB = phone.slice(7, 11); return `(${ddd}) ${partA}-${partB}`; } /** * * @param value value as string * @returns instance of MobilePhone or throw an error */ static init(value) { const isValidValue = MobilePhone.isValidProps(value); if (!isValidValue) throw new Error(MobilePhone.MESSAGE); const phone = this.removeSpecialChars(value); return new MobilePhone(phone); } /** * * @param value Brazilian Mobile phone number * @example (XX) 9XXXX-XXXX * @returns Result of MobilePhoneValueObject */ static create(value) { if (!MobilePhone.isValidProps(value)) { return rich_domain_1.Result.fail(MobilePhone.MESSAGE); } const phone = this.removeSpecialChars(value); return rich_domain_1.Result.Ok(new MobilePhone(phone)); } } exports.MobilePhone = MobilePhone; MobilePhone.REGEX = regexHash; MobilePhone.MESSAGE = 'Invalid Mobile Phone Number'; exports.default = MobilePhone;