Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize provider in finance.creditCardNumber #662

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/finance.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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