-
Notifications
You must be signed in to change notification settings - Fork 146
/
index.ts
151 lines (117 loc) · 3.58 KB
/
index.ts
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
149
150
151
import cardTypes = require("./lib/card-types");
import { addMatchingCardsToResults } from "./lib/add-matching-cards-to-results";
import { isValidInputType } from "./lib/is-valid-input-type";
import { findBestMatch } from "./lib/find-best-match";
import { clone } from "./lib/clone";
import type {
CreditCardType,
CardCollection,
CreditCardTypeCardBrandId,
} from "./types";
let customCards = {} as CardCollection;
const cardNames: Record<string, CreditCardTypeCardBrandId> = {
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",
};
const 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,
];
let testOrder = clone(ORIGINAL_TEST_ORDER) as string[];
function findType(cardType: string | number): CreditCardType {
return customCards[cardType] || cardTypes[cardType];
}
function getAllCardTypes(): CreditCardType[] {
return testOrder.map(
(cardType) => clone(findType(cardType)) as CreditCardType,
);
}
function getCardPosition(
name: string,
ignoreErrorForNotExisting = false,
): number {
const position = testOrder.indexOf(name);
if (!ignoreErrorForNotExisting && position === -1) {
throw new Error('"' + name + '" is not a supported card type.');
}
return position;
}
function creditCardType(cardNumber: string): Array<CreditCardType> {
const results = [] as CreditCardType[];
if (!isValidInputType(cardNumber)) {
return results;
}
if (cardNumber.length === 0) {
return getAllCardTypes();
}
testOrder.forEach((cardType) => {
const cardConfiguration = findType(cardType);
addMatchingCardsToResults(cardNumber, cardConfiguration, results);
});
const bestMatch = findBestMatch(results) as CreditCardType;
if (bestMatch) {
return [bestMatch];
}
return results;
}
creditCardType.getTypeInfo = (cardType: string): CreditCardType =>
clone(findType(cardType)) as CreditCardType;
creditCardType.removeCard = (name: string): void => {
const position = getCardPosition(name);
testOrder.splice(position, 1);
};
creditCardType.addCard = (config: CreditCardType): void => {
const existingCardPosition = getCardPosition(config.type, true);
customCards[config.type] = config;
if (existingCardPosition === -1) {
testOrder.push(config.type);
}
};
creditCardType.updateCard = (
cardType: string,
updates: Partial<CreditCardType>,
): void => {
const originalObject = customCards[cardType] || cardTypes[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.");
}
let clonedCard = clone(originalObject) as CreditCardType;
clonedCard = { ...clonedCard, ...updates };
customCards[clonedCard.type] = clonedCard;
};
creditCardType.changeOrder = (name: string, position: number): void => {
const currentPosition = getCardPosition(name);
testOrder.splice(currentPosition, 1);
testOrder.splice(position, 0, name);
};
creditCardType.resetModifications = (): void => {
testOrder = clone(ORIGINAL_TEST_ORDER) as string[];
customCards = {};
};
creditCardType.types = cardNames;
export = creditCardType;