UNPKG

@mczen-technologies/tui

Version:

TUI (Tunisia’s Unique Identifier) format validator

26 lines (19 loc) 671 B
/** * The unique identifier serves as the taxpayer's identifier * to the tax authorities in Tunisia, it's composed of 7 digits * with a control key. * * @api public */ class TUI { static alphabet = 'ABCDEFGHJKLMNPQRSTVWXYZ' constructor(value) { this.value = value } isValid() { const regex = new RegExp('[0-9]{7}[A-Z]') if (!regex.test(this.value)) return false const ctrlKey = Array.from(this.value.slice(0, -1), Number).reduce((prevResult, currentValue, index, array) => prevResult + currentValue * (array.length - index), 0) % 23 return this.value.charAt(this.value.length - 1) == [...TUI.alphabet][ctrlKey] } }