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

Provide quick (unique) numerify, letterify, bothify and regexify providers #122

Closed
darioseidl opened this issue Apr 27, 2022 · 4 comments · Fixed by #123
Closed

Provide quick (unique) numerify, letterify, bothify and regexify providers #122

darioseidl opened this issue Apr 27, 2022 · 4 comments · Fixed by #123
Labels
core 🧬 Issue related to :core module enhancement 🚀 New feature or request
Milestone

Comments

@darioseidl
Copy link

With JavaFaker, it's possibly to quickly generate strings or numbers with numerify, letterify, bothify and regexify. It's a little hidden, only documented in the javadoc.

numerify replaces # with numbers in a string,
letterify replaces ? with uppercase lettrs in a string,
and bothify does both.
regexify generates a string that matches the a given regular expression.

It would be nice if those were availablein kotlin-faker too, especially if they could also be made unique.

I see the FakerService does have numerify, letterify and generexify, but it seems those are only used for implementing the various providers, and not meant to be used directly?

Basically, it would be great to just write something like,

List(100) { faker.unique.letterify("foo???bar") }

If I could, I would just add the following locally, but FakerService and AbstractFakeDataProvider are package internal.

class Template constructor(fakerService: FakerService) : AbstractFakeDataProvider<Template>(fakerService) {
    override val categoryName = "Template"
    override val localUniqueDataProvider = LocalUniqueDataProvider<Template>()
    override val unique by UniqueProviderDelegate(localUniqueDataProvider)

    fun numerify(template: String) = with(fakerService) { template.numerify() }
    fun letterify(template: String) = with(fakerService) { template.letterify() }
    fun bothify(template: String) = with(fakerService) { template.letterify().numerify() }
    fun regexify(template: String) = with(fakerService) { template.generexify() }
}

Is there a way to add a custom provider like that without having to reference internal classes?

@serpro69
Copy link
Owner

Hi @darioseidl , thanks for opening this issue.

I can definitely see the point in having letterify and the rest as part of the public API. Should be quite easy to add also, just need to think where these functions would fit best "semantically". I'll take a look at adding this stuff later this week.

I've also been thinking about how one could create own implementations of AbstractFakeDataProvider. There are a few limitations with that:

  • it would require to open up a lot of internal functionality
  • and since most of Data Providers read from .yml files, I would also like to support this in the custom implementations. But there's no functionality in place to do that yet (see also Provide ability to extend a dictionary #63)
  • probably more stuff that I'm forgetting as well
    But I'm definitely open to doing this, however I'd prefer to keep issues separated, so for this thing it's better to create a new issue.

@darioseidl
Copy link
Author

Great, thanks! With those functions kotlin-faker would be a nice drop-in replacement for JavaFaker. Adding custom providers is only something I was wondering about, not something that I would really need at the moment.

@serpro69 serpro69 mentioned this issue Apr 28, 2022
3 tasks
@serpro69 serpro69 linked a pull request Apr 28, 2022 that will close this issue
3 tasks
@serpro69 serpro69 added this to the 1.11.0 milestone Apr 29, 2022
@serpro69 serpro69 added enhancement 🚀 New feature or request core 🧬 Issue related to :core module labels Apr 29, 2022
@serpro69
Copy link
Owner

@darioseidl , the PR is merged and documentation for new functions can be found here https://serpro69.github.io/kotlin-faker/wiki/extras/#random-strings-from-templates
These should be available in the latest snapshot version if you want to test it out right away. And if you do test it out, please let me know if you find any issues with the new functionality :)

@darioseidl
Copy link
Author

Thanks, that was quick! I gave the snapshot a try and it works just as I had hoped.

The unqiue value generation is super handy (for those new functions and in general). I had a similar wrapper for generating unique values from JavaFaker, but having it out of the box for all providers is just great.

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

Successfully merging a pull request may close this issue.

2 participants