Skip to content

Commit

Permalink
added percent formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaviano-Rodrigues committed Dec 4, 2024
1 parent 37c00ef commit 33cced3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flaformatter",
"version": "2.0.3",
"version": "2.1.0",
"description": "A formatter to easily your code",
"homepage": "https://github.com/Flaviano-Rodrigues/flaformatter",
"repository": {
Expand Down Expand Up @@ -32,4 +32,4 @@
"eslint-plugin-promise": "^6.1.1",
"typescript": "^4.9.4"
}
}
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Response:

- `card`: 1234 1234 1234 1234

- `percent`: 12,34%

<br />

## Feature maxLength :
Expand Down
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface e {

const flaformatter = (
e: e,
type: 'letters' | 'rg' | 'cpf' | 'cnpj' | 'cpf/cnpj' | 'telefone' | 'int' | 'celular' | 'data' | 'cep' | 'money' | 'card',
type: 'letters' | 'rg' | 'cpf' | 'cnpj' | 'cpf/cnpj' | 'telefone' | 'int' | 'celular' | 'data' | 'cep' | 'money' | 'card' | 'percent',
setMax = false,
callback = null
): void => {
Expand Down Expand Up @@ -76,6 +76,22 @@ const flaformatter = (
val === 'NaN' ? e.target.value = '0,00' : e.target.value = val
max(16)
break
case 'percent':
// 12.34% com limite até 100%
let percentValue = parseFloat(val.replace(',', '.'))

percentValue = percentValue / 100

if (percentValue > 100) {
percentValue = 100
}

val = percentValue.toFixed(2).replace('.', ',')

val === 'NaN' ? e.target.value = '0,00%' : e.target.value = `${val}%`

max(6)
break
case 'card':
// 1234 1234 1234 1234
val.length === 16 ? e.target.value = val.replace(/(\d{4})(\d{4})(\d{4})(\d{4})/, '$1 $2 $3 $4') : e.target.value = val
Expand Down

0 comments on commit 33cced3

Please sign in to comment.