Skip to content

Commit 9ce1551

Browse files
authored
fix: normalize provider in finance.creditCardNumber (#662)
1 parent 91a1aab commit 9ce1551

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/finance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export class Finance {
249249
/**
250250
* Generates a random credit card number.
251251
*
252-
* @param provider The (lowercase) name of the provider or the format used to generate one.
252+
* @param provider The name of the provider (case insensitive) or the format used to generate one.
253253
*
254254
* @example
255255
* faker.finance.creditCardNumber() // '4427163488668'
@@ -259,8 +259,9 @@ export class Finance {
259259
creditCardNumber(provider = ''): string {
260260
let format: string;
261261
const localeFormat = this.faker.definitions.finance.credit_card;
262-
if (provider in localeFormat) {
263-
format = this.faker.random.arrayElement(localeFormat[provider]);
262+
const normalizedProvider = provider.toLowerCase();
263+
if (normalizedProvider in localeFormat) {
264+
format = this.faker.random.arrayElement(localeFormat[normalizedProvider]);
264265
} else if (provider.match(/#/)) {
265266
// The user chose an optional scheme
266267
format = provider;

test/finance.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ describe('finance', () => {
384384
expect(luhnCheck(faker.finance.creditCardNumber())).toBeTruthy();
385385
});
386386

387+
it('should ignore case for provider', () => {
388+
const seed = faker.seedValue;
389+
390+
faker.seed(seed);
391+
const actualNonLowerCase = faker.finance.creditCardNumber('ViSa');
392+
393+
faker.seed(seed);
394+
const actualLowerCase = faker.finance.creditCardNumber('visa');
395+
396+
expect(actualNonLowerCase).toBe(actualLowerCase);
397+
});
398+
387399
it('should return a correct credit card number when issuer provided', () => {
388400
//TODO: implement checks for each format with regexp
389401
const visa = faker.finance.creditCardNumber('visa');
@@ -417,7 +429,7 @@ describe('finance', () => {
417429
expect(luhnCheck(instapayment)).toBeTruthy();
418430
});
419431

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

0 commit comments

Comments
 (0)