Skip to content

Commit

Permalink
fix: normalize provider in finance.creditCardNumber (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 24, 2022
1 parent 91a1aab commit 9ce1551
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/finance.ts
Expand Up @@ -249,7 +249,7 @@ export class Finance {
/**
* Generates a random credit card number.
*
* @param provider The (lowercase) name of the provider or the format used to generate one.
* @param provider The name of the provider (case insensitive) or the format used to generate one.
*
* @example
* faker.finance.creditCardNumber() // '4427163488668'
Expand All @@ -259,8 +259,9 @@ export class Finance {
creditCardNumber(provider = ''): string {
let format: string;
const localeFormat = this.faker.definitions.finance.credit_card;
if (provider in localeFormat) {
format = this.faker.random.arrayElement(localeFormat[provider]);
const normalizedProvider = provider.toLowerCase();
if (normalizedProvider in localeFormat) {
format = this.faker.random.arrayElement(localeFormat[normalizedProvider]);
} else if (provider.match(/#/)) {
// The user chose an optional scheme
format = provider;
Expand Down
14 changes: 13 additions & 1 deletion test/finance.spec.ts
Expand Up @@ -384,6 +384,18 @@ describe('finance', () => {
expect(luhnCheck(faker.finance.creditCardNumber())).toBeTruthy();
});

it('should ignore case for provider', () => {
const seed = faker.seedValue;

faker.seed(seed);
const actualNonLowerCase = faker.finance.creditCardNumber('ViSa');

faker.seed(seed);
const actualLowerCase = faker.finance.creditCardNumber('visa');

expect(actualNonLowerCase).toBe(actualLowerCase);
});

it('should return a correct credit card number when issuer provided', () => {
//TODO: implement checks for each format with regexp
const visa = faker.finance.creditCardNumber('visa');
Expand Down Expand Up @@ -417,7 +429,7 @@ describe('finance', () => {
expect(luhnCheck(instapayment)).toBeTruthy();
});

it('should return custom formated strings', () => {
it('should return custom formatted strings', () => {
let number = faker.finance.creditCardNumber('###-###-##L');
expect(number).match(/^\d{3}\-\d{3}\-\d{3}$/);
expect(luhnCheck(number)).toBeTruthy();
Expand Down

0 comments on commit 9ce1551

Please sign in to comment.