forked from braintree/credit-card-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (113 loc) · 3.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
var types = require('./lib/card-types');
var clone = require('./lib/clone');
var findBestMatch = require('./lib/find-best-match');
var isValidInputType = require('./lib/is-valid-input-type');
var addMatchingCardsToResults = require('./lib/add-matching-cards-to-results');
var testOrder;
var customCards = {};
var cardNames = {
VISA: 'visa',
MASTERCARD: 'mastercard',
AMERICAN_EXPRESS: 'american-express',
DINERS_CLUB: 'diners-club',
DISCOVER: 'discover',
JCB: 'jcb',
UNIONPAY: 'unionpay',
MAESTRO: 'maestro',
ELO: 'elo',
MIR: 'mir',
HIPER: 'hiper',
HIPERCARD: 'hipercard'
};
var ORIGINAL_TEST_ORDER = [
cardNames.VISA,
cardNames.MASTERCARD,
cardNames.AMERICAN_EXPRESS,
cardNames.DINERS_CLUB,
cardNames.DISCOVER,
cardNames.JCB,
cardNames.UNIONPAY,
cardNames.MAESTRO,
cardNames.ELO,
cardNames.MIR,
cardNames.HIPER,
cardNames.HIPERCARD
];
testOrder = clone(ORIGINAL_TEST_ORDER);
function findType(type) {
return customCards[type] || types[type];
}
function getAllCardTypes() {
return testOrder.map(function (type) {
return clone(findType(type));
});
}
function getCardPosition(name, ignoreErrorForNotExisting) {
var position = testOrder.indexOf(name);
if (!ignoreErrorForNotExisting && position === -1) {
throw new Error('"' + name + '" is not a supported card type.');
}
return position;
}
function creditCardType(cardNumber) {
var bestMatch;
var results = [];
if (!isValidInputType(cardNumber)) {
return [];
}
if (cardNumber.length === 0) {
return getAllCardTypes(testOrder);
}
testOrder.forEach(function (type) {
var cardConfiguration = findType(type);
addMatchingCardsToResults(cardNumber, cardConfiguration, results);
});
bestMatch = findBestMatch(results);
if (bestMatch) {
return [bestMatch];
}
return results;
}
creditCardType.getTypeInfo = function (type) {
return clone(findType(type));
};
creditCardType.removeCard = function (name) {
var position = getCardPosition(name);
testOrder.splice(position, 1);
};
creditCardType.addCard = function (config) {
var existingCardPosition = getCardPosition(config.type, true);
customCards[config.type] = config;
if (existingCardPosition === -1) {
testOrder.push(config.type);
}
};
creditCardType.updateCard = function (cardType, updates) {
var clonedCard;
var originalObject = customCards[cardType] || types[cardType];
if (!originalObject) {
throw new Error('"' + cardType + '" is not a recognized type. Use `addCard` instead.');
}
if (updates.type && originalObject.type !== updates.type) {
throw new Error('Cannot overwrite type parameter.');
}
clonedCard = clone(originalObject, true);
Object.keys(clonedCard).forEach(function (key) {
if (updates[key]) {
clonedCard[key] = updates[key];
}
});
customCards[clonedCard.type] = clonedCard;
};
creditCardType.changeOrder = function (name, position) {
var currentPosition = getCardPosition(name);
testOrder.splice(currentPosition, 1);
testOrder.splice(position, 0, name);
};
creditCardType.resetModifications = function () {
testOrder = clone(ORIGINAL_TEST_ORDER);
customCards = {};
};
creditCardType.types = cardNames;
module.exports = creditCardType;