function calcDigit ( ie ) { const { length } = ie ; const position = length - 1 ; let weight = length ; const body = ie . substr ( 0 , position ) ; let sum = 0 ; body . split ( '' ) . forEach ( digit => { sum += + digit * weight ; weight -- ; } ) ; const rest = sum % 11 ; let dig = 11 - rest ; if ( dig >= 10 ) { dig = 0 ; } return dig === + ie . charAt ( position ) ; } function oldStartsWith ( ie ) { const beginWith = [ '01' , '02' , '03' , '99' ] ; const begin = ie . substr ( 2 , 2 ) ; return beginWith . indexOf ( begin ) >= 0 ; } function calcOld ( ie ) { return calcDigit ( ie ) ; } function checkOld ( ie ) { const body = ie . slice ( 0 , 2 ) + ie . slice ( 4 ) ; return oldStartsWith ( ie ) && calcOld ( body ) ; } function calcNew ( ie ) { const { length } = ie ; const position = length - 1 ; let weight = 9 ; const body = ie . substr ( 0 , position ) ; let sum = 0 ; body . split ( '' ) . forEach ( digit => { sum += + digit * weight ; weight -- ; } ) ; const rest = sum % 11 ; let dig = 11 - rest ; if ( rest < 2 ) { dig = 0 ; } return dig === + ie . charAt ( position ) ; } function checkNew ( ie ) { return calcNew ( ie ) ; } export function validateTO ( ie ) { const ieStr = String ( ie ) . replace ( /\D/g , '' ) ; if ( ! [ 9 , 11 ] . includes ( ieStr . length ) ) return false ; const oldCheck = checkOld ( ieStr ) ; const newCheck = checkNew ( ieStr ) ; return oldCheck || newCheck ; }