-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from lucamenor/feat/nup
Adiciona formatter e validator para NUP
- Loading branch information
Showing
10 changed files
with
250 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'package:flutter/services.dart'; | ||
|
||
/// Formata o valor do campo com a máscara de NUP (Numeração Única de Processos): `XXXXXXX-XX.XXXX.X.XX.XXXX` | ||
/// Referência: [Documentação CNJ](https://www.cnj.jus.br/programas-e-acoes/numeracao-unica/) | ||
class NUPInputFormatter extends TextInputFormatter { | ||
@override | ||
TextEditingValue formatEditUpdate( | ||
TextEditingValue oldValue, TextEditingValue newValue) { | ||
// verifica o tamanho máximo do campo | ||
if (newValue.text.length > 20) return oldValue; | ||
|
||
var posicaoCursor = newValue.selection.end; | ||
var substrIndex = 0; | ||
final valorFinal = StringBuffer(); | ||
|
||
if (newValue.text.length >= 8) { | ||
valorFinal.write('${newValue.text.substring(0, substrIndex = 7)}-'); | ||
if (newValue.selection.end >= 7) posicaoCursor++; | ||
} | ||
if (newValue.text.length >= 10) { | ||
valorFinal.write('${newValue.text.substring(7, substrIndex = 9)}.'); | ||
if (newValue.selection.end >= 9) posicaoCursor++; | ||
} | ||
if (newValue.text.length >= 14) { | ||
valorFinal.write('${newValue.text.substring(9, substrIndex = 13)}.'); | ||
if (newValue.selection.end >= 13) posicaoCursor++; | ||
} | ||
if (newValue.text.length >= 15) { | ||
valorFinal.write('${newValue.text.substring(13, substrIndex = 14)}.'); | ||
if (newValue.selection.end >= 14) posicaoCursor++; | ||
} | ||
if (newValue.text.length >= 17) { | ||
valorFinal.write('${newValue.text.substring(14, substrIndex = 16)}.'); | ||
if (newValue.selection.end >= 16) posicaoCursor++; | ||
} | ||
|
||
if (newValue.text.length >= substrIndex) { | ||
valorFinal.write(newValue.text.substring(substrIndex)); | ||
} | ||
|
||
return TextEditingValue( | ||
text: valorFinal.toString(), | ||
selection: TextSelection.collapsed(offset: posicaoCursor), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class NUPValidator { | ||
static const stripRegex = r'[^\d]'; | ||
|
||
static bool isValid(String? nup, {stripBeforeValidation = true}) { | ||
if (stripBeforeValidation) { | ||
nup = strip(nup); | ||
} | ||
|
||
if (nup == null || nup.isEmpty) { | ||
return false; | ||
} | ||
|
||
if (nup.length != 20) { | ||
return false; | ||
} | ||
|
||
final checkDigit = _checkDigit(nup); | ||
return nup.substring(7, 9) == checkDigit.toString(); | ||
} | ||
|
||
static String strip(String? nup) { | ||
var regex = RegExp(stripRegex); | ||
nup = nup ?? ''; | ||
|
||
return nup.replaceAll(regex, ''); | ||
} | ||
|
||
// Compute the Check Digit (or 'Dígito Verificador (DV)' in PT-BR). | ||
// You can learn more about the algorithm on [CNJ (pt-br)](https://atos.cnj.jus.br/files/compilado23285720221017634de539229ab.pdf) | ||
static String _checkDigit(String nup) { | ||
final sequential = nup.substring(0, 7); | ||
final year = nup.substring(9, 13); | ||
final segment = nup[13]; | ||
final court = nup.substring(14, 16); | ||
final origin = nup.substring(16); | ||
|
||
final r1 = int.parse(sequential) % 97; | ||
final r2 = int.parse('$r1$year$segment$court') % 97; | ||
final r3 = int.parse('$r2${origin}00') % 97; | ||
|
||
final checkDigit = 98 - r3; | ||
return checkDigit.toString().padLeft(2, '0'); | ||
} | ||
|
||
static String format(String nup) { | ||
var regExp = RegExp(r'^(\d{7})(\d{2})(\d{4})(\d{1})(\d{2})(\d{4})$'); | ||
|
||
return strip(nup).replaceAllMapped( | ||
regExp, (Match m) => '${m[1]}-${m[2]}.${m[3]}.${m[4]}.${m[5]}.${m[6]}'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:brasil_fields/src/validators/nup_validator.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
test('Test NUP validator', () { | ||
expect(NUPValidator.isValid('0601064-21.2022.6.00.0000'), true); | ||
expect(NUPValidator.isValid('0601064-22.2022.6.00.0000'), false); | ||
expect(NUPValidator.isValid('00601064-22.2022.6.00.0000'), false); | ||
expect(NUPValidator.isValid('06010642120226000000'), true); | ||
expect(NUPValidator.isValid('06010642220226000000'), false); | ||
expect(NUPValidator.isValid('006010642120226000000'), false); | ||
expect( | ||
NUPValidator.isValid('03346teste1671002@mail', | ||
stripBeforeValidation: false), | ||
false); | ||
expect( | ||
NUPValidator.isValid('57abc803.6586-52', stripBeforeValidation: false), | ||
false); | ||
}); | ||
|
||
test('Test NUP formatter', () { | ||
expect(NUPValidator.format('06010642120226000000'), '0601064-21.2022.6.00.0000'); | ||
}); | ||
|
||
test('Test NUP strip', () { | ||
expect(NUPValidator.strip('0601064-21.2022.6.00.0000'), '06010642120226000000'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'package:brasil_fields/src/formatters/nup_input_formatter.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
evaluate(String oldValue, String newValue) { | ||
return NUPInputFormatter() | ||
.formatEditUpdate( | ||
TextEditingValue(text: oldValue), | ||
TextEditingValue(text: newValue), | ||
) | ||
.text; | ||
} | ||
|
||
group('NUPInputFormatter', () { | ||
test('padrao', () => expect(evaluate('', '12345678901234567890'), '1234567-89.0123.4.56.7890')); | ||
test('limite 20 digitos', () => expect(evaluate('', '123456789012345678901'), '')); | ||
test('backspace', () { | ||
expect(evaluate('', '12345678901234567890'), '1234567-89.0123.4.56.7890'); | ||
expect(evaluate('', '1234567890123456789'), '1234567-89.0123.4.56.789'); | ||
expect(evaluate('', '123456789012345678'), '1234567-89.0123.4.56.78'); | ||
expect(evaluate('', '12345678901234567'), '1234567-89.0123.4.56.7'); | ||
expect(evaluate('', '1234567890123456'), '1234567-89.0123.4.56'); | ||
expect(evaluate('', '123456789012345'), '1234567-89.0123.4.5'); | ||
expect(evaluate('', '12345678901234'), '1234567-89.0123.4'); | ||
expect(evaluate('', '1234567890123'), '1234567-89.0123'); | ||
expect(evaluate('', '123456789012'), '1234567-89.012'); | ||
expect(evaluate('', '12345678901'), '1234567-89.01'); | ||
expect(evaluate('', '1234567890'), '1234567-89.0'); | ||
expect(evaluate('', '123456789'), '1234567-89'); | ||
expect(evaluate('', '12345678'), '1234567-8'); | ||
expect(evaluate('', '1234567'), '1234567'); | ||
expect(evaluate('', '123456'), '123456'); | ||
expect(evaluate('', '12345'), '12345'); | ||
expect(evaluate('', '1234'), '1234'); | ||
expect(evaluate('', '123'), '123'); | ||
expect(evaluate('', '12'), '12'); | ||
expect(evaluate('', '1'), '1'); | ||
}); | ||
|
||
test('digitacao', () { | ||
expect(evaluate('', '1'), '1'); | ||
expect(evaluate('', '12'), '12'); | ||
expect(evaluate('', '123'), '123'); | ||
expect(evaluate('', '1234'), '1234'); | ||
expect(evaluate('', '12345'), '12345'); | ||
expect(evaluate('', '123456'), '123456'); | ||
expect(evaluate('', '1234567'), '1234567'); | ||
expect(evaluate('', '12345678'), '1234567-8'); | ||
expect(evaluate('', '123456789'), '1234567-89'); | ||
expect(evaluate('', '1234567890'), '1234567-89.0'); | ||
expect(evaluate('', '12345678901'), '1234567-89.01'); | ||
expect(evaluate('', '123456789012'), '1234567-89.012'); | ||
expect(evaluate('', '1234567890123'), '1234567-89.0123'); | ||
expect(evaluate('', '12345678901234'), '1234567-89.0123.4'); | ||
expect(evaluate('', '123456789012345'), '1234567-89.0123.4.5'); | ||
expect(evaluate('', '1234567890123456'), '1234567-89.0123.4.56'); | ||
expect(evaluate('', '12345678901234567'), '1234567-89.0123.4.56.7'); | ||
expect(evaluate('', '123456789012345678'), '1234567-89.0123.4.56.78'); | ||
expect(evaluate('', '1234567890123456789'), '1234567-89.0123.4.56.789'); | ||
expect(evaluate('', '12345678901234567890'), '1234567-89.0123.4.56.7890'); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters