UNPKG

1.25 kBJavaScriptView Raw
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5var onlyNumbers = _interopDefault(require('@brazilian-utils/helper-only-numbers'));
6var generateChecksum = _interopDefault(require('@brazilian-utils/helper-generate-checksum'));
7
8var RESERVED_ENTRIES = ['00000000000', '11111111111', '22222222222', '33333333333', '44444444444', '55555555555', '66666666666', '77777777777', '88888888888', '99999999999'];
9var VERIFICATION_INDEXES = [9, 10];
10
11function isFormatValid(taxId) {
12 return /^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$/.test(taxId);
13}
14
15function isReserved(taxId) {
16 return RESERVED_ENTRIES.indexOf(taxId) >= 0;
17}
18
19function isChecksumValid(taxId) {
20 return VERIFICATION_INDEXES.every(function (index) {
21 var mod = generateChecksum(taxId.slice(0, index).split('').reduce(function (a, b) {
22 return a + b;
23 }, ''), index + 1) % 11;
24 return taxId[index] === String(mod < 2 ? 0 : 11 - mod);
25 });
26}
27
28function isValidCpf(cpf) {
29 if (!cpf) return false;
30 var numericCPF = onlyNumbers(cpf);
31 return isFormatValid(cpf) && !isReserved(numericCPF) && isChecksumValid(numericCPF);
32}
33
34exports.default = isValidCpf;
35//# sourceMappingURL=is-valid-cpf.cjs.development.js.map