UNPKG

ean-validator

Version:

Validator for Belgium EAN numbers

17 lines (12 loc) 515 B
let uniqueArr = []; function isValid(code) { // Validate length & starting with 54 if(!code.match(/^[5]{1}[4]{1}[0-9]{16}$/)) return false; // Validate logic const sumPart = code.slice(0, -1).split(''); const sumResult = sumPart.reduce((r, n, i) => i % 2 ? r + parseInt(n) : r + parseInt((n * 3)), 0); const nextTenFold = Math.ceil(sumResult/10) * 10; const checkingPart = parseInt(code.slice(-1)); return (nextTenFold - sumResult) === checkingPart; } exports.isValid = isValid;