Coder Social home page Coder Social logo

Comments (6)

bchavez avatar bchavez commented on May 15, 2024 3

Hi @NicolaPicoli ,

I've moved ClampLength string extension to Bogus.Extensions to help work with strings and fake data. This extension method will be available in the next v12 release.

Thanks,
Brian

😎 ☁️ "I just wanna stay in the sun where I find... I know it's hard sometimes"

from bogus.

bchavez avatar bchavez commented on May 15, 2024 1

Hi @NicolaPicoli ,

Thanks for your example. Could you try this:

public static class ExtensionsForString
{
    private static Randomizer r = new Randomizer();

    public static string ClampLength(this string str, int? min = null, int? max = null)
    {
        if( max != null && str.Length > max )
        {
            return str.Substring(0, max.Value);
        }
        if( min != null && min > str.Length )
        {
            var missingChars = min - str.Length;
            var fillerChars = r.Replace("".PadRight(missingChars.Value, '?'));
            return str + fillerChars;
        }
        return str;
    }
}
company.CompanyName(0).ClampLength(12, 15).Dump();

Produces:

"Mitchell Inc"
"Schultz LLCA"
"Mueller IncR"
"Kreiger and Son"
"Welch IncSPC"
"Bashirian LLC"
"Veum IncVVIP"
"Medhurst Inc"
"Paucek Group"
"Hoppe IncPOF"
"Haley IncRUE"
"Waters Group"

I think this is the best we can do. After considering your situation I don't think there's an easy way to do this inside Bogus where we avoid the random filler characters.

The first problem arises where we don't have enough characters to meet the minimum length. What do we do?

  • We could query the faker database again with a request to something between min and max length. But we can't efficiently without a JSONPath size() filter. Newtonsoft, IIRC, doesn't support a size() JSONPath filter in its query select token. We'd have to read the entire list and keep finding words that would meet the requirement. :(

  • Add filler characters like we did above (see ExtensionsForString).

If you have other ideas let me know but I don't think we should add this to Bogus since it's not an ideal solution. Some people might not be okay with the random filler characters.

💥 🔥 "Set it ablaze like a candle wick... Light it up, light it up..."

from bogus.

bchavez avatar bchavez commented on May 15, 2024

Hi @NicolaPicoli ,

Can you give me a few examples of what you're trying to do?

Thanks,
🌲 🔮 PINES - Fate

from bogus.

NicolaPicoli avatar NicolaPicoli commented on May 15, 2024

Hi @bchavez!

Some entities have character size validations. For example, FirstName must be a minimum of 3 characters and a maximum of 30 characters.

I would like when creating a random "FirstName" could include the minimum and maximum number of characters, for example:

var result = new Faker<Customer>()
    .RuleFor(p => p.Name, v => v.Company.CompanyName(0).Length([MIN], [MAX]))

Tks!

from bogus.

NicolaPicoli avatar NicolaPicoli commented on May 15, 2024

Very Nice...

Thanks @bchavez !

from bogus.

aline-almeida avatar aline-almeida commented on May 15, 2024

Nevertheless, I think having a max value would be useful. For example for testing layouts where we want to try different lengths. I would like to have this on full names.

from bogus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.