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

feat: add creditCardIssuer #888

Merged
merged 9 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions src/definitions/finance.ts
Original file line number Diff line number Diff line change
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
29 changes: 21 additions & 8 deletions src/finance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,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.random.objectElement(localeFormat, 'value'); // There could be multiple formats
format = this.faker.random.arrayElement(formats);
Expand All @@ -286,6 +286,19 @@ export class Finance {
return cvv;
}

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

/**
* Generates a random PIN number.
*
Expand Down
12 changes: 11 additions & 1 deletion test/finance.spec.ts
Original file line number Diff line number Diff line change
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('cardCardIssuer()', () => {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
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