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 method/option to pick elements from an array multiple times #3222

Open
ST-DDT opened this issue Oct 26, 2024 · 1 comment
Open

Add method/option to pick elements from an array multiple times #3222

ST-DDT opened this issue Oct 26, 2024 · 1 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
Milestone

Comments

@ST-DDT
Copy link
Member

ST-DDT commented Oct 26, 2024

Clear and concise description of the problem

The current arrayElements method only takes elements from an array once.
The multiple method only uses a function as input.

I need a method to pick random elements from an array multiple times.

Suggested solution

Either as an additional parameter for the existing arrayElements repeatedElements: boolean = false or a new method (not sure what to call it)


Here a pseudo implementations with some potential performance improvements (safes a few invocations to faker.number.int by stacking them):

function multipleArrayElements(values: T[], count: numberOrRange) {

  const valuesLength = values.length:
  const divisions = Math.floor(Math.log(Number.MAX_SAFE_INTEGER) / Math.log(valuesLength));
  const base = valuesLength ** divisions;
  const repeats = Math.floor(count / divisions);
  const remains = count % divisions

  const result = [];
  for(let i = 0; i<repeats; i++) {
    let random = faker.number.int(base);
    for (let j=0; j<divisions; j++) {
	  const index = random % valuesLength;
      random /= valuesLength;
      result.push(values[index]);
    }
  }
  let random = faker.number.int(base);
  for (let j=0; j<remains; j++) {
	const index = random % valuesLength;
    random /= valuesLength;
    result.push(values[index]);
  }
  return result;
}

The performance part needs actual benchmarks though.

Alternative

Keep using the existing code.

Additional context

This method can be used to improve the performance of the string module and similar modules.

And also reduce code duplication a bit, namely code like this:

return this.faker.helpers
.multiple(() => this.faker.helpers.arrayElement(characters as string[]), {
count: length,
})

Due to "improvements" you would need to call faker.number.int() only once per 11 random elements of 26 characters.

@ST-DDT ST-DDT 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 Oct 26, 2024
Copy link
Contributor

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 ST-DDT added this to the vFuture milestone Oct 26, 2024
@ST-DDT ST-DDT added m: helpers Something is referring to the helpers module p: 1-normal Nothing urgent and removed s: pending triage Pending Triage labels Oct 26, 2024
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

1 participant