/** * RUT (Registro Único Tributario, Uruguay tax number). * * The Registro Único Tributario (RUT) is an identifier of legal entities for * tax purposes. * * This number consists of 12 digits, the first two indicate the registration * number (01 to 22), followed by a 6 digit sequence number, followed by 001 * and a check digit. * * Source * https://www.agesic.gub.uy/innovaportal/file/1634/1/modelo_de_datos.pdf (page 71) * https://servicios.dgi.gub.uy/ServiciosEnLinea/dgi--servicios-en-linea--consulta-de-certifcado-unico * https://github.com/arthurdejong/python-stdnum/commit/0218601 (prefix 22) * * ENTITY */ import * as exceptions from '../exceptions'; import { strings, weightedSum } from '../util'; import { Validator, ValidateReturn } from '../types'; import { pymod } from '../util/pymod'; function clean(input: string): ReturnType { const [value, err] = strings.cleanUnicode(input, ' -'); if (err) { return [value, err]; } if (value.startsWith('UY')) { return [value.slice(2), null]; } return [value, err]; } const impl: Validator = { name: 'Uruguayan Tax Number', localName: 'Registro Único Tributario', abbreviation: 'RUT', examples: ['211003420017'], formats: ['nn-nnnnnn-nnn-n'], compact(input: string): string { const [value, err] = clean(input); if (err) { throw err; } return value; }, format(input: string): string { const [value] = clean(input); return strings.splitAt(value, 2, -4, -1).join('-'); }, validate(input: string): ValidateReturn { const [value, error] = clean(input); if (error) { return { isValid: false, error }; } if (value.length !== 12) { return { isValid: false, error: new exceptions.InvalidLength() }; } if (!strings.isdigits(value)) { return { isValid: false, error: new exceptions.InvalidFormat() }; } if (parseInt(value.slice(0, 2), 10) > 22 || value.slice(0, 2) === '00') { return { isValid: false, error: new exceptions.InvalidComponent() }; } if (value.slice(2, 8) === '000000') { return { isValid: false, error: new exceptions.InvalidComponent() }; } if (value.slice(8, 11) !== '001') { return { isValid: false, error: new exceptions.InvalidComponent() }; } const [front, check] = strings.splitAt(value, 11); const sum = weightedSum(front, { weights: [4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], modulus: 11, }); const digit = String(pymod(-sum, 11)); if (check !== digit) { return { isValid: false, error: new exceptions.InvalidChecksum() }; } return { isValid: true, compact: value, isIndividual: false, isCompany: true, }; }, }; export const { name, localName, abbreviation, examples, formats, validate, format, compact, } = impl;