Skip to content

Commit

Permalink
refactor: add locale to animal module (#964)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fictionistique authored Oct 16, 2024
1 parent 96f15b0 commit e662b6b
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 106 deletions.
61 changes: 46 additions & 15 deletions include/faker-cxx/animal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,171 +3,202 @@
#include <string_view>

#include "faker-cxx/export.h"
#include "faker-cxx/types/locale.h"

namespace faker::animal
{
/**
* @brief Returns a random species of bear.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of bear.
*
* @code
* faker::animal::bear() // "Polar bear"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view bear();
FAKER_CXX_EXPORT std::string_view bear(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of bird.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of bird.
*
* @code
* faker::animal::bird() // "Black-bellied Whistling-Duck"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view bird();
FAKER_CXX_EXPORT std::string_view bird(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of cat.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of cat.
*
* @code
* faker::animal::cat() // "Chartreux"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view cat();
FAKER_CXX_EXPORT std::string_view cat(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of cetacean.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of cetacean.
*
* @code
* faker::animal::cetacean() // "Blue Whale"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view cetacean();
FAKER_CXX_EXPORT std::string_view cetacean(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of cow.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of cow.
*
* @code
* faker::animal::cow() // "American Angus"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view cow();
FAKER_CXX_EXPORT std::string_view cow(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of crocodilia.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of crocodilia.
*
* @code
* faker::animal::crocodile() // "Dwarf Crocodile"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view crocodile();
FAKER_CXX_EXPORT std::string_view crocodile(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of dog.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of dog.
*
* @code
* faker::animal::dog() // "Shiba Inu"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view dog();
FAKER_CXX_EXPORT std::string_view dog(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of fish.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of fish.
*
* @code
* faker::animal::fish() // "Silver carp"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view fish();
FAKER_CXX_EXPORT std::string_view fish(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of horse.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of horse.
*
* @code
* faker::animal::horse() // "Fjord Horse"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view horse();
FAKER_CXX_EXPORT std::string_view horse(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of insect.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of insect.
*
* @code
* faker::animal::insect() // "Bee"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view insect();
FAKER_CXX_EXPORT std::string_view insect(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of lion.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of lion.
*
* @code
* faker::animal::lion() // "West African Lion"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view lion();
FAKER_CXX_EXPORT std::string_view lion(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of rabbit.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of rabbit.
*
* @code
* faker::animal::rabbit() // "Californian"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view rabbit();
FAKER_CXX_EXPORT std::string_view rabbit(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of rodent.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of rodent.
*
* @code
* faker::animal::rodent() // "Chinchilla"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view rodent();
FAKER_CXX_EXPORT std::string_view rodent(Locale locale = Locale::en_US);

/**
* @brief Returns a random species of snake.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Species of snake.
*
* @code
* faker::animal::snake() // "Boa constrictor"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view snake();
FAKER_CXX_EXPORT std::string_view snake(Locale locale = Locale::en_US);

/**
* @brief Returns a random type of animal.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Type of animal.
*
* @code
* faker::animal::type() // "dog"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view type();
FAKER_CXX_EXPORT std::string_view type(Locale locale = Locale::en_US);
}
101 changes: 71 additions & 30 deletions src/modules/animal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,119 @@

namespace faker::animal
{
std::string_view bear()
namespace
{
return helper::randomElement(bears);
const struct AnimalDefinition& getAnimalDefinition(Locale locale)
{
switch (locale)
{
default:
return enUSAnimalDefinition;
}
}
}
std::string_view bear(Locale locale)
{
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.bears);
}

std::string_view bird()
std::string_view bird(Locale locale)
{
return helper::randomElement(birds);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.birds);
}

std::string_view cat()
std::string_view cat(Locale locale)
{
return helper::randomElement(cats);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.cats);
}

std::string_view cetacean()
std::string_view cetacean(Locale locale)
{
return helper::randomElement(cetaceans);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.cetaceans);
}

std::string_view cow()
std::string_view cow(Locale locale)
{
return helper::randomElement(cows);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.cows);
}

std::string_view crocodile()
std::string_view crocodile(Locale locale)
{
return helper::randomElement(crocodiles);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.crocodiles);
}

std::string_view dog()
std::string_view dog(Locale locale)
{
return helper::randomElement(dogs);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.dogs);
}

std::string_view fish()
std::string_view fish(Locale locale)
{
return helper::randomElement(fishes);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.fishes);
}

std::string_view horse()
std::string_view horse(Locale locale)
{
return helper::randomElement(horses);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.horses);
}

std::string_view insect()
std::string_view insect(Locale locale)
{
return helper::randomElement(insects);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.insects);
}

std::string_view lion()
std::string_view lion(Locale locale)
{
return helper::randomElement(lions);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.lions);
}

std::string_view rabbit()
std::string_view rabbit(Locale locale)
{
return helper::randomElement(rabbits);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.rabbits);
}

std::string_view rodent()
std::string_view rodent(Locale locale)
{
return helper::randomElement(rodents);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.rodents);
}

std::string_view snake()
std::string_view snake(Locale locale)
{
return helper::randomElement(snakes);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.snakes);
}

std::string_view type()
std::string_view type(Locale locale)
{
return helper::randomElement(types);
const auto& animalDefinition = getAnimalDefinition(locale);

return helper::randomElement(animalDefinition.types);
}
}
Loading

0 comments on commit e662b6b

Please sign in to comment.