Skip to content

Commit 58b4f10

Browse files
authored
feat: add creditCardIssuer (#888)
1 parent 36cd461 commit 58b4f10

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

src/definitions/finance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ export interface FinanceDefinitions {
99
*/
1010
account_type: string[];
1111
/**
12-
* The pattern by (lowercase) provider name used to generate credit card codes.
12+
* The pattern by (lowercase) issuer name used to generate credit card codes.
1313
* `L` will be replaced by the check bit.
1414
*
1515
* @see Helpers.replaceCreditCardSymbols()
1616
*/
17-
credit_card: { [provider: string]: string[] };
17+
credit_card: { [issuer: string]: string[] };
1818
/**
1919
* Currencies by their full name and their symbols (e.g. `US Dollar` -> `USD` / `$`).
2020
*/

src/finance.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,24 @@ export class Finance {
245245
/**
246246
* Generates a random credit card number.
247247
*
248-
* @param provider The name of the provider (case insensitive) or the format used to generate one.
248+
* @param issuer The name of the issuer (case insensitive) or the format used to generate one.
249249
*
250250
* @example
251251
* faker.finance.creditCardNumber() // '4427163488668'
252252
* faker.finance.creditCardNumber('visa') // '4882664999003'
253253
* faker.finance.creditCardNumber('63[7-9]#-####-####-###L') // '6375-3265-4676-6644'
254254
*/
255-
creditCardNumber(provider = ''): string {
255+
creditCardNumber(issuer = ''): string {
256256
let format: string;
257257
const localeFormat = this.faker.definitions.finance.credit_card;
258-
const normalizedProvider = provider.toLowerCase();
259-
if (normalizedProvider in localeFormat) {
260-
format = this.faker.random.arrayElement(localeFormat[normalizedProvider]);
261-
} else if (provider.match(/#/)) {
258+
const normalizedIssuer = issuer.toLowerCase();
259+
if (normalizedIssuer in localeFormat) {
260+
format = this.faker.random.arrayElement(localeFormat[normalizedIssuer]);
261+
} else if (issuer.match(/#/)) {
262262
// The user chose an optional scheme
263-
format = provider;
263+
format = issuer;
264264
} else {
265-
// Choose a random provider
265+
// Choose a random issuer
266266
// Credit cards are in an object structure
267267
const formats = this.faker.helpers.objectValue(localeFormat); // There could be multiple formats
268268
format = this.faker.random.arrayElement(formats);
@@ -285,6 +285,18 @@ export class Finance {
285285
return cvv;
286286
}
287287

288+
/**
289+
* Returns a random credit card issuer.
290+
*
291+
* @example
292+
* faker.finance.creditCardIssuer() // 'discover'
293+
*/
294+
creditCardIssuer(): string {
295+
return this.faker.helpers.objectKey(
296+
this.faker.definitions.finance.credit_card
297+
) as string;
298+
}
299+
288300
/**
289301
* Generates a random PIN number.
290302
*

test/finance.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ describe('finance', () => {
394394
expect(luhnCheck(faker.finance.creditCardNumber())).toBeTruthy();
395395
});
396396

397-
it('should ignore case for provider', () => {
397+
it('should ignore case for issuer', () => {
398398
const seed = faker.seedValue;
399399

400400
faker.seed(seed);
@@ -450,6 +450,16 @@ describe('finance', () => {
450450
});
451451
});
452452

453+
describe('creditCardIssuer()', () => {
454+
it('should return a string', () => {
455+
const issuer = faker.finance.creditCardIssuer();
456+
expect(issuer).toBeTypeOf('string');
457+
expect(Object.keys(faker.definitions.finance.credit_card)).toContain(
458+
issuer
459+
);
460+
});
461+
});
462+
453463
describe('creditCardCVV()', () => {
454464
it('should return a valid credit card CVV', () => {
455465
const cvv = faker.finance.creditCardCVV();

0 commit comments

Comments
 (0)