Skip to content

Commit

Permalink
feat: add creditCardIssuer (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
beninsydney committed Apr 30, 2022
1 parent 36cd461 commit 58b4f10
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/definitions/finance.ts
Expand Up @@ -9,12 +9,12 @@ export interface FinanceDefinitions {
*/
account_type: string[];
/**
* The pattern by (lowercase) provider name used to generate credit card codes.
* The pattern by (lowercase) issuer name used to generate credit card codes.
* `L` will be replaced by the check bit.
*
* @see Helpers.replaceCreditCardSymbols()
*/
credit_card: { [provider: string]: string[] };
credit_card: { [issuer: string]: string[] };
/**
* Currencies by their full name and their symbols (e.g. `US Dollar` -> `USD` / `$`).
*/
Expand Down
28 changes: 20 additions & 8 deletions src/finance.ts
Expand Up @@ -245,24 +245,24 @@ export class Finance {
/**
* Generates a random credit card number.
*
* @param provider The name of the provider (case insensitive) or the format used to generate one.
* @param issuer The name of the issuer (case insensitive) or the format used to generate one.
*
* @example
* faker.finance.creditCardNumber() // '4427163488668'
* faker.finance.creditCardNumber('visa') // '4882664999003'
* faker.finance.creditCardNumber('63[7-9]#-####-####-###L') // '6375-3265-4676-6644'
*/
creditCardNumber(provider = ''): string {
creditCardNumber(issuer = ''): string {
let format: string;
const localeFormat = this.faker.definitions.finance.credit_card;
const normalizedProvider = provider.toLowerCase();
if (normalizedProvider in localeFormat) {
format = this.faker.random.arrayElement(localeFormat[normalizedProvider]);
} else if (provider.match(/#/)) {
const normalizedIssuer = issuer.toLowerCase();
if (normalizedIssuer in localeFormat) {
format = this.faker.random.arrayElement(localeFormat[normalizedIssuer]);
} else if (issuer.match(/#/)) {
// The user chose an optional scheme
format = provider;
format = issuer;
} else {
// Choose a random provider
// Choose a random issuer
// Credit cards are in an object structure
const formats = this.faker.helpers.objectValue(localeFormat); // There could be multiple formats
format = this.faker.random.arrayElement(formats);
Expand All @@ -285,6 +285,18 @@ export class Finance {
return cvv;
}

/**
* Returns a random credit card issuer.
*
* @example
* faker.finance.creditCardIssuer() // 'discover'
*/
creditCardIssuer(): string {
return this.faker.helpers.objectKey(
this.faker.definitions.finance.credit_card
) as string;
}

/**
* Generates a random PIN number.
*
Expand Down
12 changes: 11 additions & 1 deletion test/finance.spec.ts
Expand Up @@ -394,7 +394,7 @@ describe('finance', () => {
expect(luhnCheck(faker.finance.creditCardNumber())).toBeTruthy();
});

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

faker.seed(seed);
Expand Down Expand Up @@ -450,6 +450,16 @@ describe('finance', () => {
});
});

describe('creditCardIssuer()', () => {
it('should return a string', () => {
const issuer = faker.finance.creditCardIssuer();
expect(issuer).toBeTypeOf('string');
expect(Object.keys(faker.definitions.finance.credit_card)).toContain(
issuer
);
});
});

describe('creditCardCVV()', () => {
it('should return a valid credit card CVV', () => {
const cvv = faker.finance.creditCardCVV();
Expand Down

0 comments on commit 58b4f10

Please sign in to comment.