45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = isCreditCard;
|
|
|
|
var _assertString = require('./util/assertString');
|
|
|
|
var _assertString2 = _interopRequireDefault(_assertString);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
/* eslint-disable max-len */
|
|
var creditCard = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14})$/;
|
|
/* eslint-enable max-len */
|
|
|
|
function isCreditCard(str) {
|
|
(0, _assertString2.default)(str);
|
|
var sanitized = str.replace(/[- ]+/g, '');
|
|
if (!creditCard.test(sanitized)) {
|
|
return false;
|
|
}
|
|
var sum = 0;
|
|
var digit = void 0;
|
|
var tmpNum = void 0;
|
|
var shouldDouble = void 0;
|
|
for (var i = sanitized.length - 1; i >= 0; i--) {
|
|
digit = sanitized.substring(i, i + 1);
|
|
tmpNum = parseInt(digit, 10);
|
|
if (shouldDouble) {
|
|
tmpNum *= 2;
|
|
if (tmpNum >= 10) {
|
|
sum += tmpNum % 10 + 1;
|
|
} else {
|
|
sum += tmpNum;
|
|
}
|
|
} else {
|
|
sum += tmpNum;
|
|
}
|
|
shouldDouble = !shouldDouble;
|
|
}
|
|
return !!(sum % 10 === 0 ? sanitized : false);
|
|
}
|
|
module.exports = exports['default']; |