Skip to content

Instantly share code, notes, and snippets.

@suissa
Created July 7, 2017 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suissa/7d2feba6869d39c12577452380f5f035 to your computer and use it in GitHub Desktop.
Save suissa/7d2feba6869d39c12577452380f5f035 to your computer and use it in GitHub Desktop.
const mod11 = ( num ) => num % 11
const NOT = ( x ) => !x
const isEqual = ( a ) => ( b ) => b === a
const mergeDigits = ( num1, num2 ) => `${num1}${num2}`
const getTwoLastDigits = ( cpf ) => `${cpf[ 9 ]}${cpf[ 10 ]}`
const getCpfToCheckInArray = ( cpf ) => cpf.substr( 0, 9 ).split( '' )
const generateArray = ( length ) => Array.from( { length }, ( v, k ) => k )
const isIn = ( list ) => ( value ) =>
list.findIndex( v => value === v ) >= 0
const isSameDigitsCPF = ( cpfFull ) =>
isIn( generateArray( 10 ).map( generateStringSequence( 11 ) ) )( cpfFull )
const generateStringSequence = ( times ) => ( char ) =>
( `${char}`.repeat( times ) )
const toSumOfMultiplication = ( total ) => ( result, num, i ) =>
result + ( num * total-- )
const getSumOfMultiplication = ( list, total ) =>
list.reduce( toSumOfMultiplication( total ), 0 )
const getValidationDigit = ( total ) => ( cpf ) =>
getDigit( mod11( getSumOfMultiplication( cpf, total ) ) )
const getDigit = ( num ) =>
( num > 1 )
? 11 - num
: 0
const isValidCPF = ( cpfFull ) => {
const cpf = getCpfToCheckInArray( cpfFull )
const firstDigit = getValidationDigit( 10 )( cpf )
const secondDigit = getValidationDigit( 11 )( cpf.concat( firstDigit ) )
return isEqual( getTwoLastDigits( cpfFull ) )
( mergeDigits( firstDigit, secondDigit ) )
}
const validate = ( CPF ) => NOT( isSameDigitsCPF( CPF ) ) && isValidCPF( CPF )
const CPFS = [
'04998264931', '03506838326','04864713901',
'03506838321', '22222222222', '00000000000'
]
CPFS.forEach( ( cpf ) => console.log( `${cpf}: ${validate( cpf )}` ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment