From 9ce1551ab7c9cafc97e3d051f0632591e34eb3ce Mon Sep 17 00:00:00 2001 From: Shinigami Date: Thu, 24 Mar 2022 08:56:07 +0100 Subject: [PATCH] fix: normalize provider in finance.creditCardNumber (#662) --- src/finance.ts | 7 ++++--- test/finance.spec.ts | 14 +++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/finance.ts b/src/finance.ts index b71e63b2e5c..37f491fe3a1 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -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' @@ -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; diff --git a/test/finance.spec.ts b/test/finance.spec.ts index d49e29a30f8..312f4fb50dd 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -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'); @@ -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();