faker icon indicating copy to clipboard operation
faker copied to clipboard

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

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

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:

https://github.com/faker-js/faker/blob/af1dbcdbf363a12c448834dff031a698c8925e2e/src/modules/string/index.ts#L148-L151

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

ST-DDT avatar Oct 26 '24 01:10 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 Oct 26 '24 01:10 github-actions[bot]