Last active
November 16, 2018 18:03
-
-
Save edumelzer/d91259e3c46bcd96acee to your computer and use it in GitHub Desktop.
Validar se numero PIS é valido
This file contains hidden or 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
| Closure isPisValid = { String pis -> | |
| final List tap = [3,2,9,8,7,6,5,4,3,2] | |
| String nroPis = pis.replaceAll("[^0-9]*", "").padLeft(11, '0') | |
| int checker = Integer.parseInt( | |
| String.valueOf(nroPis.charAt(nroPis.length()-1)) | |
| ) | |
| int total = 0 | |
| nroPis.eachWithIndex { String chr, int i -> | |
| if (i < 10) total += Integer.parseInt(chr) * tap[i] | |
| } | |
| int mod = total % 11 | |
| if (mod != 0) { | |
| mod = 11 - mod | |
| } | |
| if (mod in [10,11]) { | |
| mod = 0 | |
| } | |
| return mod == checker | |
| } | |
| assert isPisValid('120.8585.717-7') //true | |
| assert isPisValid('12085857177') //true | |
| assert isPisValid('7') //false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment