Skip to content

Commit

Permalink
refactor: location module migrate (#736)
Browse files Browse the repository at this point in the history
* refactor: location module migrate

- location module migration from class to functions within location namespace

Signed-off-by: Guru Mehar Rachaputi <[email protected]>

* modified location reference in example code

Signed-off-by: Guru Mehar Rachaputi <[email protected]>

---------

Signed-off-by: Guru Mehar Rachaputi <[email protected]>
  • Loading branch information
00thirdeye00 authored Jun 25, 2024
1 parent f0aec69 commit 9e09763
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 106 deletions.
4 changes: 2 additions & 2 deletions examples/basic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ int main()
const auto id = faker::String::uuid();
const auto email = faker::Internet::email();
const auto password = faker::Internet::password();
const auto city = faker::Location::city();
const auto streetAddress = faker::Location::streetAddress();
const auto city = faker::location::city();
const auto streetAddress = faker::location::streetAddress();

std::cout << id << std::endl;
std::cout << email << std::endl;
Expand Down
64 changes: 30 additions & 34 deletions include/faker-cxx/Location.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "types/Precision.h"

namespace faker
namespace faker::location
{
enum class AddressCountry
{
Expand All @@ -25,30 +25,27 @@ enum class AddressCountry
Usa,
};

class Location
{
public:
/**
* @brief Returns a random country name.
*
* @returns Country name.
*
* @code
* Location::country() // "Poland"
* location::country() // "Poland"
* @endcode
*/
static std::string_view country();
std::string_view country();

/**
* @brief Returns a random country code.
*
* @returns Country code consisting two letters.
*
* @code
* Location::countryCode() // "PL"
* location::countryCode() // "PL"
* @endcode
*/
static std::string_view countryCode();
std::string_view countryCode();

/**
* @brief Returns a random state for a given country.
Expand All @@ -58,10 +55,10 @@ class Location
* @returns State.
*
* @code
* Location::state() // "Arizona"
* location::state() // "Arizona"
* @endcode
*/
static std::string_view state(AddressCountry country = AddressCountry::Usa);
std::string_view state(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random county for a given country.
Expand All @@ -71,10 +68,10 @@ class Location
* @returns County.
*
* @code
* Location::county() // "Adams County"
* location::county() // "Adams County"
* @endcode
*/
static std::string_view county(AddressCountry country = AddressCountry::Usa);
std::string_view county(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random city for given country.
Expand All @@ -84,10 +81,10 @@ class Location
* @returns City.
*
* @code
* Location::city() // "Boston"
* location::city() // "Boston"
* @endcode
*/
static std::string city(AddressCountry country = AddressCountry::Usa);
std::string city(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random zip code for given country.
Expand All @@ -97,11 +94,11 @@ class Location
* @returns Zip code.
*
* @code
* Location::zipCode() // "47683-9880"
* Location::zipCode(Country::Poland) // "31-881"
* location::zipCode() // "47683-9880"
* location::zipCode(Country::Poland) // "31-881"
* @endcode
*/
static std::string zipCode(AddressCountry country = AddressCountry::Usa);
std::string zipCode(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random street address for given country.
Expand All @@ -111,10 +108,10 @@ class Location
* @returns Street address including building number and street name.
*
* @code
* Location::streetAddress() // "34830 Erdman Hollow"
* location::streetAddress() // "34830 Erdman Hollow"
* @endcode
*/
static std::string streetAddress(AddressCountry country = AddressCountry::Usa);
std::string streetAddress(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random street for given country.
Expand All @@ -124,10 +121,10 @@ class Location
* @returns Street name.
*
* @code
* Location::street() // "Schroeder Isle"
* location::street() // "Schroeder Isle"
* @endcode
*/
static std::string street(AddressCountry country = AddressCountry::Usa);
std::string street(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random building number for given country.
Expand All @@ -137,10 +134,10 @@ class Location
* @returns Building number.
*
* @code
* Location::buildingNumber() // "505"
* location::buildingNumber() // "505"
* @endcode
*/
static std::string buildingNumber(AddressCountry country = AddressCountry::Usa);
std::string buildingNumber(AddressCountry country = AddressCountry::Usa);

/**
* @brief Returns a random secondary address number for given country.
Expand All @@ -151,10 +148,10 @@ class Location
* @returns Secondary address.
*
* @code
* Location::secondaryAddress() // "Apt. 861"
* location::secondaryAddress() // "Apt. 861"
* @endcode
*/
static std::string secondaryAddress(AddressCountry country = AddressCountry::Usa);
std::string secondaryAddress(AddressCountry country = AddressCountry::Usa);

/**
* @brief Generates a random latitude.
Expand All @@ -164,10 +161,10 @@ class Location
* @returns Latitude within -90 to 90 range.
*
* @code
* Location::latitude() // "-30.9501"
* location::latitude() // "-30.9501"
* @endcode
*/
static std::string latitude(Precision precision = Precision::FourDp);
std::string latitude(Precision precision = Precision::FourDp);

/**
* @brief Generates a random longitude.
Expand All @@ -177,31 +174,30 @@ class Location
* @returns Longitude within -180 to 180 range.
*
* @code
* Location::longitude() // "-30.9501"
* location::longitude() // "-30.9501"
* @endcode
*/
static std::string longitude(Precision precision = Precision::FourDp);
std::string longitude(Precision precision = Precision::FourDp);

/**
* @brief Generates a random direction from cardinal and ordinal directions.
*
* @returns Direction.
*
* @code
* Location::direction() // "North"
* location::direction() // "North"
* @endcode
*/
static std::string_view direction();
std::string_view direction();

/**
* @brief Generates a random time zone.
*
* @returns Time zone.
*
* @code
* Location::timeZone() // "Europe/Warsaw"
* location::timeZone() // "Europe/Warsaw"
* @endcode
*/
static std::string_view timeZone();
};
std::string_view timeZone();
}
30 changes: 15 additions & 15 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "faker-cxx/types/Precision.h"
#include "LocationData.h"

namespace faker
namespace faker::location
{
namespace
{
Expand Down Expand Up @@ -95,17 +95,17 @@ Country getCountry(const AddressCountry& addressCountry)
}
}

std::string_view Location::country()
std::string_view country()
{
return Helper::arrayElement(allCountries);
}

std::string_view Location::countryCode()
std::string_view countryCode()
{
return Helper::arrayElement(countryCodes);
}

std::string_view Location::county(AddressCountry country)
std::string_view county(AddressCountry country)
{
const auto& countryAddresses = getAddresses(country);

Expand All @@ -117,14 +117,14 @@ std::string_view Location::county(AddressCountry country)
return Helper::arrayElement(countryAddresses.counties);
}

std::string_view Location::state(AddressCountry country)
std::string_view state(AddressCountry country)
{
const auto& countryAddresses = getAddresses(country);

return Helper::arrayElement(countryAddresses.states);
}

std::string Location::city(AddressCountry country)
std::string city(AddressCountry country)
{
const auto& countryAddresses = getAddresses(country);

Expand All @@ -143,14 +143,14 @@ std::string Location::city(AddressCountry country)
return FormatHelper::fillTokenValues(cityFormat, dataGeneratorsMapping);
}

std::string Location::zipCode(AddressCountry country)
std::string zipCode(AddressCountry country)
{
const auto& countryAddresses = getAddresses(country);

return Helper::replaceSymbolWithNumber(static_cast<std::string>(countryAddresses.zipCodeFormat));
}

std::string Location::streetAddress(AddressCountry country)
std::string streetAddress(AddressCountry country)
{
const auto& countryAddresses = getAddresses(country);

Expand All @@ -164,7 +164,7 @@ std::string Location::streetAddress(AddressCountry country)
return FormatHelper::fillTokenValues(addressFormat, dataGeneratorsMapping);
}

std::string Location::street(AddressCountry country)
std::string street(AddressCountry country)
{
const auto& countryAddresses = getAddresses(country);

Expand All @@ -183,7 +183,7 @@ std::string Location::street(AddressCountry country)
return FormatHelper::fillTokenValues(streetFormat, dataGeneratorsMapping);
}

std::string Location::buildingNumber(AddressCountry country)
std::string buildingNumber(AddressCountry country)
{
const auto& countryAddresses = getAddresses(country);

Expand All @@ -193,7 +193,7 @@ std::string Location::buildingNumber(AddressCountry country)
return Helper::replaceSymbolWithNumber(buildingNumberFormat);
}

std::string Location::secondaryAddress(AddressCountry country)
std::string secondaryAddress(AddressCountry country)
{
const auto& countryAddresses = getAddresses(country);

Expand All @@ -208,26 +208,26 @@ std::string Location::secondaryAddress(AddressCountry country)
return Helper::replaceSymbolWithNumber(secondaryAddressFormat);
}

std::string Location::latitude(Precision precision)
std::string latitude(Precision precision)
{
const std::floating_point auto latitude = number::decimal<double>(-90.0, 90.0);

return FormatHelper::precisionFormat(precision, latitude);
}

std::string Location::longitude(Precision precision)
std::string longitude(Precision precision)
{
const std::floating_point auto longitude = number::decimal<double>(-180.0, 180.0);

return FormatHelper::precisionFormat(precision, longitude);
}

std::string_view Location::direction()
std::string_view direction()
{
return Helper::arrayElement(directions);
}

std::string_view Location::timeZone()
std::string_view timeZone()
{
return Helper::arrayElement(timeZones);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/location/LocationData.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <span>
#include <string_view>

namespace faker
namespace faker::location
{
struct CountryAddressesInfo
{
Expand Down
Loading

0 comments on commit 9e09763

Please sign in to comment.