Comments (6)
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.
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
andmax
length. But we can't efficiently without a JSONPath size() filter. Newtonsoft, IIRC, doesn't support asize()
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.
Hi @NicolaPicoli ,
Can you give me a few examples of what you're trying to do?
Thanks,
🌲 🔮 PINES - Fate
from bogus.
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.
Very Nice...
Thanks @bchavez !
from bogus.
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)
- Update Denmark (da) locale from faker-js
- Nuget for .NET 8.0.x HOT 4
- Random history facts generation
- Are Randomizer's number ranges inclusive or exclusive? HOT 3
- bogus is missing NuGet package README file HOT 1
- .NET 8 support HOT 9
- Gender-digit is incorrect for Swedish Personnummer generation HOT 3
- Switch to licence expression rather than URL HOT 1
- Invalid Iban generation for CR (Costa Rica) HOT 3
- Add hungarian (hu) locale from faker-js HOT 2
- How to make convert to generic method HOT 6
- Fakers used inside Rules of other Fakers share the same object reference HOT 2
- Determinism with Custom Datasets HOT 1
- Include the increment count for the Rules Expressions HOT 1
- Stack memory issue in locale tests (overflows on Mac) HOT 2
- Update `zh_CN` dataset to latest HOT 3
- private setters workaround is not working for generic types
- Allow internal constructor if InternalsVisibleTo is used
- Passing parameters to Faker
- LoremPixel service does not exist anymore and should be removed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bogus.