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

Is there a way to produce random addresses that are country specific? #318

Closed
tLashbrooke opened this issue Aug 13, 2020 · 3 comments
Closed

Comments

@tLashbrooke
Copy link

Trying to generate addresses unique to the UK i.e. country can only be England, Wales, Scotland, Northern Ireland. Counties seems to work. The code I'm using is below, but it's printing out random country names in English

var LocalAddress = new Bogus.DataSets.Address(locale: "en_GB");
            var Name = new Bogus.DataSets.Name(locale: "en_GB");
            
            this.FirstName = Name.FirstName();
            this.LastName = Name.LastName();


            this.Country = LocalAddress.Country();
            this.County = LocalAddress.County();
            
            this.HouseNumber = LocalAddress.BuildingNumber();
            this.StreetName = LocalAddress.StreetName();
            this.City = LocalAddress.City();

            this.PostCode = LocalAddress.ZipCode();`

image

@bchavez
Copy link
Owner

bchavez commented Aug 14, 2020

Hi Thomas,

It looks like the faker.js en_GB countries don't tie into any of the normal/main APIs that faker.js exposes.

"uk_country": [
"England",
"Scotland",
"Wales",
"Northern Ireland"
],
"default_country": [
"England",
"Scotland",
"Wales",
"Northern Ireland"
],

I think you'll have to resort to using the following workaround:

void Main()
{
   var f = new Faker("en_GB");
   
   var country = f.Address.UkCountry();
   country.Dump();
   
   var country2 = f.Address.UkCountry2();
   country2.Dump();
}

public static class AddressExtensionsForUk
{
   public static string UkCountry(this Bogus.DataSets.Address address)
   {
      var countries = new [] {
         "England",
         "Scotland",
         "Wales",
         "Northern Ireland"
       };
       
      return address.Random.ArrayElement(countries);
   }
   
   public static string UkCountry2(this Bogus.DataSets.Address address){
      
      var array = Database.Get(nameof(address), "uk_country", "en_GB") as BArray;
      return address.Random.ArrayElement(array);
   }
}

image

Hope that helps!

Thanks,
Brian

@bchavez bchavez closed this as completed Aug 14, 2020
bchavez added a commit that referenced this issue Aug 14, 2020
…hod in `Bogus.Extensions.UnitedKingdom`.
bchavez added a commit that referenced this issue Aug 14, 2020
@bchavez
Copy link
Owner

bchavez commented Aug 14, 2020

Hi Thomas,

Added Address.CountryOfUnitedKingdom() extension method in Bogus.Extensions.UnitedKingdom.

The following code now works in Bogus v30.0.3:
https://www.nuget.org/packages/Bogus/30.0.3

using Bogus.Extensions.UnitedKingdom;

void Main()
{
   var f = new Faker("en_GB");
   var uk_country = f.Address.CountryOfUnitedKingdom();
   
   uk_country.Dump();
}

image

Update to v30.0.3 and give it try. Let me know how it goes! I hope that helps!

Thanks,
Brian

@tLashbrooke
Copy link
Author

Awesome! Thanks for your help 👍

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

No branches or pull requests

2 participants