faker icon indicating copy to clipboard operation
faker copied to clipboard

Introduce oneOf helpers function

Open ST-DDT opened this issue 1 year ago • 5 comments
trafficstars

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 avatar Feb 19 '24 11:02 ST-DDT

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

github-actions[bot] avatar Feb 19 '24 11:02 github-actions[bot]

Edge cases:

oneOf(): never (empty)

Alternatively:

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

ST-DDT avatar Feb 19 '24 11:02 ST-DDT

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

ST-DDT avatar Feb 19 '24 12:02 ST-DDT

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.

Shinigami92 avatar Feb 19 '24 13:02 Shinigami92

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.

xDivisionByZerox avatar Mar 03 '24 02:03 xDivisionByZerox