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

Add exclude on enum faker.helpers.enumValue #2994

Open
mikiyasET opened this issue Jul 7, 2024 · 3 comments
Open

Add exclude on enum faker.helpers.enumValue #2994

mikiyasET opened this issue Jul 7, 2024 · 3 comments
Labels
c: feature Request for new feature has workaround Workaround provided or linked m: helpers Something is referring to the helpers module p: 1-normal Nothing urgent s: waiting for user interest Waiting for more users interested in this feature
Milestone

Comments

@mikiyasET
Copy link

mikiyasET commented Jul 7, 2024

Clear and concise description of the problem

I wanted to seed some data to the database and one of the tables needs image type and an image so while seeding I couldn't add a file since it needs to be uploaded to the server so for the seed purpose only I wanted to remove the file value from the enum or exclude but there is no option for that

Suggested solution

enum ImageType {
  file
  link
  emoji
  constant
}

const imageType = faker.helpers.enumValue(ImageType, {exclude: [ImageType.file]});
@mikiyasET mikiyasET added c: feature Request for new feature s: pending triage Pending Triage s: waiting for user interest Waiting for more users interested in this feature labels Jul 7, 2024
Copy link
Contributor

github-actions bot commented Jul 7, 2024

Thank you for your feature proposal.

We marked it as "waiting for user interest" for now to gather some feedback from our community:

  • If you would like to see this feature be implemented, please react to the description with an up-vote (:+1:).
  • If you have a suggestion or want to point out some special cases that need to be considered, please leave a comment, so we are aware about them.

We would also like to hear about other community members' use cases for the feature to give us a better understanding of their potential implicit or explicit requirements.

We will start the implementation based on:

  • the number of votes (:+1:) and comments
  • the relevance for the ecosystem
  • availability of alternatives and workarounds
  • and the complexity of the requested feature

We do this because:

  • There are plenty of languages/countries out there and we would like to ensure that every method can cover all or almost all of them.
  • Every feature we add to faker has "costs" associated to it:
    • initial costs: design, implementation, reviews, documentation
    • running costs: awareness of the feature itself, more complex module structure, increased bundle size, more work during refactors

View more issues which are waiting for user interest

@ST-DDT
Copy link
Member

ST-DDT commented Jul 7, 2024

As a workaround - although less convenient - you can use the following:

faker.helpers.arrayElement([ImageType.link, ImageType.emoji, ImageType.constant]);

@ST-DDT ST-DDT added this to the vFuture milestone Jul 7, 2024
@ST-DDT ST-DDT added m: helpers Something is referring to the helpers module p: 1-normal Nothing urgent has workaround Workaround provided or linked and removed s: pending triage Pending Triage labels Jul 7, 2024
@xDivisionByZerox
Copy link
Member

As a workaround - although less convenient - you can use the following:

faker.helpers.arrayElement([ImageType.link, ImageType.emoji, ImageType.constant]);

You could also build a function that is basically the implementation of helpers.enumValue():

function enumValue<T extends Record<string | number, string | number>>(enumObj: T, exclude: (keyof T)[]): T[keyof T] {
  const keys: Array<keyof T> = Object.keys(enumObj)
    // ignore numeric keys added by TypeScript
    .filter((key) =>Number.isNaN(Number(key)))
    // this line is what faker is missing
    .filter((key) =>!exclude.includes(key));
  const key = faker.helpers.arrayElement(keys);
  return enumObj[key];
}

// usage

enum ImageType {
  file,
  link,
  emoji,
  constant,
}
cosnt value = enumValue(ImageType , ['file']);
console.log(value); // 1 | 2 | 3 => ImageType.link | ImageType.emoji | ImageType.constant

I'd still consider this a workaround.


Working TypeScrip Playground Example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature has workaround Workaround provided or linked m: helpers Something is referring to the helpers module p: 1-normal Nothing urgent s: waiting for user interest Waiting for more users interested in this feature
Projects
None yet
Development

No branches or pull requests

3 participants