UNPKG

@eleva-io/erp-sdk

Version:

SDK oficial para el ERP de Eleva

48 lines 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeClabeCheckDigit = computeClabeCheckDigit; exports.validateAccountNumberCLABE = validateAccountNumberCLABE; const errors_1 = require("../../../../../errors"); const invalid = (path, message) => errors_1.ElevaError.invalidField({ error: { validation: path, code: 'invalid_string', message, path: [path] }, }); /** * Calculates the check digit of a CLABE based on the first 17 digits. * @param clabe17 string with exactly 17 digits (only numbers) * @returns check digit number (0..9) * @throws Error if input does not have 17 numeric digits */ function computeClabeCheckDigit(clabe17) { if (!/^\d{17}$/.test(clabe17)) { throw new Error('the value must be a string with exactly 17 digits'); } const weights = [3, 7, 1]; // weights cycle let sum = 0; for (let i = 0; i < 17; i++) { const digit = Number(clabe17[i]); const weight = weights[i % 3]; const product = digit * weight; sum += product % 10; // add the remainder of the product by 10 } const mod = sum % 10; const checkDigit = (10 - mod) % 10; // if mod == 0 => 0 return checkDigit; } /** * Validates a Mexican CLABE. * Accepts string (can contain spaces or dashes, it is normalized). * @param clabe string with the CLABE to validate * @returns true if valid, false otherwise */ function validateAccountNumberCLABE(clabe, path) { const normalized = clabe.replace(/[\s-]/g, ''); if (!/^\d{18}$/.test(normalized)) { throw invalid(path, 'CLABE must be a string with exactly 18 digits'); } const body = normalized.slice(0, 17); const givenCheckDigit = Number(normalized[17]); if (computeClabeCheckDigit(body) !== givenCheckDigit) { throw invalid(path, 'Invalid CLABE check digit'); } } //# sourceMappingURL=clabe.js.map