Skip to content

Commit

Permalink
feat: add locale to book module (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Sep 7, 2024
1 parent 766ae4a commit 380b4ac
Show file tree
Hide file tree
Showing 11 changed files with 568 additions and 240 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file

## v3.1.0 (TBD)

### Features

* added locale to `book` module

## v3.0.0 (28.08.2024)

### ⚠ BREAKING CHANGES
Expand All @@ -19,7 +25,7 @@ All notable changes to this project will be documented in this file

* added `Video` module
* added `Esport` module

## v2.0.0 (27.06.2024)

### ⚠ BREAKING CHANGES
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ them for review. Here's how:

## Committing

Pull Request titles need to follow our semantic convention.
Pull Request enUSTitles need to follow our semantic convention.

PR titles are written in following convention: `type: subject`
PR enUSTitles are written in following convention: `type: subject`

**type** is required and indicates the intent of the PR

Expand All @@ -304,7 +304,7 @@ Allowed types are:

**subject** is required and describes what the PR does

Some examples of valid pull request titles:
Some examples of valid pull request enUSTitles:

```shell
feat: add book module
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ int main()
- 🌍 Location - countries, cities, zip codes, street addresses
- 📚 Lorem - lorem words, sentences, paragraphs
- 🏥 Medicine - conditions, medical tests, specialties
- 🎥 Movie - actors, actresses, genres, movie titles
- 🎥 Movie - actors, actresses, genres, movie enUSTitles
- 🎶 Music - artists, song names, genres
- 🔢 Number - random integers, floating point numbers
- 🧑 Person - first, last names, job titles, hobby, genders, sex, nationality, language
- 🧑 Person - first, last names, job enUSTitles, hobby, genders, sex, nationality, language
- 📞 Phone - phone number, IMEI
- 🪴 Plant - plant type, trees, flowers
- ⚽ Sport - sport names, athletes, events
Expand Down
15 changes: 9 additions & 6 deletions include/faker-cxx/book.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
#include <string_view>

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

namespace faker::book
{
/**
* @brief Returns a random book title.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @returns Book title.
*
* @code
* faker::book::title() // "Romeo and Juliet"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view title();
FAKER_CXX_EXPORT std::string_view title(Locale locale = Locale::en_US);

/**
* @brief Returns a random book genre.
Expand All @@ -26,7 +29,7 @@ FAKER_CXX_EXPORT std::string_view title();
* faker::book::genre() // "Fantasy"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view genre();
FAKER_CXX_EXPORT std::string_view genre(Locale locale = Locale::en_US);

/**
* @brief Returns a random book author.
Expand All @@ -37,7 +40,7 @@ FAKER_CXX_EXPORT std::string_view genre();
* faker::book::author() // "William Shakespeare"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view author();
FAKER_CXX_EXPORT std::string_view author(Locale locale = Locale::en_US);

/**
* @brief Returns a random book publisher.
Expand All @@ -48,7 +51,7 @@ FAKER_CXX_EXPORT std::string_view author();
* faker::book::publisher() // "Addison-Wesley"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view publisher();
FAKER_CXX_EXPORT std::string_view publisher(Locale locale = Locale::en_US);

/**
* @brief Returns format of book
Expand All @@ -59,7 +62,7 @@ FAKER_CXX_EXPORT std::string_view publisher();
* faker::book::format() // "Paperback"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view format();
FAKER_CXX_EXPORT std::string_view format(Locale locale = Locale::en_US);

/**
* @brief returns a random book series
Expand All @@ -70,5 +73,5 @@ FAKER_CXX_EXPORT std::string_view format();
* faker::book::series() // "Harry Potter"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view series();
FAKER_CXX_EXPORT std::string_view series(Locale locale = Locale::en_US);
}
50 changes: 38 additions & 12 deletions src/modules/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,59 @@

namespace faker::book
{
std::string_view title()
namespace
{
return helper::randomElement(titles);
const struct BookDefinition& getBookDefinition(Locale locale)
{
switch (locale)
{
case Locale::pl_PL:
return plBookDefinition;
default:
return enUSBookDefinition;
}
}
}

std::string_view genre()
std::string_view title(Locale locale)
{
return helper::randomElement(bookGenres);
const auto& bookDefinition = getBookDefinition(locale);

return helper::randomElement(bookDefinition.titles);
}

std::string_view author()
std::string_view genre(Locale locale)
{
return helper::randomElement(authors);
const auto& bookDefinition = getBookDefinition(locale);

return helper::randomElement(bookDefinition.genres);
}

std::string_view publisher()
std::string_view author(Locale locale)
{
return helper::randomElement(publishers);
const auto& bookDefinition = getBookDefinition(locale);

return helper::randomElement(bookDefinition.authors);
}

std::string_view format()
std::string_view publisher(Locale locale)
{
return helper::randomElement(bookFormats);
const auto& bookDefinition = getBookDefinition(locale);

return helper::randomElement(bookDefinition.publishers);
}

std::string_view series()
std::string_view format(Locale locale)
{
return helper::randomElement(bookSeries);
const auto& bookDefinition = getBookDefinition(locale);

return helper::randomElement(bookDefinition.formats);
}

std::string_view series(Locale locale)
{
const auto& bookDefinition = getBookDefinition(locale);

return helper::randomElement(bookDefinition.series);
}
}
Loading

0 comments on commit 380b4ac

Please sign in to comment.