Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Función para validar el CUPS (Código Unificado de Punto de Suministro)
* Valida un CUPS (Código Unificado de Punto de Suministro) dado
* @param {string} CUPS Código CUPS que se quiere verificar. Más información aquí:
* @returns Resultado de la verificación. `true` si el CUPS dado es correcto, o `false` en caso contrario.
function validarCUPS(CUPS) {
let ret = false;
const reCUPS = /^[A-Z]{2}(\d{4}\d{12})([A-Z]{2})(\d[FPCRXYZ])?$/i;
if (reCUPS.test(CUPS)) {
const mCUPS = CUPS.toUpperCase().match(reCUPS);
const [, cups16, control] = mCUPS;
const letters = [
'T', 'R', 'W', 'A', 'G', 'M',
'Y', 'F', 'P', 'D', 'X', 'B',
'N', 'J', 'Z', 'S', 'Q', 'V',
'H', 'L', 'C', 'K', 'E',
const cup16Mod = +cups16 % 529,
quotient = Math.floor(cup16Mod / letters.length),
remainder = cup16Mod % letters.length;
ret = (control === letters[quotient] + letters[remainder]);
return ret;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment