From cd3bc68b7ea190f2410870104866371d2d1f4fbf Mon Sep 17 00:00:00 2001 From: Mehdibenhadjkhelifa <94475712+Mehdibenhadjkhelifa@users.noreply.github.com> Date: Thu, 4 Jul 2024 18:44:41 +0100 Subject: [PATCH] refactor: moved unwanted public functions from Helper.h (#766) (#778) * Refactor:Moved unwanted public functions from Helper.h (#766) * Removed unwanted commits from the AlgoHelper.h --- include/faker-cxx/Faker.h | 2 +- include/faker-cxx/Helper.h | 207 +--------------------------- src/CMakeLists.txt | 1 + src/common/AlgoHelper.h | 78 +++++++++++ src/modules/finance/Finance.cpp | 1 + src/modules/helper/Helper.cpp | 2 +- src/modules/internet/Internet.cpp | 1 + src/modules/location/Location.cpp | 1 + src/modules/person/Person.cpp | 1 + src/modules/phone/Phone.cpp | 1 + src/modules/string/String.cpp | 1 + src/modules/system/System.cpp | 1 + tests/CMakeLists.txt | 2 +- tests/modules/helper/HelperTest.cpp | 1 + 14 files changed, 96 insertions(+), 204 deletions(-) create mode 100644 src/common/AlgoHelper.h diff --git a/include/faker-cxx/Faker.h b/include/faker-cxx/Faker.h index 9c404d094..fb5df0737 100644 --- a/include/faker-cxx/Faker.h +++ b/include/faker-cxx/Faker.h @@ -37,4 +37,4 @@ #include "faker-cxx/Version.h" #include "faker-cxx/VideoGame.h" #include "faker-cxx/Weather.h" -#include "faker-cxx/Word.h" \ No newline at end of file +#include "faker-cxx/Word.h" diff --git a/include/faker-cxx/Helper.h b/include/faker-cxx/Helper.h index 46a01078b..8ffae1651 100644 --- a/include/faker-cxx/Helper.h +++ b/include/faker-cxx/Helper.h @@ -1,17 +1,10 @@ #pragma once -#include -#include #include #include -#include -#include #include -#include #include -#include "faker-cxx/Export.h" -#include "Datatype.h" #include "Number.h" namespace faker::helper @@ -97,7 +90,6 @@ T arrayElement(const std::vector& data) return data[index]; } - /** * @brief Get a random element from an initializer list. * @@ -125,35 +117,19 @@ T arrayElement(const std::initializer_list& data) } /** - * @brief Get a random element from a std::set. + * @brief Get a random element by weight from a vector. * - * @tparam T an element type of the std::set. + * @tparam T an element type of the weighted element. * - * @param std::set of elements. + * @param data vector of weighted elements. * - * @return T a random element from the std::set. + * @return T a weighted element value from the vector. * * @code - * std::set chars{'a', 'b', 'c', 'd', 'e'}; - * faker::helper::setElement(chars) // 'd' + * faker::helper::weightedArrayElement(std::vector>{{1, "value1"}, + * {10, "value2"}}) // "hello2" * @endcode */ -template -T setElement(const std::set& data) -{ - if (data.empty()) - { - throw std::invalid_argument{"Data is empty."}; - } - - T item; - - static std::mt19937 pseudoRandomGenerator(std::random_device{}()); - - std::sample(data.begin(), data.end(), &item, 1, pseudoRandomGenerator); - - return item; -} template struct WeightedElement @@ -162,20 +138,6 @@ struct WeightedElement T value; }; -/** - * @brief Get a random element by weight from a vector. - * - * @tparam T an element type of the weighted element. - * - * @param data vector of weighted elements. - * - * @return T a weighted element value from the vector. - * - * @code - * faker::helper::weightedArrayElement(std::vector>{{1, "value1"}, {10, - * "value2"}}) // "hello2" - * @endcode - */ template T weightedArrayElement(const std::vector>& data) { @@ -214,161 +176,4 @@ T weightedArrayElement(const std::vector>& data) return data.at(currentIdx).value; } -/** - * @brief Returns shuffled std::string - * - * @param data String to be shuffled - * - * @return std::string with shuffled chars - * - * @code - * faker::helper::shuffleString("hello") // "eollh" - * @endcode - */ -FAKER_CXX_EXPORT std::string shuffleString(std::string data); - -/** - * @brief Returns a random key from given object. - * - * @tparam T The type of the object to select from. - * - * @param object The object to be used. - * - * @throws If the given object is empty - * - * @return A random key from given object. - * - * @code - * std::unordered_map testMap = { - * {1, "one"}, - * {2, "two"}, - * {3, "three"} - * }; - * faker::helper::objectKey(testMap) // "2" - * @endcode - */ -template -typename T::key_type objectKey(const T& object) -{ - if (object.empty()) - { - throw std::runtime_error("Object is empty."); - } - - std::vector keys; - - for (const auto& entry : object) - { - keys.push_back(entry.first); - } - - return arrayElement(keys); -} - -/** - * @brief Returns the result of the callback if the probability check was successful, otherwise empty string. - * - * - * @tparam TResult The type of result of the given callback. - * - * @param callback The callback to that will be invoked if the probability check was successful. - * @param probability The probability (`[0.00, 1.00]`) of the callback being invoked. Defaults to `0.5`. - * - * @return The result of the callback if the probability check was successful, otherwise empty string. - * - * @code - * faker::helper::maybe([]() { return "Hello World!"; }) // "" - * faker::helper::maybe([]() { return 42; }, 0.9) // "42" - * @endcode - */ -template -TResult maybe(std::function callback, double probability = 0.5) -{ - if (datatype::boolean(probability)) - { - return callback(); - } - - return TResult(); -} - -/** - * @brief Returns a vector equivalent to the given array. - * - * @tparam T The type of the array. - * @tparam N The size of the array. - * - * @param arr The array to convert. - * - * @return The same array as a vector. - * - * @code - * faker::helper::toVector(std::array{1, 2, 3}) // {1, 2, 3} - * @endcode - */ -template -std::vector toVector(const std::array& arr) -{ - std::vector vec; - vec.reserve(N); - vec.insert(vec.end(), arr.begin(), arr.end()); - return vec; -} - -/** - * @brief Returns the given string parsed symbol by symbol and replaced the placeholders with digits ("0" - "9"). - * "!" will be replaced by digits >=2 ("2" - "9"). - * - * @param str The template to parse string. - * @param symbol The symbol to replace with digits. Defaults to '#'. - * - * @return The string replaced symbols with digits. - * - * @code - * faker::helper::replaceSymbolWithNumber() // "" - * faker::helper::replaceSymbolWithNumber("#####") // "04812" - * faker::helper::replaceSymbolWithNumber("!####") // "27378" - * faker::helper::replaceSymbolWithNumber("Your pin is: !####") // "29841" - * @endcode - */ -FAKER_CXX_EXPORT std::string replaceSymbolWithNumber(const std::string& str, const char& symbol = '#'); - -/** - * @brief Returns credit card schema with replaced symbols and patterns in a credit card including Luhn checksum - * This method supports both range patterns `[4-9]` as well as the patterns used by `replaceSymbolWithNumber()`. - * `L` will be replaced with the appropriate Luhn checksum. - * - * @param inputString TThe credit card format pattern. Defaults to "6453-####-####-####-###L". - * @param symbol The symbol to replace with a digit. Defaults to '#'. - * - * @return The string replaced symbols with digits. - * - * @code - * faker::helper::replaceCreditCardSymbols() // "6453-4876-8626-8995-3771" - * faker::helper::replaceCreditCardSymbols("1234-[4-9]-##!!-L") // "1234-9-5298-2" - * @endcode - */ -FAKER_CXX_EXPORT std::string replaceCreditCardSymbols(const std::string& inputString = "6453-####-####-####-###L", char symbol = '#'); - -/** - * @brief Returns the replaced regex-like expression in the string with matching values. - * - * Supported patterns: - * - `.{times}` => Repeat the character exactly `times` times. - * - `.{min,max}` => Repeat the character `min` to `max` times. - * - `[min-max]` => Generate a number between min and max (inclusive). - * - * @param input The template string to to parse. - * - * @return The replaced regex-like expression in the string with matching values. - * - * @code - * faker::helper::regexpStyleStringParse() // "" - * faker::helper::regexpStyleStringParse("#{5}") // "#####" - * faker::helper::regexpStyleStringParse("#{2,9}") // "#######" - * faker::helper::regexpStyleStringParse("[500-15000]") // "8375" - * faker::helper::regexpStyleStringParse("#{3}test[1-5]") // "###test3" - * @endcode - */ -FAKER_CXX_EXPORT std::string regexpStyleStringParse(const std::string& input); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 99944354c..979169130 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -45,6 +45,7 @@ set(FAKER_SOURCES set(FAKER_HEADERS common/LuhnCheck.h common/FormatHelper.h + common/AlgoHelper.h common/StringHelper.h modules/plant/PlantData.h modules/person/PersonData.h diff --git a/src/common/AlgoHelper.h b/src/common/AlgoHelper.h new file mode 100644 index 000000000..9d57a1020 --- /dev/null +++ b/src/common/AlgoHelper.h @@ -0,0 +1,78 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "faker-cxx/Datatype.h" +#include "faker-cxx/Export.h" +#include "faker-cxx/Helper.h" + +namespace faker::helper +{ +template +static T setElement(const std::set& data) +{ + if (data.empty()) + { + throw std::invalid_argument{"Data is empty."}; + } + + T item; + + static std::mt19937 pseudoRandomGenerator(static_cast(std::random_device{}())); + + std::sample(data.begin(), data.end(), &item, 1, pseudoRandomGenerator); + + return item; +} + +FAKER_CXX_EXPORT std::string shuffleString(std::string data); + +template +static T::key_type objectKey(const T& object) +{ + if (object.empty()) + { + throw std::runtime_error("Object is empty."); + } + + std::vector keys; + + for (const auto& entry : object) + { + keys.push_back(entry.first); + } + + return arrayElement(keys); +} + +template +static TResult maybe(std::function callback, double probability = 0.5) +{ + if (datatype::boolean(probability)) + { + return callback(); + } + return TResult(); +} + +template +static std::vector toVector(const std::array& arr) +{ + std::vector vec; + vec.reserve(N); + vec.insert(vec.end(), arr.begin(), arr.end()); + return vec; +} + +FAKER_CXX_EXPORT std::string replaceSymbolWithNumber(const std::string& str, const char& symbol = '#'); + +FAKER_CXX_EXPORT std::string replaceCreditCardSymbols(const std::string& inputString = "6453-####-####-####-###L", + char symbol = '#'); + +FAKER_CXX_EXPORT std::string regexpStyleStringParse(const std::string& input); +} diff --git a/src/modules/finance/Finance.cpp b/src/modules/finance/Finance.cpp index aac3116ff..22fddf745 100644 --- a/src/modules/finance/Finance.cpp +++ b/src/modules/finance/Finance.cpp @@ -5,6 +5,7 @@ #include #include +#include "../../common/AlgoHelper.h" #include "../../common/FormatHelper.h" #include "faker-cxx/Date.h" #include "faker-cxx/Helper.h" diff --git a/src/modules/helper/Helper.cpp b/src/modules/helper/Helper.cpp index d7071badf..0af713676 100644 --- a/src/modules/helper/Helper.cpp +++ b/src/modules/helper/Helper.cpp @@ -1,4 +1,3 @@ -#include "faker-cxx/Helper.h" #include #include @@ -7,6 +6,7 @@ #include "../../common/LuhnCheck.h" #include "../../common/StringHelper.h" +#include "../../common/AlgoHelper.h" #include "faker-cxx/Number.h" namespace faker::helper diff --git a/src/modules/internet/Internet.cpp b/src/modules/internet/Internet.cpp index 7946ddabe..bac5ad4c3 100644 --- a/src/modules/internet/Internet.cpp +++ b/src/modules/internet/Internet.cpp @@ -11,6 +11,7 @@ #include #include +#include "common/AlgoHelper.h" #include "common/FormatHelper.h" #include "common/StringHelper.h" #include "faker-cxx/Helper.h" diff --git a/src/modules/location/Location.cpp b/src/modules/location/Location.cpp index cc46aa208..c34ea0af3 100644 --- a/src/modules/location/Location.cpp +++ b/src/modules/location/Location.cpp @@ -5,6 +5,7 @@ #include #include "../../common/FormatHelper.h" +#include "../../common/AlgoHelper.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" #include "faker-cxx/Person.h" diff --git a/src/modules/person/Person.cpp b/src/modules/person/Person.cpp index 5ac41f4a8..afdf213d3 100644 --- a/src/modules/person/Person.cpp +++ b/src/modules/person/Person.cpp @@ -6,6 +6,7 @@ #include #include "common/FormatHelper.h" +#include "common/AlgoHelper.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Internet.h" #include "faker-cxx/Number.h" diff --git a/src/modules/phone/Phone.cpp b/src/modules/phone/Phone.cpp index 80a7d5943..5da339fcb 100644 --- a/src/modules/phone/Phone.cpp +++ b/src/modules/phone/Phone.cpp @@ -5,6 +5,7 @@ #include #include +#include "../../common/AlgoHelper.h" #include "faker-cxx/Helper.h" #include "PhoneData.h" diff --git a/src/modules/string/String.cpp b/src/modules/string/String.cpp index 0fdbc0451..9da62b140 100644 --- a/src/modules/string/String.cpp +++ b/src/modules/string/String.cpp @@ -9,6 +9,7 @@ #include #include "common/FormatHelper.h" +#include "common/AlgoHelper.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" #include "faker-cxx/types/Hex.h" diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 317cee8bd..557883484 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -7,6 +7,7 @@ #include #include "../src/common/StringHelper.h" +#include "common/AlgoHelper.h" #include "common/FormatHelper.h" #include "faker-cxx/Datatype.h" #include "faker-cxx/Helper.h" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3427e6125..5dc466eed 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -85,4 +85,4 @@ set_tests_properties(${PROJECT_NAME} PROPERTIES ENVIRONMENT_MODIFICATION if (CODE_COVERAGE) target_code_coverage(${PROJECT_NAME} ALL) -endif () \ No newline at end of file +endif () diff --git a/tests/modules/helper/HelperTest.cpp b/tests/modules/helper/HelperTest.cpp index a4609d181..ffda7cc10 100644 --- a/tests/modules/helper/HelperTest.cpp +++ b/tests/modules/helper/HelperTest.cpp @@ -1,4 +1,5 @@ #include "faker-cxx/Helper.h" +#include #include #include