formatToPhone formata uma string de número de telefone de acordo com as regras de formatação de um país específico, identificadas pelo código do país e um prefixo opcional.ts
import { formatToPhone } from "@arkyn/shared";
prop) deve seguir um dos seguintes formatos:"+<códigoDoPaís> <númeroDeTelefone>""+<códigoDoPaís>-<prefixo> <númeroDeTelefone>"propstring---
title: formatToPhone
description: Formats a phone number string based on the country code and optional prefix.formatToPhone function formats a phone number string according to the formatting rules of a specific country, identified by the country code and an optional prefix.ts
import { formatToPhone } from "@arkyn/shared"; ```## Input FormatThe input string (`prop`) must follow one of the following formats:- `"+<countrycode> <phonenumber>"`- `"+<countrycode>-<prefix> <phonenumber>"`## ParametersThe function accepts the following parameters:**`prop`**- **Type**: `string`- **Description**: The phone number string to be formatted, including the country code and an optional prefix.- **Required**: Yes- **Default**: N/A## ReturnsThe function returns a `string` with the phone number formatted according to the country's rules.## ErrorsThe function may generate the following errors:- `Error("Invalid phone number format...")`: If the input string does not match the expected format. - `Error("Invalid phone number format")`: If the country code or phone number is missing.- `Error("Invalid country code or prefix")`: If the country code and prefix combination is invalid.- `Error("Invalid country code")`: If the country code is invalid.## Usage Example### Formatting a Brazilian Phone Number```javascriptimport { formatToPhone } from "./formatToPhone";const formattedPhone = formatToPhone("+55 11912345678");console.log(formattedPhone); // Output: "(11) 91234-5678"
javascript
import { formatToPhone } from "./formatToPhone";const formattedPhone = formatToPhone("+1-123 4567890");console.log(formattedPhone); // Output: "(123) 456-7890"
javascript
import { formatToPhone } from "./formatToPhone";try {formatToPhone("11912345678");} catch (error) {console.error(error.message); // Output: "Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>"}
string com o número de telefone formatado de acordo com as regras do país.Error("Invalid phone number format..."): Se a string de entrada não corresponder ao formato esperado.Error("Invalid phone number format"): Se o código do país ou o número de telefone estiverem ausentes.Error("Invalid country code or prefix"): Se a combinação de código de país e prefixo for inválida.Error("Invalid country code"): Se o código do país for inválido.javascript
import { formatToPhone } from "./formatToPhone";const formattedPhone = formatToPhone("+55 11912345678");console.log(formattedPhone); // Saída: "(11) 91234-5678"
javascript
import { formatToPhone } from "./formatToPhone";const formattedPhone = formatToPhone("+1-123 4567890");console.log(formattedPhone); // Saída: "(123) 456-7890"
javascript
import { formatToPhone } from "./formatToPhone";try {formatToPhone("11912345678");} catch (error) {console.error(error.message);// Saída: "Invalid phone number format. Expected format: +<countryCode>-<optionalPrefix> <phoneNumber>"}