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

Feature request: random length between min and max #1

Open
raphael-leger opened this issue Oct 8, 2019 · 2 comments
Open

Feature request: random length between min and max #1

raphael-leger opened this issue Oct 8, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@raphael-leger
Copy link

Hi,

It would be great to be able to specify a length range to generate a password:

> password.randomPassword({
    length: {
        min: 8,
        max: 16
    }
})

It would generate a password and its length would randomly be between 8 and 16.

😃

@mkropat
Copy link
Owner

mkropat commented Oct 14, 2019

Thanks for the feature suggestion. I especially appreciate that you provided an example interface.

I like the interface you suggested. One minor question whoever implements this has to decide is what the behavior should be when the min length provided is less than what the character set rules require.

Here is a concrete example:

password.randomPassword({
  characters: [
    { characters: password.upper, exactly: 1 },
    { characters: password.symbols, exactly: 1 },
    password.lower
  ],
  length: {
    min: 2,
    max: 6,
  }
})

^ the implicit minimum length (based on the character set rules) is 3, but the user specified minimum is 2.

Broadly speaking, I can think of two ways to handle this:

  1. Throw an error to tell the developer that their expectations might be wrong
  2. Calculate the effective min as Math.max(min, implicitMin), as long as max >= implicitMin

Personally I'm leaning towards 2, but I think either behavior is fine.

I can probably implement this feature myself sometime in the next few weeks. However, if someone else is interested to do it, please go ahead. (Just be sure to add unit tests for it.)

@mkropat mkropat added the enhancement New feature or request label Oct 14, 2019
@mkropat
Copy link
Owner

mkropat commented Nov 15, 2019

@raphael-leger I implemented this in a PR here. If you get the time, can you pull down the random-length branch and see if it works for you?

I ended up changing the contract slightly so that instead of:

    length: {
        min: 8,
        max: 16
    }

I went with a range like:

length: [8, 16]

I can support the former too if necessary. The latter seemed simpler to implement when I was working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants