Last active
June 5, 2020 17:51
-
-
Save LucasCalazans/78232bf2c69830a36b4d3f67d2cb6040 to your computer and use it in GitHub Desktop.
Verify the CC brand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const CC_BRAND_ELO = 'elo'; | |
| export const CC_BRAND_VISA = 'visa'; | |
| export const CC_BRAND_MASTER = 'master'; | |
| export const CC_BRAND_AMEX = 'amex'; | |
| export const CC_BRAND_DISCOVER = 'discover'; | |
| export const CC_BRAND_DINERS = 'diners'; | |
| export const CC_BRAND_HIPER = 'hiper'; | |
| const REPEATED_NUMBERS = 'not-valid'; | |
| export const getCardBrand = number => { | |
| const cleanNumber = number.replace(/[^0-9]/g, ''); | |
| const tests = { | |
| [REPEATED_NUMBERS]: /\b(\d)\1+\b/, | |
| [CC_BRAND_ELO]: /^(636368|636369|438935|504175|451416|636297|5067|4576|4011|506699)/g, | |
| [CC_BRAND_VISA]: /^4[0-9]{12}(?:[0-9]{3})?$/g, | |
| [CC_BRAND_MASTER]: /^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/, | |
| [CC_BRAND_AMEX]: /^3[47]/g, | |
| [CC_BRAND_DISCOVER]: /^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/g, | |
| [CC_BRAND_DINERS]: /^(?:5[45]|36|30[0-5]|3095|3[8-9])\d+$/g, | |
| [CC_BRAND_HIPER]: /^(606282\d{10}(\d{3})?)|(3841\d{15})$/g, | |
| }; | |
| const result = Object.keys(tests).find(key => tests[key].exec(cleanNumber)); | |
| return !!result && result !== REPEATED_NUMBERS && result; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| getCardBrand, | |
| CC_BRAND_ELO, | |
| CC_BRAND_VISA, | |
| CC_BRAND_MASTER, | |
| CC_BRAND_AMEX, | |
| CC_BRAND_DISCOVER, | |
| CC_BRAND_DINERS, | |
| CC_BRAND_HIPER, | |
| } from './cc-utils'; | |
| const ELO_NUMBERS = [ | |
| '5066 9913 7695 2198', | |
| '4389 3540 8898 5860', | |
| '5066 9977 4337 5559', | |
| ]; | |
| const VISA_NUMBERS = [ | |
| '4716 4141 8296 3694', | |
| '4916 8527 3156 3945', | |
| '4716 7042 7034 9594', | |
| ]; | |
| const MASTER_NUMBERS = [ | |
| '5410 6607 6355 6135', | |
| '5357 3154 7223 7585', | |
| '5226 0468 0116 4831', | |
| ]; | |
| const AMEX_NUMBERS = [ | |
| '3717 406763 67271', | |
| '3472 618078 39246', | |
| '3709 406098 25947', | |
| ]; | |
| const DISCOVER_NUMBERS = [ | |
| '6011 3189 3699 1827', | |
| '6011 6879 1256 1485', | |
| '6011 9948 0525 3935', | |
| ]; | |
| const DINERS_NUMBERS = [ | |
| '3880 997000 5526', | |
| '3016 591901 3453', | |
| '3039 111152 3046', | |
| ]; | |
| const HIPER_NUMBERS = [ | |
| '6062 8261 7047 1973', | |
| '6062 8283 9823 9059', | |
| '6062 8248 3920 1659', | |
| ]; | |
| const INVALID_NUMBERS = [ | |
| '1111 1111 1111 1111', | |
| '2222 2222 2222 2222', | |
| '1111 2222 3333 4444', | |
| ]; | |
| describe('CC Utils', () => { | |
| it('[getCardBrand] should detect the correct card brand', () => { | |
| ELO_NUMBERS.forEach(number => | |
| expect(getCardBrand(number)).toBe(CC_BRAND_ELO), | |
| ); | |
| VISA_NUMBERS.forEach(number => | |
| expect(getCardBrand(number)).toBe(CC_BRAND_VISA), | |
| ); | |
| MASTER_NUMBERS.forEach(number => | |
| expect(getCardBrand(number)).toBe(CC_BRAND_MASTER), | |
| ); | |
| AMEX_NUMBERS.forEach(number => | |
| expect(getCardBrand(number)).toBe(CC_BRAND_AMEX), | |
| ); | |
| DISCOVER_NUMBERS.forEach(number => | |
| expect(getCardBrand(number)).toBe(CC_BRAND_DISCOVER), | |
| ); | |
| DINERS_NUMBERS.forEach(number => | |
| expect(getCardBrand(number)).toBe(CC_BRAND_DINERS), | |
| ); | |
| HIPER_NUMBERS.forEach(number => | |
| expect(getCardBrand(number)).toBe(CC_BRAND_HIPER), | |
| ); | |
| INVALID_NUMBERS.forEach(number => expect(getCardBrand(number)).toBeFalsy()); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment