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

Refactor word module #610

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ set(FAKER_SOURCES
src/modules/videoGame/VideoGameData.cpp
src/modules/weather/Weather.cpp
src/modules/word/Word.cpp
src/modules/word/wordData.cpp
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

source file name should start with upper case - WordData.cpp

src/common/FormatHelper.cpp
src/common/LuhnCheck.cpp
src/common/StringHelper.cpp
Expand Down
40 changes: 27 additions & 13 deletions include/faker-cxx/Word.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

#include <optional>
#include <string>
#include <vector>

namespace faker
{
class Word
{
namespace faker {
class Word {
/**
* @brief Creates a vector of all words.
*
* @returns Vector of all words.
*
* @code
* Word::createAllWords() // ["aboard", "about", "above", "absent", "absorbed", "and" ...]
* ^^^ all words (adjectives, adverbs, conjunctions ect.) ^^^
* @endcode
*
* @note This function is used internally, as a helper
* and it is not intended to be used by the user.
*/
static std::vector<std::string_view> createAllWords();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to cpp file, it is implementation detail - should not be in API file


public:
/**
* @brief Returns a random word.
Expand All @@ -21,7 +35,7 @@ class Word
* Word::sample(5) // "spell"
* @endcode
*/
static std::string sample(std::optional<unsigned> length = std::nullopt);
static std::string_view sample(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a string containing a number of space separated random words.
Expand All @@ -35,7 +49,7 @@ class Word
* Word::words(5) // "before hourly patiently dribble equal"
* @endcode
*/
static std::string words(unsigned numberOfWords = 1);
static std::string_view words(unsigned numberOfWords = 1);

/**
* @brief Returns a random adjective.
Expand All @@ -50,7 +64,7 @@ class Word
* Word::adjective(3) // "bad"
* @endcode
*/
static std::string adjective(std::optional<unsigned> length = std::nullopt);
static std::string_view adjective(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random adverb.
Expand All @@ -65,7 +79,7 @@ class Word
* Word::adverb(5) // "almost"
* @endcode
*/
static std::string adverb(std::optional<unsigned> length = std::nullopt);
static std::string_view adverb(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random conjunction.
Expand All @@ -80,7 +94,7 @@ class Word
* Word::conjunction(6) // "indeed"
* @endcode
*/
static std::string conjunction(std::optional<unsigned> length = std::nullopt);
static std::string_view conjunction(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random interjection.
Expand All @@ -95,7 +109,7 @@ class Word
* Word::interjection(4) // "yuck"
* @endcode
*/
static std::string interjection(std::optional<unsigned> length = std::nullopt);
static std::string_view interjection(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random noun.
Expand All @@ -110,7 +124,7 @@ class Word
* Word::noun(8) // "distance"
* @endcode
*/
static std::string noun(std::optional<unsigned> length = std::nullopt);
static std::string_view noun(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random preposition.
Expand All @@ -125,7 +139,7 @@ class Word
* Word::preposition(4) // "from"
* @endcode
*/
static std::string preposition(std::optional<unsigned> length = std::nullopt);
static std::string_view preposition(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random verb.
Expand All @@ -140,6 +154,6 @@ class Word
* Word::verb(9) // "stabilise"
* @endcode
*/
static std::string verb(std::optional<unsigned> length = std::nullopt);
static std::string_view verb(std::optional<unsigned> length = std::nullopt);
};
}
2 changes: 1 addition & 1 deletion src/modules/person/Person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ std::string Person::bio()
const auto dataGeneratorsMapping = std::unordered_map<std::string, std::function<std::string()>>{
{"bio_part", []() { return Helper::arrayElement(bioPart); }},
{"bio_supporter", []() { return Helper::arrayElement(bioSupporter); }},
{"noun", []() { return Word::noun(); }},
{"noun", []() { return std::string(Word::noun()); }},
{"emoji", []() { return Internet::emoji(); }}};

return FormatHelper::fillTokenValues(randomBioFormat, dataGeneratorsMapping);
Expand Down
3 changes: 2 additions & 1 deletion src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ std::string extension(const std::string& mimeType)

std::string System::fileName(const FileOptions& options)
{
std::string baseName = Word::words();
std::string baseName = std::string(Word::words()); // Konwersja std::string_view na std::string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment

std::string extensionsStr;

if (options.extensionCount > 0)
Expand Down Expand Up @@ -75,6 +75,7 @@ std::string System::fileName(const FileOptions& options)
return baseName + extensionsStr;
}


std::string System::fileExtension(const std::optional<FileType>& mimeType)
{
if (mimeType.has_value())
Expand Down
Loading
Loading