UNPKG

identity-number-validator

Version:
61 lines (60 loc) 2.27 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ecuadorianIdentifierValidator = void 0; var utils_1 = __importDefault(require("../utils")); exports.ecuadorianIdentifierValidator = function (identifier) { if (typeof identifier !== 'string') { throw Error('Not a string'); } if (identifier.length !== 10) { return false; } var identifierWithoutLastDigit = identifier.slice(0, -1); var verifierNumber = identifier.slice(-1); var evenDigits = []; var oddDigits = []; __spread(identifierWithoutLastDigit).forEach(function (digit, index) { var parsedDigit = parseInt(digit); if ((index + 1) % 2 === 0) { evenDigits.push(parsedDigit); } else { var oddDigitMultipliedByTwo = parsedDigit * 2; if (oddDigitMultipliedByTwo > 9) { oddDigitMultipliedByTwo = oddDigitMultipliedByTwo - 9; } oddDigits.push(oddDigitMultipliedByTwo); } }); var oddDigitsSum = oddDigits.reduce(utils_1.default.sumArray, 0); var evenDigitsSum = evenDigits.reduce(utils_1.default.sumArray, 0); var sumOfAllDigits = oddDigitsSum + evenDigitsSum; var calculatedVerifierNumber = sumOfAllDigits % 10; if (calculatedVerifierNumber !== 0) { calculatedVerifierNumber = 10 - calculatedVerifierNumber; } return calculatedVerifierNumber === parseInt(verifierNumber); };