Sha256: 381140b8ec558deeb800bb349b0086b4adce5f3c5a06ed078e950cc7f5f5fb7a
Contents?: true
Size: 643 Bytes
Versions: 73
Compression:
Stored size: 643 Bytes
Contents
'use strict'; function isValid(number) { var numbers = number.replace(/\s/g, ''); const digits = [...numbers]; const sum = digits // convert to integers .map(d => parseInt(d, 10)) // double even positions (odd indexes) .map((d, i) => { if (i % 2 !== 0) { return d * 2; } return d; }) // limit to digits less than 10 .map(d => { if (d > 9) { return d - 9; } return d; }) // sum all digits .reduce((d, acc) => d + acc, 0); return sum > 0 && sum % 10 === 0; } function Luhn(number) { this.valid = isValid(number); } module.exports = Luhn;
Version data entries
73 entries across 73 versions & 1 rubygems