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

Introduce oneOf helpers function #2674

Open
ST-DDT opened this issue Feb 19, 2024 · 5 comments
Open

Introduce oneOf helpers function #2674

ST-DDT opened this issue Feb 19, 2024 · 5 comments
Labels
c: feature Request for new feature 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

@ST-DDT
Copy link
Member

ST-DDT commented Feb 19, 2024

Clear and concise description of the problem

Currently, if you want to select one of a few local values, you have to wrap them explicitly in an array before you can pass them to arrayElement.

Suggested solution

Introduce faker.helpers.oneOf(...values: T[]): T that hides the array creation from the user.

Alternative

Stay with arrayElement only.

Additional context

Definition

function oneOf(...values: T[]) {
  return arrayElement(values);
}

Usage with arrays

arrayElement(someArray);
oneOf(...someArray);

Usage with values

arrayElement([a,b,c])
oneOf(a,b,c)
@ST-DDT ST-DDT added c: feature Request for new feature p: 1-normal Nothing urgent m: helpers Something is referring to the helpers module s: waiting for user interest Waiting for more users interested in this feature labels Feb 19, 2024
@ST-DDT ST-DDT added this to the vFuture milestone Feb 19, 2024
Copy link

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 Author

ST-DDT commented Feb 19, 2024

Edge cases:

oneOf(): never (empty)

Alternatively:

oneOf(aValue: T, ...moreValues: T[]): T

@ST-DDT
Copy link
Member Author

ST-DDT commented Feb 19, 2024

Another benefit of oneOf would be oneOf('a', 'b', 'c'): 'a' | 'b' | 'c' at least hopefully.

@Shinigami92
Copy link
Member

Another benefit of oneOf would be oneOf('a', 'b', 'c'): 'a' | 'b' | 'c' at least hopefully.

arrayElement could try to achieve the same with const, not tested but:
arrayElement<const T>(array: ReadonlyArray<T>): T { or something like that, it's relative new TS feature.

@xDivisionByZerox
Copy link
Member

I don't see the necessity of this potential feature.

If you are working with dynamic values you need to create an array anyway.
If you are working with static data, all you have to do is add the parentheses.

As pointed out before, the static value inference can be done with arrays as well since TypeScript v5.0 with the generic const argument.

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 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