Class: Format β
Format - A collection of formatting utilities for strings, numbers, and dates.
Author β
Heliomar P. Marques https://navto.me/heliomarpm
Format.abbreviateNumber β
abbreviateNumber() β
static abbreviateNumber: (value, options) => string;
Abbreviates a number by adding a suffix based on its magnitude.
This function takes a number and returns a string with the number abbreviated using metric suffixes (e.g., K for thousand, M for million).
Parameters β
value β
number
The number to abbreviate.
options β
Options for abbreviation, including:
fractionDigits
: Number of decimal places to include in the abbreviated value.removeEndZero
: Whether to remove trailing zeros after the decimal point.
fractionDigits? β
number
= 2
removeEndZero? β
boolean
= true
Returns β
string
A string representing the abbreviated number with a suffix.
Example β
abbreviateNumber(1500); // '1.50K'
abbreviateNumber(2000000); // '2.00M'
abbreviateNumber(123_456_789); // '123.46M'
abbreviateNumber(1e33); // '1.00D'
Format.currency β
currency() β
static currency: (value, options) => string;
Formats a number as currency.
Parameters β
value β
number
The number to format.
options β
Options for formatting. These include the locale and currency.
currency β
string
locale β
LocalesArgument
Returns β
string
The formatted string.
Example β
Format.currency(123456.78); // 'R$ 123.456,78'Format.currency(1234.56);
// Output: R$ 1.234,56
See β
https://www.w3schools.com/jsref/jsref_tolocalestring.asp
Format.date β
date() β
static date: (date, format, locale) => string;
Formats a Date object into a string according to the specified format. Supported formats are:
- 'a': 'am' or 'pm' in lowercase
- 'A': 'AM' or 'PM' in uppercase
- 'hh': two-digit hours in 12h format (01-12)
- 'h': hours in 12h format (1-12)
- 'HH': two-digit hours in 24h format (00-23)
- 'H': hours in 24h format (0-23)
- 'MM': two-digit minutes (00-59)
- 'ss': two-digit seconds (00-59)
- 'SSS': three-digit milliseconds (000-999)
- 'yyyy': four-digit year (2024)
- 'yy': two-digit year (24)
- 'mmmm': full month name (January, February, ...)
- 'mmm': full month name abbreviated month (Jan, Feb, ...)
- 'mm': two-digit month (01-12)
- 'dddd': full weekday name (Sun, Mon, ...)
- 'ddd': abbreviated weekday name (Sun, Mon, ...)
- 'dd': two-digit day (01-31)
Parameters β
date β
The date to format.
string
| number
| Date
format β
string
The desired format for the output string.
locale β
LocalesArgument
= "default"
The locale to use.
Returns β
string
The date formatted as a string.
Example β
Format.date('2025-03-02', 'dddd, dd mmmm yyyy', 'en-US'); // Output: 'Sunday, 02 March 2025'
See β
https://www.w3schools.com/jsref/jsref_tolocalestring.asp
Format.interpolate β
interpolate() β
static interpolate: (template, ...variables) => string;
Interpolates a template string with the provided variables.
Parameters β
template β
string
The template string to interpolate.
variables β
...(string
| number
)[]
The variables to interpolate into the template.
Returns β
string
The interpolated template string.
Throws β
If a placeholder index is out of range of the provided variables.
Example β
interpolate('Hello, {0}!', 'World'); // 'Hello, World!'
interpolate('The sum of {0} and {1} is {2}.', 1, 2, 3); // 'The sum of 1 and 2 is 3.'
Format.maskIt β
maskIt() β
static maskIt(
value,
maskChar?,
startIndex?,
finalIndex?): string;
Masks a substring of a string with a specified character.
Parameters β
value β
string
The original string to mask.
maskChar? β
string
= "*"
startIndex? β
number
= 0
The starting index of the substring to mask (inclusive).
finalIndex? β
number
= null
The ending index of the substring to mask (exclusive). If null, masks until the end of the string.
Returns β
string
The masked string.
Throws β
maskChar must be a single character
Example β
maskIt('1234567890', '*', 2, 5); // '12*****90'
maskIt('1234567890', '#', 3); // '123########'
Format.maskItParts β
maskItParts() β
static maskItParts(
text,
maskChar?,
visibleChars?): string;
Masks a part of a string with a specified character.
Parameters β
text β
string
The original string to mask.
maskChar? β
string
= "*"
The character to use for masking.
visibleChars? β
number
= 1
The number of visible characters in the masked part.
Returns β
string
The masked string.
Throws β
maskChar must be a single character
Example β
maskItParts('Heliomar P. Marques', '*', 1); // 'H******* P. M******'
maskItParts('+55 (11) 91888-0000', '#', 1); // '+5# (1#) 9####-0###'
maskItParts('123.444.555-67', '_', 2); // '12_.44_.55_-67'
Format.number β
number() β
static number: (value, locale) => string;
Formats a number according to the given locale.
Parameters β
value β
number
The number to format.
locale β
LocalesArgument
= "default"
The locale to use.
Returns β
string
The formatted string.
Example β
Format.number(123456.78); // '123.456,78'Format.number(1234.56);
// Output: 1.234,56
See β
https://www.w3schools.com/jsref/jsref_tolocalestring.asp
Format.onlyNumbers β
onlyNumbers() β
static onlyNumbers: (value) => string;
Removes all non-numeric characters from a string.
Parameters β
value β
string
The string to remove non-numeric characters from.
Returns β
string
The resulting string with only numeric characters.
Example β
onlyNumbers('123abc'); // '123'
onlyNumbers('abc'); // ''
Format.padZerosByRef β
padZerosByRef() β
static padZerosByRef(value, refValue): string;
Pads a number with leading zeros to match the number of digits in a given maximum value.
Parameters β
value β
number
The number to be padded with leading zeros.
refValue β
number
The reference value for determining the maximum length for adding leading zeros.
Returns β
string
the input number padded with leading zeros to match the number of digits in the maximum value.
Example β
Format.padZerosByRef(2, 90); // '02'
Format.padZerosByRef(12, 110); // '012'
Format.ptBr β
ptBr β
static ptBr: object;
Namespace
FormataΓ§Γ΅es em portuguΓͺs brasileiro.
cep() β
cep: (value, fallback?) => string;
Formata um CEP.
Parameters β
value β
string
O valor a ser formatado.
fallback? β
string
= "CEP estΓ‘ incorreto!"
O valor a ser retornado caso o valor informado esteja incorreto.
Returns β
string
O CEP formatado.
Example β
const cep = Format.ptBr.cep('12345678'); // Output: '12345-678'
cnpj() β
cnpj: (value, fallback) => string;
Formata um CNPJ.
Parameters β
value β
string
O valor a ser formatado.
fallback β
string
= "CNPJ estΓ‘ incorreto!"
O valor a ser retornado caso o valor informado esteja incorreto.
Returns β
string
O CNPJ formatado.
Example β
const cnpj = Format.ptBr.cnpj('12345678000195'); // Output: '12.345.678/0001-95'
cpf() β
cpf: (value, fallback) => string;
Formata um CPF.
Parameters β
value β
string
O valor a ser formatado.
fallback β
string
= "CPF estΓ‘ incorreto!"
O valor a ser retornado caso o valor informado esteja incorreto.
Returns β
string
O CPF formatado.
Example β
const cpf = Format.ptBr.cpf('12345678909'); // Output: '123.456.789-09'
telefone() β
telefone: (value, defaultAreaCode?, fallback?) => string;
Formata um nΓΊmero de telefone com DDD. Se o valor informado tiver entre 8 e 11 dΓgitos, ele serΓ‘ formatado com DDD. Caso o valor tenha 8 ou 9 dΓgitos, ele usarΓ‘ o DDD informado como parΓ’metro.
Parameters β
value β
string
O valor a ser formatado.
defaultAreaCode? β
string
= ""
DDD padrΓ£o a ser usado.
fallback? β
string
= "Telefone estΓ‘ incorreto!"
O valor a ser retornado caso o valor informado esteja incorreto.
Returns β
string
O valor formatado.
Throws β
Se o valor informado tiver menos de 8 ou mais de 11 dΓgitos.
Example β
const telefone1 = Format.ptBr.telefone('11987654321'); // Output: '(11) 98765-4321'
const telefone2 = Format.ptBr.telefone('987654321', '11'); // Output: '(11) 98765-4321'
valorPorExtenso() β
valorPorExtenso: (value) => string;
Converte um valor em uma string escrita por extenso.
Parameters β
value β
number
O valor a ser convertido.
Returns β
string
Uma string com o valor escrito por extenso.
Example β
Format.valorPorExtenso(1000); // Output: "mil"
Format.valorPorExtenso(1000000); // Output: "um milhΓ£o"
Format.valorPorExtenso(1000000000); // Output: "um bilhΓ£o"
Format.valorPorExtenso(1000000001); // Output: "um bilhΓ£o e um"
Format.valorPorExtenso(2_000_000_000_000_000); // Output: "dois quatrilhΓ΅es"
Example β
import { Format } from '@heliomarpm/helpers';
const cnpj = Format.ptBr.cnpj('12345678000195'); // Output: '12.345.678/0001-95'
const cpf = Format.ptBr.cpf('12345678909'); // Output: '123.456.789-09'
const cep = Format.ptBr.cep('12345678'); // Output: '12345-678'
const telefone1 = Format.ptBr.telefone('11987654321'); // Output: '(11) 98765-4321'
const telefone2 = Format.ptBr.telefone('987654321', '11'); // Output: '(11) 98765-4321'
const valorPorExtenso = Format.ptBr.valorPorExtenso(123456); // Output: 'cento e vinte e trΓͺs mil quatrocentos e cinquenta e seis'
ptBr
Format.slugify β
slugify() β
static slugify(str): string;
Converts a string to a slug.
Parameters β
str β
string
The string to convert.
Returns β
string
The slugified string.
Example β
Format.slugify('Hello, world!'); // 'hello-world'
Format.titleCase β
titleCase() β
static titleCase(name): string;
Capitalizes the first letter of the full name while maintaining lowercase for specified conjunctions.
Parameters β
name β
string
The full name string to format.
Returns β
string
The formatted name in title case.
Example β
titleCase('john doe de souza'); // 'John Doe de Souza'
titleCase('maria da silva'); // 'Maria da Silva'
Format.truncate β
truncate() β
static truncate: (text, maxLength, ellipsis?) => string;
Truncates a given text to a maximum length and appends an ellipsis.
Parameters β
text β
string
The text to truncate.
maxLength β
number
The maximum allowed length of the text.
ellipsis? β
string
= "..."
The string to append to the end of the truncated text.
Returns β
string
The truncated text.
Throws β
If maxLength is less than or equal to the length of ellipsis.
Example β
truncate('Hello, world!', 5); // 'He...'
truncate('Short text', 20); // 'Short text'