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

feat: added ordinalDirection generation function #992

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/faker-cxx/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ FAKER_CXX_EXPORT std::tuple<std::string, std::string> nearbyGPSCoordinate(
*/
FAKER_CXX_EXPORT std::string_view direction();

/**
* @brief Generates a random direction from ordinal directions.
*
* @param abbreviated If `true` this will return abbreviated directions (NW, SE, etc). Otherwise this
* will return the long name. By default, this is set to `false`.
*
* @returns Ordinal direction.
*
* @code
* faker::location::ordinalDirection() // "Southeast"
* faker::location::ordinalDirection(true) // "NW"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view ordinalDirection(bool abbreviated = false);

/**
* @brief Generates a random time zone.
*
Expand Down
6 changes: 6 additions & 0 deletions src/modules/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ std::string_view direction()
return helper::randomElement(directions);
}

std::string_view ordinalDirection(bool abbreviated)
{
return abbreviated ? helper::randomElement(ordinalDirections).second :
helper::randomElement(ordinalDirections).first;
}

std::string_view timeZone()
{
return helper::randomElement(timeZones);
Expand Down
7 changes: 7 additions & 0 deletions src/modules/location_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,13 @@ const auto directions = std::to_array<std::string_view>({
"West",
});

constexpr auto ordinalDirections = std::to_array<std::pair<std::string_view, std::string_view>>({
{"Northeast", "NE"},
{"Northwest", "NW"},
{"Southeast", "SE"},
{"Southwest", "SW"}
});

const auto timeZones = std::to_array<std::string_view>({
// clang-format off
"Africa/Abidjan",
Expand Down
16 changes: 16 additions & 0 deletions tests/modules/location_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,22 @@ TEST_F(LocationTest, shouldGenerateDirection)
{ return direction == generatedDirection; }));
}

TEST_F(LocationTest, shouldGenerateOrdinalDirection)
{
const auto generatedOrdinalDirection = ordinalDirection();
const auto generatedOrdinalDirectionAbbreviated = ordinalDirection(true);

ASSERT_TRUE(std::ranges::any_of(
ordinalDirections,
[generatedOrdinalDirection](const std::pair<std::string_view, std::string_view>& ordinalDirection)
{ return ordinalDirection.first == generatedOrdinalDirection; }));

ASSERT_TRUE(std::ranges::any_of(
ordinalDirections,
[generatedOrdinalDirectionAbbreviated](const std::pair<std::string_view, std::string_view>& ordinalDirection)
{ return ordinalDirection.second == generatedOrdinalDirectionAbbreviated; }));
}

TEST_F(LocationTest, shouldGenerateTimeZone)
{
const auto generatedTimeZone = timeZone();
Expand Down
Loading