Skip to content

Commit 4d63ced

Browse files
authored
feat: add support for troy cards (#202)
1 parent 68d5bc6 commit 4d63ced

6 files changed

Lines changed: 102 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 10.3.0
4+
5+
- Add support for Troy cards
6+
37
## 10.2.0
48

59
- feat(deps): Add [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) to the repo

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ console.log(ambiguousCards[2].niceType); // 'Maestro'
3636

3737
| Key | Type | Description |
3838
| ---------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
39-
| `niceType` | `String` | A pretty printed representation of the card brand.<br/>- `Visa`<br />- `Mastercard`<br />- `American Express`<br />- `Diners Club`<br />- `Discover`<br />- `JCB`<br />- `UnionPay`<br />- `Maestro`<br />- `Mir`<br />- `Elo`<br />- `Hiper`<br />- `Hipercard`<br />- `Verve`<br />- `Naranja` |
40-
| `type` | `String` | A code-friendly presentation of the card brand (useful to class names in CSS). Please refer to Card Type "Constants" below for the list of possible values.<br/>- `visa`<br />- `mastercard`<br />- `american-express`<br />- `diners-club`<br />- `discover`<br />- `jcb`<br />- `unionpay`<br />- `maestro`<br />- `mir`<br /> - `elo`<br /> - `hiper`<br /> - `hipercard`<br /> - `verve`<br /> - `naranja` |
39+
| `niceType` | `String` | A pretty printed representation of the card brand.<br/>- `Visa`<br />- `Mastercard`<br />- `American Express`<br />- `Diners Club`<br />- `Discover`<br />- `JCB`<br />- `UnionPay`<br />- `Maestro`<br />- `Mir`<br />- `Elo`<br />- `Hiper`<br />- `Hipercard`<br />- `Verve`<br />- `Naranja`<br />- `Troy` |
40+
| `type` | `String` | A code-friendly presentation of the card brand (useful to class names in CSS). Please refer to Card Type "Constants" below for the list of possible values.<br/>- `visa`<br />- `mastercard`<br />- `american-express`<br />- `diners-club`<br />- `discover`<br />- `jcb`<br />- `unionpay`<br />- `maestro`<br />- `mir`<br /> - `elo`<br /> - `hiper`<br /> - `hipercard`<br /> - `verve`<br /> - `naranja`<br /> - `troy` |
4141
| `gaps` | `Array` | The expected indices of gaps in a string representation of the card number. For example, in a Visa card, `4111 1111 1111 1111`, there are expected spaces in the 4th, 8th, and 12th positions. This is useful in setting your own formatting rules. |
4242
| `lengths` | `Array` | The expected lengths of the card number as an array of strings (excluding spaces and `/` characters). |
4343
| `code` | `Object` | The information regarding the security code for the determined card. Learn more about the [code object](#code) below. |
@@ -68,6 +68,7 @@ Named variables are provided for each of the supported card types:
6868
- `VISA`
6969
- `VERVE`
7070
- `NARANJA`
71+
- `TROY`
7172

7273
#### `code`
7374

@@ -89,6 +90,7 @@ Card brands provide different nomenclature for their security codes as well as v
8990
| `Hipercard` | `CVC` | 4 |
9091
| `Verve` | `CVV` | 3 |
9192
| `Naranja` | `CVV` | 3 |
93+
| `Troy` | `CVV` | 3 |
9294

9395
A full response for a `Visa` card will look like this:
9496

@@ -190,6 +192,7 @@ Adding new cards puts them at the bottom of the priority for testing. Priority i
190192
creditCardType.types.AMERICAN_EXPRESS,
191193
creditCardType.types.DINERS_CLUB,
192194
creditCardType.types.DISCOVER,
195+
creditCardType.types.TROY,
193196
creditCardType.types.JCB,
194197
creditCardType.types.UNIONPAY,
195198
creditCardType.types.NARANJA,

src/__tests__/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ describe("creditCardType", () => {
5252
["402918", "naranja"],
5353
["527572", "naranja"],
5454

55+
["9792100000000001", "troy"],
56+
["9792090000000004", "troy"],
57+
["6502740000000001", "troy"],
58+
["9792", "troy"],
59+
["650274", "troy"],
60+
["6509230000000000", "troy"],
61+
5562
["506099", "verve"],
5663
["506100", "verve"],
5764
["506127", "verve"],
@@ -190,6 +197,7 @@ describe("creditCardType", () => {
190197
"american-express",
191198
"diners-club",
192199
"discover",
200+
"troy",
193201
"jcb",
194202
"unionpay",
195203
"naranja",
@@ -205,7 +213,10 @@ describe("creditCardType", () => {
205213
["3", ["american-express", "diners-club", "jcb"]],
206214
["5", ["mastercard", "naranja", "verve", "maestro", "elo"]],
207215
["50", ["verve", "maestro", "elo"]],
208-
["6", ["discover", "unionpay", "maestro", "elo", "hiper", "hipercard"]],
216+
[
217+
"6",
218+
["discover", "troy", "unionpay", "maestro", "elo", "hiper", "hipercard"],
219+
],
209220
["60", ["discover", "maestro", "hipercard"]],
210221
["601", ["discover", "maestro"]],
211222
["64", ["discover", "maestro"]],
@@ -227,6 +238,12 @@ describe("creditCardType", () => {
227238
["58956", ["naranja", "maestro"]],
228239
["589562", ["naranja"]],
229240

241+
["9", ["troy"]],
242+
["97", ["troy"]],
243+
["979", ["troy"]],
244+
["650274", ["troy"]],
245+
["650923", ["troy"]],
246+
230247
["506099", ["verve"]],
231248
["506100", ["verve"]],
232249
["506195", ["verve"]],
@@ -254,7 +271,7 @@ describe("creditCardType", () => {
254271
["6370", ["maestro", "hiper"]],
255272
["6376", ["maestro", "hiper"]],
256273
["6375", ["maestro", "hiper"]],
257-
["65", ["discover", "maestro", "elo"]],
274+
["65", ["discover", "troy", "maestro", "elo"]],
258275
["655", ["discover", "maestro", "elo"]],
259276
["6550", ["discover", "maestro", "elo"]],
260277
["65502", ["discover", "maestro", "elo"]],
@@ -280,7 +297,6 @@ describe("creditCardType", () => {
280297
"32",
281298
"33",
282299
"7",
283-
"9",
284300
])("returns an empty array for %s", (unknown) => {
285301
expect(creditCardType(unknown)).toHaveLength(0);
286302
});
@@ -297,6 +313,7 @@ describe("creditCardType", () => {
297313
["Mir", "2200000000000000", { size: 3, name: "CVP2" }],
298314
["Verve", "5061001234567890", { size: 3, name: "CVV" }],
299315
["Naranja", "5895626292990446", { size: 3, name: "CVV" }],
316+
["Troy", "9792100000000001", { size: 3, name: "CVV" }],
300317
])("returns security codes for %s", (brand, number, code) => {
301318
const parsedCode = creditCardType(number)[0].code;
302319

@@ -317,6 +334,7 @@ describe("creditCardType", () => {
317334
["Mir", "220", { type: "mir", lengths: [16, 17, 18, 19] }],
318335
["Verve", "5061", { type: "verve", lengths: [16, 18, 19] }],
319336
["Naranja", "589562", { type: "naranja", lengths: [16] }],
337+
["Troy", "9792", { type: "troy", lengths: [16] }],
320338
])("returns lengths for %s", (brand, number, meta) => {
321339
const cardType = creditCardType(number)[0];
322340

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const cardNames: Record<string, CreditCardTypeCardBrandId> = {
1717
AMERICAN_EXPRESS: "american-express",
1818
DINERS_CLUB: "diners-club",
1919
DISCOVER: "discover",
20+
TROY: "troy",
2021
JCB: "jcb",
2122
UNIONPAY: "unionpay",
2223
NARANJA: "naranja",
@@ -34,6 +35,7 @@ const ORIGINAL_TEST_ORDER = [
3435
cardNames.AMERICAN_EXPRESS,
3536
cardNames.DINERS_CLUB,
3637
cardNames.DISCOVER,
38+
cardNames.TROY,
3739
cardNames.JCB,
3840
cardNames.UNIONPAY,
3941
cardNames.NARANJA,

src/lib/card-types.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,73 @@ const cardTypes: CardCollection = {
4545
size: 3,
4646
},
4747
} as BuiltInCreditCardType,
48+
troy: {
49+
niceType: "Troy",
50+
type: "troy",
51+
patterns: [
52+
9792,
53+
650052,
54+
[650082, 650083],
55+
650092,
56+
650161,
57+
650170,
58+
650173,
59+
650175,
60+
650268,
61+
650271,
62+
[650273, 650274],
63+
[650456, 650457],
64+
650836,
65+
[650846, 650850],
66+
650923,
67+
650987,
68+
650990,
69+
654997,
70+
657366,
71+
657998,
72+
658758,
73+
[658767, 658768],
74+
[65083700, 65083704],
75+
[65085800, 65085804],
76+
[65085900, 65085901],
77+
65086000,
78+
[65086100, 65086105],
79+
[65086200, 65086203],
80+
[65875000, 65875003],
81+
65875009,
82+
[65875101, 65875104],
83+
65875200,
84+
65875501,
85+
65875601,
86+
65875900,
87+
[65876000, 65876002],
88+
[65876100, 65876103],
89+
65876110,
90+
[65876115, 65876116],
91+
[65876200, 65876201],
92+
65876500,
93+
[65876504, 65876505],
94+
[65876600, 65876602],
95+
[65877100, 65877101],
96+
[65877600, 65877602],
97+
65877700,
98+
[65877801, 65877802],
99+
[65878200, 65878202],
100+
[65878300, 65878311],
101+
[65878400, 65878405],
102+
[65878500, 65878505],
103+
[65878600, 65878601],
104+
65879800,
105+
65880800,
106+
65880900,
107+
],
108+
gaps: [4, 8, 12],
109+
lengths: [16],
110+
code: {
111+
name: "CVV",
112+
size: 3,
113+
},
114+
} as BuiltInCreditCardType,
48115
discover: {
49116
niceType: "Discover",
50117
type: "discover",

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type CreditCardTypeCardBrandId =
22
| "american-express"
33
| "diners-club"
44
| "discover"
5+
| "troy"
56
| "elo"
67
| "hiper"
78
| "hipercard"
@@ -28,7 +29,8 @@ type CreditCardTypeCardBrandNiceType =
2829
| "UnionPay"
2930
| "Visa"
3031
| "Verve"
31-
| "Naranja";
32+
| "Naranja"
33+
| "Troy";
3234

3335
type CreditCardTypeSecurityCodeLabel =
3436
| "CVV"

0 commit comments

Comments
 (0)