1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.cardNumber = void 0;
|
4 | var luhn10 = require("./luhn-10");
|
5 | var getCardTypes = require("credit-card-type");
|
6 | function verification(card, isPotentiallyValid, isValid) {
|
7 | return {
|
8 | card: card,
|
9 | isPotentiallyValid: isPotentiallyValid,
|
10 | isValid: isValid,
|
11 | };
|
12 | }
|
13 | function cardNumber(value, options) {
|
14 | if (options === void 0) { options = {}; }
|
15 | var isPotentiallyValid, isValid, maxLength;
|
16 | if (typeof value !== "string" && typeof value !== "number") {
|
17 | return verification(null, false, false);
|
18 | }
|
19 | var testCardValue = String(value).replace(/-|\s/g, "");
|
20 | if (!/^\d*$/.test(testCardValue)) {
|
21 | return verification(null, false, false);
|
22 | }
|
23 | var potentialTypes = getCardTypes(testCardValue);
|
24 | if (potentialTypes.length === 0) {
|
25 | return verification(null, false, false);
|
26 | }
|
27 | else if (potentialTypes.length !== 1) {
|
28 | return verification(null, true, false);
|
29 | }
|
30 | var cardType = potentialTypes[0];
|
31 | if (options.maxLength && testCardValue.length > options.maxLength) {
|
32 | return verification(cardType, false, false);
|
33 | }
|
34 | if (options.skipLuhnValidation === true ||
|
35 | (cardType.type === getCardTypes.types.UNIONPAY &&
|
36 | options.luhnValidateUnionPay !== true)) {
|
37 | isValid = true;
|
38 | }
|
39 | else {
|
40 | isValid = luhn10(testCardValue);
|
41 | }
|
42 | maxLength = Math.max.apply(null, cardType.lengths);
|
43 | if (options.maxLength) {
|
44 | maxLength = Math.min(options.maxLength, maxLength);
|
45 | }
|
46 | for (var i = 0; i < cardType.lengths.length; i++) {
|
47 | if (cardType.lengths[i] === testCardValue.length) {
|
48 | isPotentiallyValid = testCardValue.length < maxLength || isValid;
|
49 | return verification(cardType, isPotentiallyValid, isValid);
|
50 | }
|
51 | }
|
52 | return verification(cardType, testCardValue.length < maxLength, false);
|
53 | }
|
54 | exports.cardNumber = cardNumber;
|