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

refactor(finance)!: rewrite creditCardNumber to work with issuer enum #2720

Draft
wants to merge 11 commits into
base: next
Choose a base branch
from

Conversation

ST-DDT
Copy link
Member

@ST-DDT ST-DDT commented Mar 5, 2024

Fixes #181 (our oldest open issue)

(I removed the jsdocs from the implementation signature, as it isn't visible to our users anyway.)

@ST-DDT ST-DDT added c: docs Improvements or additions to documentation p: 1-normal Nothing urgent m: finance Something is referring to the finance module labels Mar 5, 2024
@ST-DDT ST-DDT added this to the vAnytime milestone Mar 5, 2024
@ST-DDT ST-DDT requested review from a team March 5, 2024 16:29
@ST-DDT ST-DDT self-assigned this Mar 5, 2024
Copy link

codecov bot commented Mar 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.94%. Comparing base (7dc8a18) to head (e73034f).

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #2720      +/-   ##
==========================================
- Coverage   99.96%   99.94%   -0.02%     
==========================================
  Files        2977     2965      -12     
  Lines      215422   215430       +8     
  Branches      950      952       +2     
==========================================
- Hits       215351   215317      -34     
- Misses         71      113      +42     
Files Coverage Δ
src/index.ts 100.00% <100.00%> (ø)
.../locales/base/finance/common_credit_card_issuer.ts 100.00% <100.00%> (ø)
src/locales/base/finance/index.ts 100.00% <100.00%> (ø)
src/locales/base/index.ts 100.00% <100.00%> (ø)
src/locales/el/finance/index.ts 100.00% <ø> (ø)
...rc/locales/en/finance/common_credit_card_issuer.ts 100.00% <100.00%> (ø)
src/locales/en/finance/index.ts 100.00% <100.00%> (ø)
...locales/zh_CN/finance/common_credit_card_issuer.ts 100.00% <100.00%> (ø)
src/locales/zh_CN/finance/index.ts 100.00% <100.00%> (ø)
src/modules/finance/index.ts 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

src/modules/finance/index.ts Outdated Show resolved Hide resolved
@ST-DDT ST-DDT requested review from matthewmayer and a team March 5, 2024 17:14
Shinigami92
Shinigami92 previously approved these changes Mar 5, 2024
@ST-DDT ST-DDT added the s: needs decision Needs team/maintainer decision label Mar 6, 2024
@ST-DDT
Copy link
Member Author

ST-DDT commented Mar 7, 2024

Team Decision

  • We will use a literal CreditCardIssuer enum.
  • To check, whether the credit card patterns contain localized parts.
    • If not, remove localized patterns and only use localized data to identify which issuer is preferred in a country
creditCardNumber(options: { issuer?: CreditCardIssuer } = {}) {
  const { issuer = (weighted)arrayElement(defintions.finance.preferred_credit_card_issuer) } = options;

  switch (issuer) {
   case issuer1: ...;
   case issuer2: ...;
}

@ST-DDT
Copy link
Member Author

ST-DDT commented Mar 9, 2024

Which variant would you prefer?

// 1A) Using our "options" syntax?
creditCardNumber(options: { issuer?: CreditCardIssuerType } = {})
creditCardNumber({ issuer: CreditCardIssuer.Visa })

or

// 1B) Like person.firstName
creditCardNumber(issuer: CreditCardIssuerType = this.creditCardIssuer())
creditCardNumber(CreditCardIssuer.Visa)

@ST-DDT ST-DDT changed the title docs(finance): mention where to look up issuer names refactor(finance): rewrite creditCardNumber to work an issuer enum Mar 9, 2024
@ST-DDT ST-DDT marked this pull request as draft March 9, 2024 19:46
@ST-DDT
Copy link
Member Author

ST-DDT commented Mar 9, 2024

Should we allow passing array's of allowed issuers?
And how so?

// 2A) Forbid
... issuer: CreditCardIssuerType ...

or

// 2B) Allow
... issuer: CreditCardIssuerType | CreditCardIssuerType[] ...

or

// 2C) Require
... issuer: CreditCardIssuerType[] ...

@ST-DDT ST-DDT changed the title refactor(finance): rewrite creditCardNumber to work an issuer enum refactor(finance): rewrite creditCardNumber to work with issuer enum Mar 10, 2024
@ST-DDT ST-DDT marked this pull request as ready for review March 11, 2024 15:45
break;
}

default: {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

netlify bot commented Mar 15, 2024

Deploy Preview for fakerjs ready!

Name Link
🔨 Latest commit e73034f
🔍 Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/6648f891d8a5b800081030c2
😎 Deploy Preview https://deploy-preview-2720.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@matthewmayer
Copy link
Contributor

This should be marked breaking no? it changes the behavior when a unknown or non lower case issuer is passed.

@ST-DDT ST-DDT changed the title refactor(finance): rewrite creditCardNumber to work with issuer enum refactor(finance)!: rewrite creditCardNumber to work with issuer enum Mar 16, 2024
Comment on lines +568 to 576
if (typeof issuer !== 'string') {
issuer = issuer.issuer ?? this.creditCardIssuer();
deprecated({
deprecated: 'faker.finance.creditCardNumber({ issuer })',
proposed: 'faker.finance.creditCardNumber(issuer)',
since: '9.0',
until: '10.0',
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to deprecate this? "quick options" are a part of fakers signatures in a lot of places. 🤔

Comment on lines +81 to +86
.it('with issuer option visa', {
issuer: 'visa',
} as unknown as CreditCardIssuerType)
.it('with issuer option mastercard', {
issuer: 'mastercard',
} as unknown as CreditCardIssuerType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these deprecated? 'visa' | 'mastercard' should be included in CreditCardIssuerType. What am I missing?

const actualIssuer = issuer.toLowerCase() as CreditCardIssuerType;
if (actualIssuer !== issuer) {
deprecated({
deprecated: 'faker.finance.creditCardNumber(iSsUeR)',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks absolutely weird to me 👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea how to show case insesitive is deprecated?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about

 deprecated: `faker.finance.creditCardNumber('${issuer}')`,
 proposed: `faker.finance.creditCardNumber('${actualIssuer}')`,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe:

Suggested change
deprecated: 'faker.finance.creditCardNumber(iSsUeR)',
deprecated: 'faker.finance.creditCardNumber(issuerWithRandomCase)',

Comment on lines +30 to +31
AmericanExpress = 'american_express',
DinersClub = 'diners_club',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any conventions yet? Otherwise I would be in favor of using - instead of _ for the string values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any conventions yet?

No. I just used what we use/have already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this change is breaking anyway, we might want to standardize these cases. I'm not that much in favor of any case, but would like to have a "rule" for these types.

Comment on lines +592 to +593
switch (actualIssuer) {
case CreditCardIssuer.AmericanExpress: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wont work when the string value of the enum contains a separator like _ or -, but the actuallIssuer is just lowercased in line 580.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldnt it? The test seem to be working fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, its because issuer is now CreditCardIssuerType strictly typed instead of a string. But why do we need the toLowerCase then in the first place?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backwards compatibility.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a discussion about #2725 again? Otherwise the type def is wrong, because arbitrary strings are not possible anymore.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a discussion about #2725 again?

In the sense, that we follow our usual deprecation steps: Yes

That's why the following comment is there:

// TODO @ST-DDT 2024-03-09: Remove the fallback in v10

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to do this in two stages? In 9.0 log a runtime warning if an unknown or mixed case issuer is passed. Then in 10.0 switch to the enum.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Not sure about that.
JS users already have the backwards compatibility code.
TS users would have more time to migrate at the cost of runtime warnings instead of compile time errors.

@ST-DDT ST-DDT removed the s: needs decision Needs team/maintainer decision label Apr 4, 2024
@ST-DDT ST-DDT marked this pull request as draft April 11, 2024 06:42
@ST-DDT ST-DDT added c: refactor PR that affects the runtime behavior, but doesn't add new features or fixes bugs breaking change Cannot be merged when next version is not a major release and removed c: docs Improvements or additions to documentation labels Apr 11, 2024
@ST-DDT ST-DDT modified the milestones: vAnytime, v9.0 Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Cannot be merged when next version is not a major release c: refactor PR that affects the runtime behavior, but doesn't add new features or fixes bugs m: finance Something is referring to the finance module p: 1-normal Nothing urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Credit Card number parameter
4 participants