Skip to content

Commit

Permalink
refactor location class to use regex generator for address formats (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Sep 12, 2023
1 parent b51500c commit 97c0aa6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 59 deletions.
3 changes: 2 additions & 1 deletion include/faker-cxx/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,9 @@ class Number
static std::string hex(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);

private:
static std::string convertToHex(int number);

static std::random_device randomDevice;
static std::mt19937 pseudoRandomGenerator;
static std::string convertToHex(int number);
};
}
71 changes: 17 additions & 54 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include "../../common/StringHelper.h"
#include "data/Countries.h"
#include "data/Directions.h"
#include "data/russia/RussiaAddressFormat.h"
#include "data/russia/RussiaCities.h"
#include "data/russia/RussiaStreetPrefixes.h"
#include "data/States.h"
#include "data/TimeZones.h"
#include "data/usa/UsaAddressFormat.h"
#include "data/usa/UsaCities.h"
#include "data/usa/UsaStreetSuffixes.h"
#include "data/russia/RussiaAddressFormat.h"
#include "data/russia/RussiaCities.h"
#include "data/russia/RussiaStreetPrefixes.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Person.h"
#include "faker-cxx/String.h"
Expand All @@ -23,27 +23,32 @@ namespace faker
namespace
{
const std::map<Country, std::vector<std::string>> countryToCitiesMapping{
{Country::Usa, usaCities}, {Country::Russia, russiaCities},
{Country::Usa, usaCities},
{Country::Russia, russiaCities},
};

const std::map<Country, std::string> countryToZipCodeFormatMapping{
{Country::Usa, usaZipCodeFormat}, {Country::Russia, russiaZipCodeFormat},
{Country::Usa, usaZipCodeFormat},
{Country::Russia, russiaZipCodeFormat},
};

const std::map<Country, std::vector<std::string>> countryToBuildingNumberFormatsMapping{
{Country::Usa, usaBuildingNumberFormats}, {Country::Russia, russiaBuildingNumberFormats},
{Country::Usa, usaBuildingNumberFormats},
{Country::Russia, russiaBuildingNumberFormats},
};

const std::map<Country, std::vector<std::string>> countryToStreetFormatsMapping{
{Country::Usa, usaStreetFormats}, {Country::Russia, russiaStreetFormats},
{Country::Usa, usaStreetFormats},
{Country::Russia, russiaStreetFormats},
};

const std::map<Country, std::vector<std::string>> countryToSecondaryAddressFormatsMapping{
{Country::Usa, usaSecondaryAddressFormats},
};

const std::map<Country, std::string> countryToAddressFormatMapping{
{Country::Usa, usaAddressFormat}, {Country::Russia, russiaAddressFormat},
{Country::Usa, usaAddressFormat},
{Country::Russia, russiaAddressFormat},
};

const std::map<Country, std::vector<std::string>> countryToStreetSuffixesMapping{
Expand Down Expand Up @@ -81,21 +86,7 @@ std::string Location::zipCode(Country country)
{
const auto& zipCodeFormat = countryToZipCodeFormatMapping.at(country);

std::string zipCode;

for (const auto& zipCodeFormatCharacter : zipCodeFormat)
{
if (zipCodeFormatCharacter == '#')
{
zipCode += String::numeric(1);
}
else
{
zipCode += zipCodeFormatCharacter;
}
}

return zipCode;
return Helper::replaceSymbolWithNumber(zipCodeFormat);
}

std::string Location::streetAddress(Country country)
Expand All @@ -115,7 +106,7 @@ std::string Location::streetAddress(Country country)
else if (addressFormatElement == "{street}")
{
addressElements.push_back(street(country));
}
}
}

return StringHelper::join(addressElements, " ");
Expand Down Expand Up @@ -168,21 +159,7 @@ std::string Location::buildingNumber(Country country)

const auto buildingNumberFormat = Helper::arrayElement<std::string>(buildingNumberFormats);

std::string buildingNumber;

for (const auto& buildingNumberFormatCharacter : buildingNumberFormat)
{
if (buildingNumberFormatCharacter == '#')
{
buildingNumber += String::numeric(1);
}
else
{
buildingNumber += buildingNumberFormatCharacter;
}
}

return buildingNumber;
return Helper::replaceSymbolWithNumber(buildingNumberFormat);
}

std::string Location::secondaryAddress(Country country)
Expand All @@ -191,21 +168,7 @@ std::string Location::secondaryAddress(Country country)

const auto secondaryAddressFormat = Helper::arrayElement<std::string>(secondaryAddressFormats);

std::string secondaryAddress;

for (const auto& secondaryAddressFormatCharacter : secondaryAddressFormat)
{
if (secondaryAddressFormatCharacter == '#')
{
secondaryAddress += String::numeric(1);
}
else
{
secondaryAddress += secondaryAddressFormatCharacter;
}
}

return secondaryAddress;
return Helper::replaceSymbolWithNumber(secondaryAddressFormat);
}

std::string Location::latitude(Precision precision)
Expand Down
9 changes: 5 additions & 4 deletions src/modules/number/Number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ std::string Number::hex(std::optional<int> min, std::optional<int> max)

std::string Number::convertToHex(int number)
{
std::stringstream sstream;
sstream << std::hex << number;
std::string hexString = sstream.str();
return hexString;
std::stringstream stream;

stream << std::hex << number;

return stream.str();
}

}

0 comments on commit 97c0aa6

Please sign in to comment.