Skip to content

Commit

Permalink
refactor: moved unwanted public functions from Helper.h (#766) (#778)
Browse files Browse the repository at this point in the history
* Refactor:Moved unwanted public functions from Helper.h (#766)

* Removed unwanted commits from the AlgoHelper.h
  • Loading branch information
Mehdibenhadjkhelifa authored Jul 4, 2024
1 parent 3995d83 commit cd3bc68
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 204 deletions.
2 changes: 1 addition & 1 deletion include/faker-cxx/Faker.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
#include "faker-cxx/Version.h"
#include "faker-cxx/VideoGame.h"
#include "faker-cxx/Weather.h"
#include "faker-cxx/Word.h"
#include "faker-cxx/Word.h"
207 changes: 6 additions & 201 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#pragma once

#include <algorithm>
#include <functional>
#include <initializer_list>
#include <numeric>
#include <random>
#include <set>
#include <span>
#include <string>
#include <vector>

#include "faker-cxx/Export.h"
#include "Datatype.h"
#include "Number.h"

namespace faker::helper
Expand Down Expand Up @@ -97,7 +90,6 @@ T arrayElement(const std::vector<T>& data)
return data[index];
}


/**
* @brief Get a random element from an initializer list.
*
Expand Down Expand Up @@ -125,35 +117,19 @@ T arrayElement(const std::initializer_list<T>& 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<char> chars{'a', 'b', 'c', 'd', 'e'};
* faker::helper::setElement(chars) // 'd'
* faker::helper::weightedArrayElement<std::string>(std::vector<helper::WeightedElement<std::string>>{{1, "value1"},
* {10, "value2"}}) // "hello2"
* @endcode
*/
template <class T>
T setElement(const std::set<T>& 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 <class T>
struct WeightedElement
Expand All @@ -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::string>(std::vector<helper::WeightedElement<std::string>>{{1, "value1"}, {10,
* "value2"}}) // "hello2"
* @endcode
*/
template <class T>
T weightedArrayElement(const std::vector<WeightedElement<T>>& data)
{
Expand Down Expand Up @@ -214,161 +176,4 @@ T weightedArrayElement(const std::vector<WeightedElement<T>>& 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<int, std::string> testMap = {
* {1, "one"},
* {2, "two"},
* {3, "three"}
* };
* faker::helper::objectKey(testMap) // "2"
* @endcode
*/
template <typename T>
typename T::key_type objectKey(const T& object)
{
if (object.empty())
{
throw std::runtime_error("Object is empty.");
}

std::vector<typename T::key_type> keys;

for (const auto& entry : object)
{
keys.push_back(entry.first);
}

return arrayElement<typename T::key_type>(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<std::string>([]() { return "Hello World!"; }) // ""
* faker::helper::maybe<int>([]() { return 42; }, 0.9) // "42"
* @endcode
*/
template <typename TResult>
TResult maybe(std::function<TResult()> 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<int, 3>{1, 2, 3}) // {1, 2, 3}
* @endcode
*/
template <typename T, std::size_t N>
std::vector<T> toVector(const std::array<T, N>& arr)
{
std::vector<T> 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);
}
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions src/common/AlgoHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once

#include <algorithm>
#include <functional>
#include <random>
#include <set>
#include <stdexcept>
#include <string>

#include "faker-cxx/Datatype.h"
#include "faker-cxx/Export.h"
#include "faker-cxx/Helper.h"

namespace faker::helper
{
template <class T>
static T setElement(const std::set<T>& data)
{
if (data.empty())
{
throw std::invalid_argument{"Data is empty."};
}

T item;

static std::mt19937 pseudoRandomGenerator(static_cast<unsigned long>(std::random_device{}()));

std::sample(data.begin(), data.end(), &item, 1, pseudoRandomGenerator);

return item;
}

FAKER_CXX_EXPORT std::string shuffleString(std::string data);

template <typename T>
static T::key_type objectKey(const T& object)
{
if (object.empty())
{
throw std::runtime_error("Object is empty.");
}

std::vector<typename T::key_type> keys;

for (const auto& entry : object)
{
keys.push_back(entry.first);
}

return arrayElement<typename T::key_type>(keys);
}

template <typename TResult>
static TResult maybe(std::function<TResult()> callback, double probability = 0.5)
{
if (datatype::boolean(probability))
{
return callback();
}
return TResult();
}

template <typename T, std::size_t N>
static std::vector<T> toVector(const std::array<T, N>& arr)
{
std::vector<T> 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);
}
1 change: 1 addition & 0 deletions src/modules/finance/Finance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string_view>
#include <vector>

#include "../../common/AlgoHelper.h"
#include "../../common/FormatHelper.h"
#include "faker-cxx/Date.h"
#include "faker-cxx/Helper.h"
Expand Down
2 changes: 1 addition & 1 deletion src/modules/helper/Helper.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "faker-cxx/Helper.h"

#include <algorithm>
#include <random>
Expand All @@ -7,6 +6,7 @@

#include "../../common/LuhnCheck.h"
#include "../../common/StringHelper.h"
#include "../../common/AlgoHelper.h"
#include "faker-cxx/Number.h"

namespace faker::helper
Expand Down
1 change: 1 addition & 0 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <utility>
#include <vector>

#include "common/AlgoHelper.h"
#include "common/FormatHelper.h"
#include "common/StringHelper.h"
#include "faker-cxx/Helper.h"
Expand Down
1 change: 1 addition & 0 deletions src/modules/location/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unordered_map>

#include "../../common/FormatHelper.h"
#include "../../common/AlgoHelper.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "faker-cxx/Person.h"
Expand Down
1 change: 1 addition & 0 deletions src/modules/person/Person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>

#include "common/FormatHelper.h"
#include "common/AlgoHelper.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Internet.h"
#include "faker-cxx/Number.h"
Expand Down
1 change: 1 addition & 0 deletions src/modules/phone/Phone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string_view>
#include <unordered_map>

#include "../../common/AlgoHelper.h"
#include "faker-cxx/Helper.h"
#include "PhoneData.h"

Expand Down
Loading

0 comments on commit cd3bc68

Please sign in to comment.