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

Helper to keep calling a function until a criterion is met #3390

Open
matthewmayer opened this issue Feb 1, 2025 · 1 comment
Open

Helper to keep calling a function until a criterion is met #3390

matthewmayer opened this issue Feb 1, 2025 · 1 comment
Assignees
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

@matthewmayer
Copy link
Contributor

Clear and concise description of the problem

We often get requests for additional options for methods to further restrict the output in some way, e.g. limiting the length or the range of outputs, or excluding certain types of data

Suggested solution

In some cases, rather than add many more options, it may make sense to just repeatedly generate until a certain criterion is met. This could be wrapped in a helpers function.

pseudocode.

/** repeatedly calls a function until a condition is met and returns the result */
function generateUntil(func, condition) {
    let result = null
    do {
        result = func()
    } while (!condition(result))
    return result
}

Usage:

const shortCompanyName = faker.helpers.generateUntil(faker.company.name, (name) => name.length < 30)
const femaleNameStartingWithJ = faker.helpers.generateUntil(()=>faker.person.firstName("female"), (name) => name.startsWith("J"))

Alternative

Write the loop manually

Additional context

Note if you provide an impossible condition, this gives an infinite loop. Might need a automatic failure after a certain number of loops.

@matthewmayer matthewmayer 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 Feb 1, 2025
Copy link
Contributor

github-actions bot commented Feb 1, 2025

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

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

2 participants